如何更改虚拟目录的物理路径?

时间:2012-05-23 10:21:35

标签: c# deployment installer

我为我的Web服务应用程序创建了一个Web设置,并设法成功部署。但是,Web设置将始终在inetpub \ wwwroot目录下创建我的虚拟目录(恰好在我的C盘中)。

无论如何,我可以让网页设置创建虚拟 另一个位置的目录,例如d-drive,因为我的客户端在d盘中有wwwroot。我试图删除Web设置的安装地址步骤,但它仍然默认为inetpub \ wwwroot。

1 个答案:

答案 0 :(得分:2)

以下是我编写的一些代码,可以让您了解我如何创建虚拟目录。我刚刚编写代码时已经包含了代码,所以在那里有一些信念。但希望入站参数是显而易见的(尽管请注意该方法需要您知道网站的ID)。还要检查第二种方法(由第一种方法调用) - 组合这两种方法将允许您访问现有站点的现有虚拟目录,并设置各种属性,包括“路径”属性。

希望这可以帮助你一路......

    /// <summary>
    /// Creates a virtual directory on the specified web site
    /// </summary>
    /// <param name="serverName">name of server</param>
    /// <param name="siteId">Site Id</param>
    /// <param name="directoryName">Virtual Directory Name</param>
    /// <param name="physicalPath">physical path of directory</param>
    /// <param name="bAnonymousLogon">Integrated Windows authentication or Anonymous Logon</param>
    /// <param name="defaultDoc">The default document for the site.</param>
    /// <returns>The Virtual Directory</returns>
    public DirectoryEntry CreateVirtualDirectory(string serverName, int siteId, string directoryName, string physicalPath, bool bAnonymousLogon, string defaultDoc)
    {
        try
        {
            // Does the directory exist already?
            DirectoryEntry vDir = GetVirtualDirectory(serverName, siteId, directoryName);

            if (vDir != null)
            {
                // Yes, it does, no need to create
                _Logger.Log(String.Format(CultureInfo.CurrentUICulture, VirDirAlreadyExists, directoryName, siteId));
            }
            else
            {
                // No, we have some creating to do
                DirectoryEntry root = new DirectoryEntry("IIS://" + serverName + "/W3SVC/" + siteId.ToString(CultureInfo.CurrentUICulture) + "/Root");
                vDir = root.Children.Add(directoryName, IisVirtualDirectory);
                vDir.CommitChanges();
                _Logger.Log(String.Format(CultureInfo.CurrentUICulture, VirDirCreated, directoryName, siteId));
            }

            // Created or not, we want to set the virtual directory's properties
            // We take a risk here by using literals rather than variables (need to make sure the
            // log value is the same as the actual entry)
            // But its not really much of a risk - the main reason for logging is to record progress
            // through the "set" operation

            _Logger.Log(String.Format(CultureInfo.CurrentUICulture, VirDirCreatePath, physicalPath));
            vDir.Properties["Path"][0] = physicalPath;

            _Logger.Log(String.Format(CultureInfo.CurrentUICulture, VirDirCreateAccessRead, true.ToString(CultureInfo.CurrentUICulture)));
            vDir.Properties["AccessRead"][0] = true;

            _Logger.Log(String.Format(CultureInfo.CurrentUICulture, VirDirCreateAccessExecute, true.ToString(CultureInfo.CurrentUICulture)));
            vDir.Properties["AccessExecute"][0] = true;

            _Logger.Log(String.Format(CultureInfo.CurrentUICulture, VirDirCreateAccessWrite, false.ToString(CultureInfo.CurrentUICulture)));
            vDir.Properties["AccessWrite"][0] = false;

            _Logger.Log(String.Format(CultureInfo.CurrentUICulture, VirDirCreateAccessScript, false.ToString(CultureInfo.CurrentUICulture)));
            vDir.Properties["AccessScript"][0] = false;

            _Logger.Log(String.Format(CultureInfo.CurrentUICulture, VirDirCreateAuthNtlm, (!bAnonymousLogon).ToString(CultureInfo.CurrentUICulture)));
            vDir.Properties["AuthNTLM"][0] = !bAnonymousLogon;

            _Logger.Log(String.Format(CultureInfo.CurrentUICulture, VirDirCreateAuthAnon, bAnonymousLogon.ToString(CultureInfo.CurrentUICulture)));
            vDir.Properties["AuthAnonymous"][0] = bAnonymousLogon;

            _Logger.Log(String.Format(CultureInfo.CurrentUICulture, VirDirCreateDefaultDoc, true.ToString(CultureInfo.CurrentUICulture)));
            vDir.Properties["EnableDefaultDoc"][0] = true;

            if (!string.IsNullOrEmpty(defaultDoc))
            {
                _Logger.Log(String.Format(CultureInfo.CurrentUICulture, VirDirCreateDefaultDoc,
                                          defaultDoc.ToString(CultureInfo.CurrentUICulture)));
                vDir.Properties["DefaultDoc"][0] = defaultDoc + ",default.htm,default.aspx,default.asp";
            }

            _Logger.Log(String.Format(CultureInfo.CurrentUICulture, VirDirCreateDirBrowse, false.ToString(CultureInfo.CurrentUICulture)));
            vDir.Properties["EnableDirBrowsing"][0] = false;

            _Logger.Log(String.Format(CultureInfo.CurrentUICulture, VirDirCreateAppFriendlyName, directoryName));
            vDir.Properties["AppFriendlyName"][0] = directoryName;    // The name of the application

            if (_InstalledIISVersion >= IisVersion.Iis6)
            {
                _Logger.Log(String.Format(CultureInfo.CurrentUICulture, VirDirCreateAppPool, directoryName));
                vDir.Properties["AppPoolId"][0] = directoryName;
            }

            // Commit the changes to the directory
            vDir.CommitChanges();

            // Create out-of-process application
            vDir.Invoke("AppCreate", false);

            _Logger.Log(VirDirCreateSetUp);

            return vDir;
        }
        catch (Exception ex)
        {
            _Logger.Log(String.Format(CultureInfo.CurrentUICulture, VirDirCreateException, directoryName, siteId));
            _Logger.Log(ex);
            throw ;
        }
    }

也...

    /// <summary>
    /// Determines whether the directory entry exists on the specified site
    /// </summary>
    /// <param name="serverName">name of server</param>
    /// <param name="siteId">site id</param>
    /// <param name="directoryName">virtual directory name</param>
    /// <returns>the Virtual Directory, if it exists, otherwise null</returns>
    private DirectoryEntry GetVirtualDirectory(string serverName, int siteId, string directoryName)
    {
        try
        {
            DirectoryEntry root = new DirectoryEntry("IIS://" + serverName + "/W3SVC/" + siteId.ToString(CultureInfo.CurrentUICulture) + "/Root");

            foreach (DirectoryEntry entry in root.Children)
            {
                if (0 == String.Compare(directoryName, entry.Name, true, CultureInfo.CurrentUICulture))
                {
                    _Logger.Log(String.Format(CultureInfo.CurrentUICulture, VirDirFound, directoryName, serverName));
                    return entry;
                }
            }

            _Logger.Log(String.Format(CultureInfo.CurrentUICulture, VirDirNotFound, directoryName, serverName));
            return null;
        }
        catch (Exception ex)
        {
            _Logger.Log(String.Format(CultureInfo.CurrentUICulture, VirDirException, directoryName, siteId, serverName));
            _Logger.Log(ex);
            throw;
        }
    }

刚刚注意到顶部的匈牙利符号 - Cringe!