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用户权限才行。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?