在C#中将虚拟目录转换为应用程序

时间:2014-06-10 08:24:08

标签: c# iis-7

我的项目中有一个场景,可以在运行时添加子域(意味着可以在用户操作上生成新的网站)。

我使用目录服务完成了它,但它将其创建为虚拟目录而不是应用程序。以下是我使用的代码。

public static string CreateVDir(string WebSite, string VDirName, string Path, bool RootDir, bool chkRead, bool chkWrite, bool chkExecute, bool
                                    chkScript, bool chkAuth, int webSiteNum, string serverName)
    {
        string sRet = String.Empty;
        System.DirectoryServices.DirectoryEntry IISSchema;
        System.DirectoryServices.DirectoryEntry IISAdmin;
        System.DirectoryServices.DirectoryEntry VDir;
        bool IISUnderNT;
        IISSchema = new System.DirectoryServices.DirectoryEntry("IIS://" + serverName + "/Schema/AppIsolated");
        if (IISSchema.Properties["Syntax"].Value.ToString().ToUpper() == "BOOLEAN")
            IISUnderNT = true;
        else
            IISUnderNT = false;
        IISAdmin = new System.DirectoryServices.DirectoryEntry("IIS://localhost/W3SVC/" + webSiteNum + "/Root");
        if (!RootDir)
        {
            foreach (System.DirectoryServices.DirectoryEntry v in IISAdmin.Children)
            {
                if (v.Name == VDirName)
                {
                    // Delete the specified virtual directory if it already exists
                    try
                    {
                        IISAdmin.Invoke("Delete", new string[] { v.SchemaClassName, VDirName });
                        IISAdmin.CommitChanges();
                    }
                    catch (Exception ex)
                    {
                        sRet += ex.Message;
                    }
                }
            }
        }

        //
        // Create the virtual directory
        //
        if (!RootDir)
        {
            VDir = IISAdmin.Children.Add(VDirName, "IIsWebVirtualDir");
        }
        else
        {
            VDir = IISAdmin;
        }

        //
        // Setup the VDir
        //
        VDir.Properties["AccessRead"][0] = chkRead;
        VDir.Properties["AccessExecute"][0] = chkExecute;
        VDir.Properties["AccessWrite"][0] = chkWrite;
        VDir.Properties["AccessScript"][0] = chkScript;
        VDir.Properties["AuthNTLM"][0] = chkAuth;
        VDir.Properties["EnableDefaultDoc"][0] = true;
        VDir.Properties["EnableDirBrowsing"][0] = false;
        VDir.Properties["DefaultDoc"][0] = true;
        VDir.Properties["Path"][0] = Path;
        VDir.Properties["AppFriendlyName"][0] = VDirName;
        //
        // NT doesn't support this property
        //
        if (!IISUnderNT)
        {
            VDir.Properties["AspEnableParentPaths"][0] = true;
        }
        VDir.CommitChanges();
        if (IISUnderNT)
        {
            VDir.Invoke("AppCreate", false);
        }
        else
        {
            VDir.Invoke("AppCreate", 1);
        }
        sRet += "VRoot " + VDirName + " created!";
        return sRet;
    }

0 个答案:

没有答案
相关问题