[英]Creating Virtual directory in IIS with c#
這個需要一點解釋...
我想自動設置IIS,以便我們的集成測試(使用Watin)可以在任何環境下運行。 為此,我想創建一個Setup和Teardown,分別創建和銷毀一個虛擬目錄。
我想到的解決方案是使用MSBuild社區任務通過模擬的IBuildEngine自動執行代碼中的IIS。
但是,當我嘗試創建虛擬目錄時,出現以下錯誤代碼: 0x80005008 。 編輯:我從早期的嘗試中刪除了工件,現在我得到了IndexOutOfRangeException
我在帶有IIS7的Vista上。 這是我用來運行任務的代碼:
var buildEngine = new MockedBuildEngine();
buildEngine.OnError += (o, e) => Console.WriteLine("ERROR" + e.Message);
buildEngine.OnMessage += (o, e) => Console.WriteLine("MESSAGE" + e.Message);
buildEngine.OnWarning += (o, e) => Console.WriteLine("WARNING: " + e.Message);
var createWebsite = new MSBuild.Community.Tasks.IIS.WebDirectoryCreate()
{
ServerName = "localhost",
VirtualDirectoryPhysicalPath = websiteFolder.FullName,
VirtualDirectoryName = "MedicatiebeheerFittests",
AccessRead = true,
AuthAnonymous = true,
AnonymousPasswordSync = false,
AuthNtlm = true,
EnableDefaultDoc = true,
BuildEngine = buildEngine
};
createWebsite.Execute();
有人知道這是怎么回事嗎? 還是有人知道更好的方法?
這是我的應用程序使用的引用System.DirectoryServices dll的代碼:
using System.DirectoryServices;
using System.IO;
/// <summary>
/// Creates the virtual directory.
/// </summary>
/// <param name="webSite">The web site.</param>
/// <param name="appName">Name of the app.</param>
/// <param name="path">The path.</param>
/// <returns></returns>
/// <exception cref="Exception"><c>Exception</c>.</exception>
public static bool CreateVirtualDirectory(string webSite, string appName, string path)
{
var schema = new DirectoryEntry("IIS://" + webSite + "/Schema/AppIsolated");
bool canCreate = !(schema.Properties["Syntax"].Value.ToString().ToUpper() == "BOOLEAN");
schema.Dispose();
if (canCreate)
{
bool pathCreated = false;
try
{
var admin = new DirectoryEntry("IIS://" + webSite + "/W3SVC/1/Root");
//make sure folder exists
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
pathCreated = true;
}
//If the virtual directory already exists then delete it
IEnumerable<DirectoryEntry> matchingEntries = admin.Children.Cast<DirectoryEntry>().Where(v => v.Name == appName);
foreach (DirectoryEntry vd in matchingEntries)
{
admin.Invoke("Delete", new[] { vd.SchemaClassName, appName });
admin.CommitChanges();
break;
}
//Create and setup new virtual directory
DirectoryEntry vdir = admin.Children.Add(appName, "IIsWebVirtualDir");
vdir.Properties["Path"][0] = path;
vdir.Properties["AppFriendlyName"][0] = appName;
vdir.Properties["EnableDirBrowsing"][0] = false;
vdir.Properties["AccessRead"][0] = true;
vdir.Properties["AccessExecute"][0] = true;
vdir.Properties["AccessWrite"][0] = false;
vdir.Properties["AccessScript"][0] = true;
vdir.Properties["AuthNTLM"][0] = true;
vdir.Properties["EnableDefaultDoc"][0] = true;
vdir.Properties["DefaultDoc"][0] =
"default.aspx,default.asp,default.htm";
vdir.Properties["AspEnableParentPaths"][0] = true;
vdir.CommitChanges();
//the following are acceptable params
//INPROC = 0, OUTPROC = 1, POOLED = 2
vdir.Invoke("AppCreate", 1);
return true;
}
catch (Exception)
{
if (pathCreated)
Directory.Delete(path);
throw;
}
}
return false;
}
這可能有點麻煩,因為它不是C#或MSBuild,但是如何使用簡單的vbscript做到這一點呢?
http://msdn.microsoft.com/zh-CN/library/ms951564.aspx#autoadm_topic5
可以在MSBuild任務中進行配置。
如何使用Dylan建議的相同方法直接在C#中使用?
請參閱: 使用System.DirectoryServices創建站點和虛擬目錄作為起點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.