本质上,我的程序正在远程处理服务器,在IIS中停止应用程序池,复制文件,最后重新启动应用程序池。我必须将相同的过程应用于六个不同的服务器。如何将其整合为最简单,最有效的代码?
现在是我的代码(基本上,它已经完成了6次相同的操作:]
using (ServerManager mgr = ServerManager.OpenRemote("RemoteServer1"))
{
ApplicationPool appPool = mgr.ApplicationPools["TestAppPool"];
if (appPool != null)
{
if (appPool.State == ObjectState.Started)
{
appPool.Stop();
}
}
File.Copy(@"C:\Users\TestUser\Downloads\testfile.txt", @"\\RemoteServer1\Users\TestUser\Documents\testfile.txt");
appPool.Start;
}
using (ServerManager mgr = ServerManager.OpenRemote("RemoteServer2"))
{
ApplicationPool appPool = mgr.ApplicationPools["TestAppPool"];
if (appPool != null)
{
if (appPool.State == ObjectState.Started)
{
appPool.Stop();
}
}
File.Copy(@"C:\Users\TestUser\Downloads\testfile.txt", @"\\RemoteServer2\Users\TestUser\Documents\testfile.txt");
appPool.Start;
}
using (ServerManager mgr = ServerManager.OpenRemote("RemoteServer3"))
{
ApplicationPool appPool = mgr.ApplicationPools["TestAppPool"];
if (appPool != null)
{
if (appPool.State == ObjectState.Started)
{
appPool.Stop();
}
}
File.Copy(@"C:\Users\TestUser\Downloads\testfile.txt", @"\\RemoteServer3\Users\TestUser\Documents\testfile.txt");
appPool.Start;
}
using (ServerManager mgr = ServerManager.OpenRemote("RemoteServer4"))
{
ApplicationPool appPool = mgr.ApplicationPools["TestAppPool"];
if (appPool != null)
{
if (appPool.State == ObjectState.Started)
{
appPool.Stop();
}
}
File.Copy(@"C:\Users\TestUser\Downloads\testfile.txt", @"\\RemoteServer4\Users\TestUser\Documents\testfile.txt");
appPool.Start;
}
using (ServerManager mgr = ServerManager.OpenRemote("RemoteServer5"))
{
ApplicationPool appPool = mgr.ApplicationPools["TestAppPool"];
if (appPool != null)
{
if (appPool.State == ObjectState.Started)
{
appPool.Stop();
}
}
File.Copy(@"C:\Users\TestUser\Downloads\testfile.txt", @"\\RemoteServer5\Users\TestUser\Documents\testfile.txt");
appPool.Start;
}
using (ServerManager mgr = ServerManager.OpenRemote("RemoteServer6"))
{
ApplicationPool appPool = mgr.ApplicationPools["TestAppPool"];
if (appPool != null)
{
if (appPool.State == ObjectState.Started)
{
appPool.Stop();
}
}
File.Copy(@"C:\Users\TestUser\Downloads\testfile.txt", @"\\RemoteServer6\Users\TestUser\Documents\testfile.txt");
appPool.Start;
}
将所有内容放入以服务器名称为参数的方法中
public void CopyFileToServer(string targetServerName)
{
using (ServerManager mgr = ServerManager.OpenRemote(targetServerName))
{
ApplicationPool appPool = mgr.ApplicationPools["TestAppPool"];
if (appPool != null)
{
if (appPool.State == ObjectState.Started)
{
appPool.Stop();
}
}
// Depending on the Framework or Core version you might need to play
// with the string interpolation for the second parameter
File.Copy(@"C:\Users\TestUser\Downloads\testfile.txt", @$"\\{targetServerName}\Users\TestUser\Documents\testfile.txt");
appPool.Start;
}
}
将这些更改之一提取为方法,将更改替换为参数:
public void CopyToRemote(string remoteServer)
{
using (ServerManager mgr = ServerManager.OpenRemote(remoteServer))
{
ApplicationPool appPool = mgr.ApplicationPools["TestAppPool"];
if (appPool != null)
{
if (appPool.State == ObjectState.Started)
{
appPool.Stop();
}
}
File.Copy(@"C:\Users\TestUser\Downloads\testfile.txt", string.Format(@"\\{0}\Users\TestUser\Documents\testfile.txt", remoteServer));
appPool.Start;
}
}
呼叫方式:
for (int index = 1; index <= 6; index++)
{
CopyToRemote($"RemoteServer{index}");
}