C# 批量获取IIS网站信息 批量创建IIS网站

public class WebsiteBinding

{

public string Protocol { set; get; }

public string Host { set; get; }

public int Port { set; get; }

}

public class WebsiteInfo

{

public string Name { set; get; }

public string PhysicalPath { set; get; }

public List Bindings { set; get; } = new List();

}

private void collectWebsitService(object obj)

{

try

{

string fn = (string)obj;

foreach (Control control in this.Controls) control.Enabled = false;

List websitelst = new List();

using (ServerManager serverManager = new ServerManager())

{

// 获取所有网站

var websites = serverManager.Sites;

progressBar1.Value = 0;

progressBar1.Minimum = 0;

progressBar1.Maximum = websites.Count;

// 遍历并显示每个网站的信息

foreach (Site site in websites)

{

progressBar1.Value += 1;

WebsiteInfo website = new WebsiteInfo();

website.Name = site.Name;

foreach (var bd in site.Bindings)

{

WebsiteBinding binding = new WebsiteBinding();

binding.Host = bd.Host;

binding.Protocol = bd.Protocol;

binding.Port = bd.EndPoint.Port;

website.Bindings.Add(binding);

}

website.PhysicalPath = site.Applications[0].VirtualDirectories[0].PhysicalPath;

websitelst.Add(website);

}

}

string json = JsonConvert.SerializeObject(websitelst);

File.WriteAllText(fn, json, Encoding.UTF8);

ThreadMessageBox.Show(this, "提取成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

catch (ThreadAbortException) { }

catch (Exception ex)

{

ThreadMessageBox.Show(this, ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

}

finally

{

foreach (Control control in this.Controls) control.Enabled = true;

}

}

private void btnCollect_Click(object sender, EventArgs e)

{

SaveFileDialog dlg = new SaveFileDialog();

dlg.Filter = "Json文档(*.json)|*.json";

if (dlg.ShowDialog() != DialogResult.OK) return;

_thread = new Thread(new ParameterizedThreadStart(collectWebsitService));

_thread.Start(dlg.FileName);

}

private void btnCreate_Click(object sender, EventArgs e)

{

OpenFileDialog dlg = new OpenFileDialog();

dlg.Filter = "Json文档(*.json)|*.json";

if (dlg.ShowDialog() != DialogResult.OK) return;

_thread = new Thread(new ParameterizedThreadStart(createWebsiteService));

_thread.Start(dlg.FileName);

}

private void createWebsiteService(object obj)

{

try

{

foreach (Control control in this.Controls) control.Enabled = false;

string fn = (string)obj;

string json = File.ReadAllText(fn, Encoding.UTF8);

List websites = JsonConvert.DeserializeObject>(json);

progressBar1.Value = 0;

progressBar1.Minimum = 0;

progressBar1.Maximum = websites.Count;

foreach (WebsiteInfo website in websites)

{

progressBar1.Value += 1;

//这个很怪,如果一旦 CommitChanges(),对象serverManager就是只读的了,不能再对其修改了

//只能多用几个 using (var serverManager = new ServerManager()) 来处理

using (var serverManager = new ServerManager())

{

var originalWebsite = serverManager.Sites[website.Name];

if (originalWebsite != null)

{

serverManager.Sites.Remove(originalWebsite);

serverManager.CommitChanges();

}

}

using (var serverManager = new ServerManager())

{

ApplicationPool originalAppPool = serverManager.ApplicationPools[website.Name];

if (originalAppPool != null)

{

serverManager.ApplicationPools.Remove(originalAppPool);

serverManager.CommitChanges();

}

}

var bdDefault = website.Bindings[0];

using (var serverManager = new ServerManager())

{

Site newWebsite = serverManager.Sites.Add(

website.Name,

bdDefault.Protocol,

$"*:{bdDefault.Port}:{bdDefault.Host}",

website.PhysicalPath);

for (int i = 1; i < website.Bindings.Count; i++)

{

var bd = website.Bindings[i];

newWebsite.Bindings.Add($"*:{bd.Port}:{bd.Host}", bd.Protocol);

}

serverManager.CommitChanges();

}

using (var serverManager = new ServerManager())

{

ApplicationPool newAppPool = serverManager.ApplicationPools.Add(website.Name);

newAppPool.ManagedRuntimeVersion = "v4.0"; // 设置.NET版本

newAppPool.ManagedPipelineMode = ManagedPipelineMode.Integrated;

serverManager.Sites[website.Name].ApplicationDefaults.ApplicationPoolName = newAppPool.Name;

serverManager.CommitChanges();

}

}

ThreadMessageBox.Show(this, "创建成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

catch (ThreadAbortException) { }

catch (Exception ex)

{

ThreadMessageBox.Show(this, ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

}

finally

{

foreach (Control control in this.Controls) control.Enabled = true;

}

}

文件名精灵2025年最新版 机器翻译 AI智能
...
批量修改文件名称、文件夹名称

免费使用 功能无限制

全面接入机器自动翻译功能,主流AI大模型智能改名

方便快捷,支持正则表达式、无需安装功能齐全、支持定制无广告、无插件,放心使用。

免费下载
本站中所有的计算器的计算结果仅供参考,本站对此结果的准确性不承担任何责任,实际数额以银行/保险公司/国家相关机构确认的结果为准。
在线客服QQ:543690914,备案号: 苏ICP备15037649号-27。东海县白塔埠镇佳诚电脑经营部版权所有。