Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Windows Process备份IIS设置_C#_Windows_Iis - Fatal编程技术网

C# 使用Windows Process备份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 当我在命令提示符下运行这些命

我可以使用以下命令备份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
当我在命令提示符下运行这些命令时,效果很好。创建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
的参数

您有两个选择:

  • 使用它的方法如下,相当于在CMD中执行命令:

    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中键入的命令相同

  • 使用appcmd.exe提供的另一个选项备份IIS,避免将输出重定向到文件。命令是appcmd.exe add backup%backupname%。此命令添加IIS配置的备份,以后您可以通过
    restore backup$backupname%
    进行恢复


  • 您使用的帐户可能不正确。IIS中的每个站点都在应用程序池中运行。每个应用程序池都有一个用户。这些可能是您需要的凭证。请检查这个。@Christos。上述代码在windows本身中运行。不在网站的应用程序池中。