C# 使用Windows Process备份IIS设置
我可以使用以下命令备份IIS的设置C# 使用Windows Process备份IIS设置,c#,windows,iis,C#,Windows,Iis,我可以使用以下命令备份IIS的设置 //sites C:\Windows\system32\inetsrv\appcmd.exe list site /config /xml > C:\Temp\iis_config_sites.xml //apppools C:\Windows\system32\inetsrv\appcmd.exe list apppool /config /xml > C:\Temp\iis_config_apppool.xml 当我在命令提示符下运行这些命
//sites
C:\Windows\system32\inetsrv\appcmd.exe list site /config /xml > C:\Temp\iis_config_sites.xml
//apppools
C:\Windows\system32\inetsrv\appcmd.exe list apppool /config /xml > C:\Temp\iis_config_apppool.xml
当我在命令提示符下运行这些命令时,效果很好。创建XML文件。但是我想通过使用System.Diagnostics.process
从C#code执行这个命令来自动化这个过程。为此,我使用以下代码
using (Process process = new Process())
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = @"C:\Windows\system32\inetsrv\appcmd.exe";
startInfo.Arguments = @"list site /config /xml > C:\Temp\iis_config_sites.xml";
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
}
但是当我运行这段代码时,什么都没有发生。未创建XML文件。但是没有错误,也没有警告。执行此命令的程序在Windows Server 2019和IIS 10的管理员帐户下运行。我还尝试添加了WorkingDirectory
,但也没有效果
我使用Process
使用WinRar生成一个压缩文件夹,这也很好
因此,如果有人知道什么可能是问题,那就太好了。问题在于,您在参数中使用了'>',这是一个CMD命令,指示它将命令的输出(
appcmd.exe
,在这种情况下)写入指定的文件路径(C:\Temp\iis\u config\u sites.xml
)。当然,它不能作为appcmd.exe
的参数
您有两个选择:
startInfo.FileName = @"C:\Windows\system32\cmd.exe";
startInfo.Arguments = "/c \"C:\\Windows\\system32\\inetsrv\\appcmd.exe\" list site /config /xml > C:\\Temp\\iis_config_sites.xml";
请注意,/c
是cmd.exe的一个命令行选项,用于将其余参数作为“命令”执行,就像将其输入命令行窗口一样。其余的命令与您通常在cmd中键入的命令相同restore backup$backupname%
进行恢复您使用的帐户可能不正确。IIS中的每个站点都在应用程序池中运行。每个应用程序池都有一个用户。这些可能是您需要的凭证。请检查这个。@Christos。上述代码在windows本身中运行。不在网站的应用程序池中。