Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.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# 要在命令提示符窗口中执行命令吗_C#_Command Prompt - Fatal编程技术网

C# 要在命令提示符窗口中执行命令吗

C# 要在命令提示符窗口中执行命令吗,c#,command-prompt,C#,Command Prompt,在我的控制台应用程序中,我需要执行一系列命令: D: cd d:\datafeeds grp -encrypt -myfile.xls 这组命令实际上使用工具(gpg)加密文件。 如何执行?Process.start允许您在shell中执行命令 请参阅此问题:您可以创建一个流程。要使用它,请在grp所在的文件夹中执行生成的.exe Process process1 = new Process(); process1.StartInfo.UseShellExecute = false; p

在我的控制台应用程序中,我需要执行一系列命令:

D:
cd d:\datafeeds
grp -encrypt -myfile.xls
这组命令实际上使用工具(gpg)加密文件。

如何执行?

Process.start允许您在shell中执行命令


请参阅此问题:

您可以创建一个流程。要使用它,请在grp所在的文件夹中执行生成的.exe

 Process process1 = new Process();
 process1.StartInfo.UseShellExecute = false;
 process1.StartInfo.RedirectStandardOutput = true;
 process1.StartInfo.FileName = "cmd.exe";
 process1.StartInfo.Arguments = "/C grp -encrypt -myfile.xls";

创建包含命令的批处理文件。
然后使用Process.Start和ProcessStartInfo类执行批处理

  ProcessStartInfo psi = new ProcessStartInfo(@"d:\datafeeds\yourbatch.cmd");
  psi.WindowStyle = ProcessWindowStyle.Minimized;
  psi.WorkingDirectory = @"d:\datafeeds";
  Process.Start(psi);
ProcessStartInfo包含其他有用的属性
Process和ProcessStartInfo需要使用System.Diagnostics的


在这种情况下(当您需要运行命令行工具时),我更喜欢使用批处理方法,而不是通过ProcessStartInfo属性对所有内容进行编码。当您必须更改某些内容而代码不可用时,它将更加灵活。

其他答案没有提到设置工作目录的功能。这样就无需进行目录更改操作,也无需将可执行文件存储在datafeeds目录中:

Process proc = new Process();
proc.StartInfo.WorkingDirectory = "D:\\datafeeds";
proc.StartInfo.FileName = "grp";
proc.StartInfo.Arguments = "-encrypt -myfile.xls";
proc.Start();

// Comment this out if you don't want to wait for the process to exit.
proc.WaitForExit();

是的,但对我来说,这不是一个单一的命令像复制等。这是一个三步的过程。然后你运行几个过程。结果将显示在目录中。或者检查这个问题:我想通过将“d:\datafeeds”作为当前文件夹来执行它,因为gpg将只查找当前文件夹中的文件。