执行命令在我的控制台应用程序,我需要执行一系列命令:要在命令提示符窗口
D:
cd d:\datafeeds
grp -encrypt -myfile.xls
这组命令的使用工具(GPG)实际上加密文件。
我该怎么做?
执行命令在我的控制台应用程序,我需要执行一系列命令:要在命令提示符窗口
D:
cd d:\datafeeds
grp -encrypt -myfile.xls
这组命令的使用工具(GPG)实际上加密文件。
我该怎么做?
创建一个包含您的命令的批处理文件。
然后使用Process.Start和ProcessStartInfo类来执行批处理。
ProcessStartInfo psi = new ProcessStartInfo(@"d:\datafeeds\yourbatch.cmd");
psi.WindowStyle = ProcessWindowStyle.Minimized;
psi.WorkingDirectory = @"d:\datafeeds";
Process.Start(psi);
的ProcessStartInfo包含其他有用的属性See MSDN docs
过程和的ProcessStartInfo需要using System.Diagnostics;
在这种情况下(当你需要运行命令行工具),我更喜欢使用,而不是通过编码一切分批进场ProcessStartInfo属性。当你必须改变某些东西而你没有可用的代码时,它会更加灵活。
Process.start允许您在shell中执行命令。
看到了这个问题:ShellExecute equivalent in .NET
你可以创建一个过程。要使用它,请在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";
我想执行它通过使“d:\ datafeeds”作为当前文件夹,因为gpg只会在当前文件夹中查找文件。 –
其他答案还没有提到能够设置WorkingDirectory。这消除了对目录更改操作,以及存储可执行的数据传送专线目录的需要,需要:
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();
肯定的,但对我来说,它不是一个简单的命令像拷贝等它需要3个步骤。 –
然后你运行几个进程。结果将在目录中。或检查这个问题:http://stackoverflow.com/questions/437419/execute-multiple-command-lines-with-the-same-process-using-net –