C# 批量新建网站 IIS服务器, 循环创建多个网站
private void toolStripButton1_Click(object sender, EventArgs e)
{
// 循环创建多个网站
string[] lines = File.ReadAllLines(Path.Combine(Application.StartupPath, "网站名称.txt"));
foreach (string line in lines)
{
if (string.IsNullOrEmpty(line) || line.Trim() == "") continue;
string name = line.Trim();
string siteName = "5ADanCi_" + name;
string sitePath = "C:\\5ADanCi_Batch\\" + name;
if (!Directory.Exists(sitePath)) { Directory.CreateDirectory(sitePath); }
string appPoolName = siteName + "_Pool";
// 使用 appcmd 命令创建网站
string command = $"add site /name:{siteName} /bindings:http://{name}.5adanci.com:80 /physicalPath:{sitePath}";
ExecuteAppCmd(command);
command = $"add apppool /name:{appPoolName} /managedRuntimeVersion:\"v4.0\" /managedPipelineMode:\"Integrated\"";
ExecuteAppCmd(command);
// 设置网站的应用程序池
command = $"set app /app.name:{siteName}/ /applicationPool:{appPoolName}";
ExecuteAppCmd(command);
}
MessageBox.Show("OK");
}
// 执行 appcmd 命令的方法
private void ExecuteAppCmd(string command)
{
textBox1.Text += command + "\r\n";
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "C:\\Windows\\System32\\inetsrv\\appcmd.exe",
Arguments = command,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
}