【问题标题】:Redirect result to other process on command line将结果重定向到命令行上的其他进程
【发布时间】:2015-09-02 10:06:06
【问题描述】:

我正在尝试通过获取以“abc”开头的池列表来更改池设置,然后使用 System.Diagnostics.Process.Start 更改参数。在这种情况下,将其更改为 32 位。

class Program
{
    static void Main(string[] args)
    {

        Process.Start(new ProcessStartInfo
        {
            Arguments = "list apppool /name:$=\"*abc*\" /xml | c:\\Windows\\System32\\inetsrv\\appcmd set apppool /in /enable32BitAppOnWin64:true",
            FileName = "appcmd.exe",
            WorkingDirectory = Environment.GetEnvironmentVariable("SystemRoot") + @"\system32\inetsrv\",
            WindowStyle = ProcessWindowStyle.Hidden
        });

    }
}

我遇到的问题是参数内部的管道。我不太确定这是否被允许以及在语法上应该是什么样子。任何帮助将不胜感激。

【问题讨论】:

    标签: c# system.diagnostics


    【解决方案1】:

    如果将第一个命令的输出读入当前进程,然后将其写入第二个命令,可能会更容易。像这样:

    Process first = new Process();
    first.StartInfo.UseShellExecute = false;
    first.StartInfo.RedirectStandardOutput = true;
    first.StartInfo.FileName = "c:\\Windows\\System32\\inetsrv\\appcmd";
    first.StartInfo.WorkingDirectory = Environment.GetEnvironmentVariable("SystemRoot") + @"\system32\inetsrv\";
    first.StartInfo.Arguments = "list apppool /name:$=\"*abc*\" /xml";
    first.Start();
    string output = first.StandardOutput.ReadToEnd();
    first.WaitForExit();
    
    // now put into the second
    Process second = new Process();
    second.StartInfo.FileName = "c:\\Windows\\System32\\inetsrv\\appcmd.exe";
    second.StartInfo.WorkingDirectory = Environment.GetEnvironmentVariable("SystemRoot") + @"\system32\inetsrv\";
    second.StartInfo.Arguments = "set apppool /in /enable32BitAppOnWin64:true";
    second.StartInfo.UseShellExecute = false;
    second.StartInfo.RedirectStandardInput = true;
    second.Start();
    second.StandardInput.Write(output);
    second.StandardInput.Close();
    second.WaitForExit();
    

    【讨论】:

      猜你喜欢
      • 2010-10-14
      • 2012-08-03
      • 2010-11-14
      • 1970-01-01
      • 1970-01-01
      • 2019-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多