简体   繁体   中英

How to change physical path of virtual directory?

I have created a web setup for my web service application and managed to deploy successfully. However, the web setup will always create my virtual directory under the inetpub\\wwwroot directory (which happens to be in my C-drive).

Is there anyway I can make the web setup create the virtual directory in another location, eg, d-drive as my client have wwwroot in d drive. I have tried to remove the installation address step of the web setup, but it still default to inetpub\\wwwroot.

Here's some code I wrote which will give you an idea of how I create a virtual directory. I've just included the code as I wrote it, so there are a couple of leaps of faith in there. But hopefully the inbound parameters are obvious (although note that the method expects that you know the ID of the web site). Check out also the second method (called by the first) - these two methods in combination will allow you to access an existing site's existing virtual directory, and to set various properties including the "Path" property.

Hope this helps you along the way...

    /// <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 ;
        }
    }

Also...

    /// <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;
        }
    }

Just noticed the hungarian notation right at the top - Cringe!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM