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

qq_21230597 2014-11-05 05:40:05

现在需要做一个命令调用,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);
}
...全文
7189 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
害羞的大叔 2014-11-05
  • 打赏
  • 举报
回复 3
p.StartInfo.WorkingDirectory = Application.StartupPath;
phommy 2014-11-05
  • 打赏
  • 举报
回复
引用 2 楼 gxingmin 的回复:
加一句 p.startinfo.arguments="cd " + Application.StartPath;
不安全,cd命令只"change directory",不change driver
  • 打赏
  • 举报
回复
引用 8 楼 qq_21230597 的回复:
[quote=引用 6 楼 xomix 的回复:]

p.StartInfo.FileName = "C:\\Windows\\System32\\cmd.exe /k cd /d \"D:\\ServKit\"";//后面的 \"D:\\ServKit\"替换为你需要的路径即可。
路径需要动态获取,不是写死的.[/quote]

p.StartInfo.FileName = "C:\\Windows\\System32\\cmd.exe /k cd /d"+" \"D:\\ServKit\"";//后面的 \"D:\\ServKit\"替换为你需要的路径即可。
能看明白吗?
  • 打赏
  • 举报
回复
引用 8 楼 qq_21230597 的回复:
[quote=引用 6 楼 xomix 的回复:]

p.StartInfo.FileName = "C:\\Windows\\System32\\cmd.exe /k cd /d \"D:\\ServKit\"";//后面的 \"D:\\ServKit\"替换为你需要的路径即可。
路径需要动态获取,不是写死的.[/quote] 动态获取你就动态拼接字符串啊!获取当前运行路径是另外的问题了吧?
  • 打赏
  • 举报
回复
当你使用 FullName 来执行一个不在当前目录下的程序时,你的“当前工作目录”并不一定在那里。那么如果程序要在当前工作目录找“有些数据文件”,则这种方法就是错误的!你应该这真正要设置的不是 FileName 属性,而是设置好当前工作目录属性。
qq_21230597 2014-11-05
  • 打赏
  • 举报
回复
引用 6 楼 xomix 的回复:

p.StartInfo.FileName = "C:\\Windows\\System32\\cmd.exe /k cd /d \"D:\\ServKit\"";//后面的 \"D:\\ServKit\"替换为你需要的路径即可。
路径需要动态获取,不是写死的.
  • 打赏
  • 举报
回复 1
你到底是想让 p.StartInfo.FileName 是执行文件的 FullName 呢?还是想让当前工作目录在其它路径呢?这是两个不同的概念。
  • 打赏
  • 举报
回复

p.StartInfo.FileName = "C:\\Windows\\System32\\cmd.exe /k cd /d \"D:\\ServKit\"";//后面的 \"D:\\ServKit\"替换为你需要的路径即可。
  • 打赏
  • 举报
回复
引用 4 楼 qq_21230597 的回复:
[quote=引用 3 楼 gxingmin 的回复:] [quote=引用 2 楼 gxingmin 的回复:] 加一句 p.startinfo.arguments="cd " + Application.StartPath;
前提是当前程序路径是短路径,如果是长路径,要用系统api函数转成短路径[/quote] 试了不行。 怎么才算是长路径?[/quote] 你用我那个方法,调用cmd的时候就调用就可以了,纠结长短路径。
qq_21230597 2014-11-05
  • 打赏
  • 举报
回复
引用 3 楼 gxingmin 的回复:
[quote=引用 2 楼 gxingmin 的回复:] 加一句 p.startinfo.arguments="cd " + Application.StartPath;
前提是当前程序路径是短路径,如果是长路径,要用系统api函数转成短路径[/quote] 试了不行。 怎么才算是长路径?
gxingmin 2014-11-05
  • 打赏
  • 举报
回复
引用 2 楼 gxingmin 的回复:
加一句 p.startinfo.arguments="cd " + Application.StartPath;
前提是当前程序路径是短路径,如果是长路径,要用系统api函数转成短路径
gxingmin 2014-11-05
  • 打赏
  • 举报
回复
加一句 p.startinfo.arguments="cd " + Application.StartPath;
  • 打赏
  • 举报
回复
你这实际上是cmd应用的问题。 我就发一行命令你看:

C:\Windows\System32\cmd.exe /k cd /d "D:\ServKit"

111,082

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧