ASP.Net管理IIS7
老程序是管理IIS6,服务器更新IIS7后不能使用。
在网上查询到这个地址的介绍http://dflying.cnblogs.com/archive/2006/04/17/377276.html
不过按照上面的代码不能生成。
ServerManager iisManager = new ServerManager();
iisManager.Sites.Add("NewSite", "http", "*:8080:", "d:\\MySite");
iisManager.Update();
iisManager.Sites.Add("NewSite", "http", "*:8080:", "d:\\MySite");
iisManager.Update();
显示没有 Update方法。
经过查询MSDN,找到解决办法,调试成功。
/// <summary>
/// IIS7创建网站
/// </summary>
/// <param name="name">网站名称</param>
/// <param name="Port">端口</param>
/// <param name="domain">绑定域名</param>
public void CreateSiteByName(string name, string Port, string domain)
{
string path = @"C:\web\" + name + "site";
// Validate the site name
char[] invalid = SiteCollection.InvalidSiteNameCharacters();
if (name.IndexOfAny(invalid) > -1)
{
Console.WriteLine("Invalid site name: {0}", name);
}
// Create the content directory if it doesn't exist.
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
// Create a new site using the new directory and update the config
ServerManager manager = new ServerManager();
try
{ // Add this site.
Site hrSite = manager.Sites.Add(name, "http", "*:" + Port + ":" + domain, path);
hrSite.ServerAutoStart = true;
manager.CommitChanges();
Console.WriteLine("Site " + name + " added to ApplicationHost.config file.");
}
catch
{
}
}
/// IIS7创建网站
/// </summary>
/// <param name="name">网站名称</param>
/// <param name="Port">端口</param>
/// <param name="domain">绑定域名</param>
public void CreateSiteByName(string name, string Port, string domain)
{
string path = @"C:\web\" + name + "site";
// Validate the site name
char[] invalid = SiteCollection.InvalidSiteNameCharacters();
if (name.IndexOfAny(invalid) > -1)
{
Console.WriteLine("Invalid site name: {0}", name);
}
// Create the content directory if it doesn't exist.
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
// Create a new site using the new directory and update the config
ServerManager manager = new ServerManager();
try
{ // Add this site.
Site hrSite = manager.Sites.Add(name, "http", "*:" + Port + ":" + domain, path);
hrSite.ServerAutoStart = true;
manager.CommitChanges();
Console.WriteLine("Site " + name + " added to ApplicationHost.config file.");
}
catch
{
}
}
程序发布的时候显示有没权限。需要给C:\Windows\System32\inetsrv\config添加network service用户权限才行。