C#调用命令行时(cmd.exe),如何让命令行打开的目录为执行程序的目录?

qq_21230597 qq_21230597 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

现在需要做一个命令调用,C#的执行程序在c:\folder1下,但是调用cmd 打开的路径不是当前执行程序的路径,有些数据文件在
执行程序的目录下,如何让cmd时跳到执行程序下:

 string command = "dir";
Process p = new Process();
p.StartInfo.FileName = "cmd.exe ";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = false;
// string strOutput = null;
try
{
p.Start();
Console.WriteLine("command:" + command);
p.StandardInput.WriteLine(command + "&exit");
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
Console.WriteLine("result:" + output);

}
catch (Exception e1)
{
Console.WriteLine("error" + e1.Message);
}
...全文

相关推荐

评论 13
收藏 已收藏
分享
返回顶部
qq_21230597
C# 111,129 成员 642,538 内容