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

本文关键字:路径 何更改 虚拟 | 更新日期: 2023-09-27 18:34:58

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

无论如何,我可以让网络设置创建虚拟的目录在另一个位置,例如,D-Drive,因为我的客户端在D驱动器中有wwwroot。我试图删除网络安装程序的安装地址步骤,但它仍然默认为 inetpub''wwwroot。

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

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

希望这对您一路有所帮助...

    /// <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!