简体   繁体   English

如何使用System.Diagnostics.Process运行命令?

[英]How to run a command using System.Diagnostics.Process?

What is wrong with this code to run a command in command prompt? 在命令提示符下运行命令的这段代码有什么问题? I try to run this code and it does not give any error and it does not do what it is supposed to do. 我尝试运行此代码,它没有给出任何错误,并且没有执行应有的功能。 It works fine if I copy the command to command prompt and run it manually? 如果我将命令复制到命令提示符并手动运行,它会很好地工作吗?

Thank you! 谢谢!

[TestMethod]
public void TestProcess()
{
    string command1 = @"sejda-console simplesplit --files -f C:\TestFiles\test.pdf -o C:\TestFiles\split1\ -s all";

    ProcessStartInfo processInfo;
    Process process;

    //I have the batch file sejda-console in C:\sejda-console-3.2.83\bin so I concatenated the directory of the batch file with the actual command.

    processInfo = new ProcessStartInfo("cmd.exe", @"C:\sejda-console-3.2.83\bin " + command1);  
    processInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    process = Process.Start(processInfo);

    process.WaitForExit();

    process.Close();
}

Alternatively, I am trying this one too which does not work either. 或者,我也尝试这种方法,也不起作用。

[TestMethod]
public void TestProcess3()
{
    string MyBatchFile = @"C:\sejda-console-3.2.83\bin\sejda-console.bat";

    string _sourcePath = @"C:\TestFiles\test.pdf";
    string _targetPath = @"C:\TestFiles\split1\";

    var process = new Process
    {
        StartInfo = {
                        Arguments = String.Format("/C simplesplit --files -f {0} -o {1} -s all", _sourcePath, _targetPath)
                    }
    };

    process.StartInfo.FileName = MyBatchFile;
    bool b = process.Start();
}
  1. You are missing /C to send arguments to cmd.exe 您缺少/ C来向cmd.exe发送参数
  2. Add backslash after \\bin\\ 在\\ bin \\之后添加反斜杠
  3. Wrap your command line arguments with quotes. 用引号将命令行参数引起来。

So your code should look like: 因此,您的代码应如下所示:

[TestMethod]
public void TestProcess()
{

   string command1 = @"sejda-console simplesplit --files -f C:\TestFiles\test.pdf -o C:\TestFiles\split1\ -s all";

    ProcessStartInfo processInfo;
    Process process;

    //I have the batch file sejda-console in C:\sejda-console-3.2.83\bin so I concatenated the directory of the batch file with the actual command.

    processInfo = new ProcessStartInfo("cmd.exe", @"/C \"C:\sejda-console-3.2.83\bin\" + command1 + "\"");  
    processInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    process = Process.Start(processInfo);

    process.WaitForExit();

    process.Close();
}

Try this processInfo : 试试这个processInfo

var batch = "sejda-console.bat";
var sourcePath = @"C:\TestFiles\test.pdf";
var targetPath = @"C:\TestFiles\split1\";

var processInfo = new ProcessStartInfo();
processInfo.WorkingDirectory = @"C:\sejda-console-3.2.83\bin";
processInfo.FileName = "cmd.exe";
processInfo.Arguments = $"/C {batch} simplesplit --files -f \"{sourcePath}\" -o \"{targetPath}\" -s all";
// todo set windows style etc

Also have a look at Executing Batch File in C# for error handling. 另请参阅在C#执行批处理文件以进行错误处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM