[英]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.