2009-01-22 18 views
0

我面临的一个奇怪的问题在IIS 7.0:怪异的行为在IIS 7.0中 - 的System.DirectoryServices

我有以下虚拟目录在IIS: alt text http://i39.tinypic.com/4iijbb.jpg

只有Windows身份验证模式上启用在IIS

现在,如果我尝试虚拟目录以这种方式得到相关的DirectoryEntry为TestV /文件夹/ file.aspx:

string vDir = @"/TestV/folder/file.aspx"; 

      DirectoryEntry dir = new DirectoryEntry("IIS://" + serverName + "/W3SVC/1/ROOT" + vDir, @"adminusername", @"password"); 
      dir.AuthenticationType = AuthenticationTypes.Secure; 

      try 
      { 
       Console.WriteLine(dir.Name); 
      } 
      catch (Exception exp) 
      { 
       Console.WriteLine(exp.Message); 
      } 

      Console.WriteLine(""); 

我得到异常: “系统找不到指定的路径:”如果我回到IIS,然后执行以下步骤

现在: 右键单击TestV /文件夹并启用匿名身份验证模式,然后再次关闭

上TestV /文件夹右键/ file.aspx 并启用匿名身份验证模式,然后再次关闭

基本上我只是进行一些手动址O n aspx文件Testv/Folder/file.aspx。

上述步骤后,如果我重新运行该程序,代码能够顺利访问该目录条目,并成功打印的名称(file.aspx)

这里有什么问题吗?

另外一个信息:

我在IIS 6.0上也看到了这种行为。所以它看起来像直到并且除非我在IIS中为虚拟目录中的文件夹/文件执行一些手动操作,否则它不会在活动目录中创建相应的元数据?

回答

1

我得到了答案的问题(有一些帮助,从我的一个同事)

这里是解决方案: 1.程序之前需要访问条目添加到IIS元数据(伪?)文件/虚拟目录下的文件夹,在我们访问的条目:

try 
      { 
       // make pseudo entries: 
       DirectoryEntry folder = rootDir.Children.Add("Folder", "IISWebDirectory"); 
       folder.CommitChanges(); 
       file = folder.Children.Add("File.aspx", "IISWebFile"); 
       file.CommitChanges(); 
      } 

然后瞧它的工作原理

PS:

DirectoryEntry dir = new DirectoryEntry("IIS://" + serverName + "/W3SVC/1/ROOT" + vDir, @"adminusername", @"password"); 
dir.AuthenticationType = AuthenticationTypes.Secure; 
dir.RefreshCache(); 

Directory.Refresh不利于

+0

您可以使用静态DirectoryEntry.Exists方法,看是否存在条目。 – robertburke 2013-01-17 17:45:59

0

如果您在第三行之后立即调用RefreshCache(),它有帮助吗?

DirectoryEntry dir = new DirectoryEntry("IIS://" + serverName + "/W3SVC/1/ROOT" + vDir, @"adminusername", @"password"); 
dir.AuthenticationType = AuthenticationTypes.Secure; 
dir.RefreshCache(); 
0

虽然这不完全是一个答案,我会指出System.DirectoryServices通常不用于与IIS交互。虽然它可以让您访问IIS设置,但WMI通常是更好的选择。