c# - 使用c#在IIS中创建虚拟目录

标签 c# iis msbuild

这个需要一点解释...

我想自动设置 IIS,以便我们的集成测试(使用 Watin)可以在任何环境中运行。为此,我想创建一个 Setup 和 Teardown,分别创建和销毁一个虚拟目录。

我想到的解决方案是使用 MSBuild Community Tasks使用模拟的 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# - 使用c#在IIS中创建虚拟目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1243600/

相关文章:

c# - 如何在 C# .Net 4.6 WPF 应用程序中隐藏 ConnectionString

visual-studio-2008 - 在Visual Studio 2008中使用发布时删除pdb文件

visual-studio - Visual Studio 中是否有 "ZIP project"?

c# - 使用已知的 XSD 从 XML 中读取类型化对象

c# - 在 c# asp.net 中获取列表框的选定值

c# - 试图理解 GetHashCode()

.net - 如何在配置 Jenkins 作业时排除/忽略项目(Dot Net)

authentication - 网站SSL认证

asp.net-mvc-3 - 为什么我无法访问 IIS7.5 下 MVC3 应用程序中的 Content 文件夹?

iis - 如何在 Windows Server 2012 r2 上为托管在 websphere 上的应用程序创建自签名证书