我为我的 Web 服务应用程序创建了一个 Web 设置并成功部署。但是,Web 设置将始终在 inetpub\wwwroot 目录下创建我的虚拟目录(它恰好在我的 C 驱动器中)。
无论如何我可以让网络设置在另一个位置创建虚拟目录,例如,d-drive,因为我的客户在 d 驱动器中有 wwwroot。我已经尝试删除 web setup 的安装地址步骤,但它仍然默认为 inetpub\wwwroot。
我为我的 Web 服务应用程序创建了一个 Web 设置并成功部署。但是,Web 设置将始终在 inetpub\wwwroot 目录下创建我的虚拟目录(它恰好在我的 C 驱动器中)。
无论如何我可以让网络设置在另一个位置创建虚拟目录,例如,d-drive,因为我的客户在 d 驱动器中有 wwwroot。我已经尝试删除 web setup 的安装地址步骤,但它仍然默认为 inetpub\wwwroot。
这是我编写的一些代码,可以让您了解我如何创建虚拟目录。我刚刚在编写代码时包含了代码,因此其中有几个信念的飞跃。但希望入站参数是显而易见的(尽管请注意,该方法希望您知道网站的 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;
}
}
刚刚注意到顶部的匈牙利符号 - 畏缩!