运行 C# 中的命令
Running cmd in C#
有什么区别吗:
var startInfo = new ProcessStartInfo();
string path = Directory.GetCurrentDirectory() + @"\foldername";
startInfo.WorkingDirectory = path;
startInfo.FileName = path + @"\do_run.cmd";
startInfo.Arguments = "/c arg1 arg2";
Process.Start(startInfo);
和
var startInfo = new ProcessStartInfo();
string path = Directory.GetCurrentDirectory() + @"\foldername";
startInfo.FileName = @"C:\windows\system32\cmd.exe";
startInfo.Arguments = @"/c cd " + path + " && do_run arg1 arg2";
Process.Start(startInfo);
由于某种原因,第二个代码块可以正常工作,但第一个代码块不能。
其次,我在发布应用时无法使用我的C:
目录,那么我如何在Visual Studio项目文件夹中运行cmd.exe
?
谢谢
只需使用cmd.exe
:
var startInfo = new ProcessStartInfo();
string path = Directory.GetCurrentDirectory() + @"\foldername";
startInfo.FileName = "cmd.exe";
startInfo.Arguments = @"/c cd " + path + " && do_run arg1 arg2";
Process.Start(startInfo);
默认情况下,Windows 将在系统 PATH
变量中包含 System32(cmd.exe
所在的位置)(这意味着您可以 运行 cmd.exe
来自它会在任何地方找到它。
至于为什么你的第一个代码不起作用,我不是 100% 确定,但如果你 运行ning 在 .NET Core 上,你可能需要将 UseShellExecute
设置为true
因为与 .NET Framework 不同,它默认为 false
。也就是说,我认为以上是更好的选择。
像这样:
using System.IO;
using System.Reflection;
...
var startInfo = new ProcessStartInfo();
// We want a standard path (special folder) which is C:\windows\system32 in your case
// Path.Combine - let .Net make paths for you
startInfo.FileName = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.System),
"cmd.exe");
string path = Path.Combine(
// If you want exe path; change into
// Environment.CurrentDirectory if you want current directory
// if you want current directory
Path.GetDirectoryName(Assembly.GetEntryAssembly().Location),
@"foldername");
// ""{path}"" - be careful since path can contain space(s)
startInfo.Arguments = $@"/c cd ""{path}"" && do_run arg1 arg2";
// using : do not forget to Dispose (i.e. free unmanaged resources - HProcess, HThread)
using (Process.Start(startInfo)) {
...
}
有什么区别吗:
var startInfo = new ProcessStartInfo();
string path = Directory.GetCurrentDirectory() + @"\foldername";
startInfo.WorkingDirectory = path;
startInfo.FileName = path + @"\do_run.cmd";
startInfo.Arguments = "/c arg1 arg2";
Process.Start(startInfo);
和
var startInfo = new ProcessStartInfo();
string path = Directory.GetCurrentDirectory() + @"\foldername";
startInfo.FileName = @"C:\windows\system32\cmd.exe";
startInfo.Arguments = @"/c cd " + path + " && do_run arg1 arg2";
Process.Start(startInfo);
由于某种原因,第二个代码块可以正常工作,但第一个代码块不能。
其次,我在发布应用时无法使用我的C:
目录,那么我如何在Visual Studio项目文件夹中运行cmd.exe
?
谢谢
只需使用cmd.exe
:
var startInfo = new ProcessStartInfo();
string path = Directory.GetCurrentDirectory() + @"\foldername";
startInfo.FileName = "cmd.exe";
startInfo.Arguments = @"/c cd " + path + " && do_run arg1 arg2";
Process.Start(startInfo);
默认情况下,Windows 将在系统 PATH
变量中包含 System32(cmd.exe
所在的位置)(这意味着您可以 运行 cmd.exe
来自它会在任何地方找到它。
至于为什么你的第一个代码不起作用,我不是 100% 确定,但如果你 运行ning 在 .NET Core 上,你可能需要将 UseShellExecute
设置为true
因为与 .NET Framework 不同,它默认为 false
。也就是说,我认为以上是更好的选择。
像这样:
using System.IO;
using System.Reflection;
...
var startInfo = new ProcessStartInfo();
// We want a standard path (special folder) which is C:\windows\system32 in your case
// Path.Combine - let .Net make paths for you
startInfo.FileName = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.System),
"cmd.exe");
string path = Path.Combine(
// If you want exe path; change into
// Environment.CurrentDirectory if you want current directory
// if you want current directory
Path.GetDirectoryName(Assembly.GetEntryAssembly().Location),
@"foldername");
// ""{path}"" - be careful since path can contain space(s)
startInfo.Arguments = $@"/c cd ""{path}"" && do_run arg1 arg2";
// using : do not forget to Dispose (i.e. free unmanaged resources - HProcess, HThread)
using (Process.Start(startInfo)) {
...
}