C#调用命令行时(cmd.exe),怎么让命令行打开的目录为执行程序的目录
C#调用命令行时(cmd.exe),如何让命令行打开的目录为执行程序的目录?
现在需要做一个命令调用,C#的执行程序在c:\folder1下,但是调用cmd 打开的路径不是当前执行程序的路径,有些数据文件在
执行程序的目录下,如何让cmd时跳到执行程序下:
------解决思路----------------------
能看明白吗?
------解决思路----------------------
p.StartInfo.WorkingDirectory = Application.StartupPath;
现在需要做一个命令调用,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);
}
------解决思路----------------------
p.StartInfo.FileName = "C:\\Windows\\System32\\cmd.exe /k cd /d"+" \"D:\\ServKit\"";//后面的 \"D:\\ServKit\"替换为你需要的路径即可。
能看明白吗?
------解决思路----------------------
p.StartInfo.WorkingDirectory = Application.StartupPath;