How to get the IIS root path in other application.

如果在其它应用程序(非web application)里取得IIS的根目录
[Key words:IIS, rootPath,DirectoryEntry]

想必会写ASP代码或者ASP.net代码的人,没有一个不知道怎样取得IIS目录的。然而我又遇到一个新问题,那就是如果在其它应该程序里取得IIS的根目录。
首先还是给出问题:如何在ASP.net应该多线程而且可以取得IIS的目录。在一个多线程线程里发送邮件,这样用户的页面就可以很快的返回而不用等待。这应该是一个好主意,而且还有其它的很多事情可以在服务器上设定非HttpContent相关的线程来执行任务。然而,一但与HttpContent不相关后,那么就不得取得Server.MapPath()方法来取得IIS的目录。
我试着想过用APPSetting,而这样一要在不同的服务器上修改web.config文件,因此并不是一个好的办法。
先看看例子,如何取得IIS目录:
    unsafe public static void Main()
    
{
        DirectoryEntry iis 
= new DirectoryEntry("IIS://localhost/W3SVC");
        
foreach(DirectoryEntry index in iis.Children)
        
{
            
if(index.SchemaClassName == "IIsWebServer")
            
{
                
int id = Convert.ToInt32(index.Name);
                DirectoryEntry site 
= new DirectoryEntry("IIS://localhost/W3SVC/" + id);
                
string siteName = site.Properties["ServerComment"].Value.ToString();
                DirectoryEntry rootVDir 
= new DirectoryEntry("IIS://localhost/W3SVC/" + id + "/Root");
                
string rootPath = rootVDir.Properties["Path"].Value.ToString();
                
// write site name and root path to console
                Console.WriteLine("{0}: \"{1}\" -> {2}", id, siteName, rootPath);
            }

            Console.WriteLine(index.Name
+":"+index.Path+":"+index.SchemaClassName);
        }

    }



这样一来,就可以让服务器为我们做更多事情了,而且是在没有任何用户对服务器发生请求的时候。

关于如果在服务器上设定Timer,可以参考我的以前的博客以及博客堂宝玉的一篇相关的文章。
================================
  /\_/\                        
 (=^o^=)  Wu.Country@侠缘      
 (~)@(~)  一辈子,用心做一件事!
--------------------------------
  学而不思则罔,思而不学则怠!  
================================
原文地址:https://www.cnblogs.com/WuCountry/p/310730.html