如何使用 C# 在命令提示符中更改目录位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10222004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to change the directory location in command prompt using C#?
提问by Saravanan
I have successfully opened command prompt window using C# through the following code.
我已经通过以下代码使用 C# 成功打开了命令提示符窗口。
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WorkingDirectory = @"d:\pdf2xml";
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.Start();
p.StandardInput.WriteLine(@"pdftoxml.win32.1.2.7 -annotation "+filename);
p.StandardInput.WriteLine(@"cd D:\python-source\ds-xmlStudio-1.0-py27");
p.StandardInput.WriteLine(@"main.py -i example-8.xml -o outp.xml");
p.WaitForExit();
But, i have also passed command to change the directory.
但是,我也通过了命令来更改目录。
problems:
问题:
- how to change the directory location?
- Cmd prompt will be shown always after opened...
- 如何更改目录位置?
- Cmd 提示将在打开后始终显示...
Please guide me to get out of those issue...
请指导我摆脱这些问题......
采纳答案by Waqar
You can use p.StandardInput.WriteLine to send commands to cmd window. For this just set the p.StartInfo.RedirectStandardOutput to ture. like below
您可以使用 p.StandardInput.WriteLine 将命令发送到 cmd 窗口。为此,只需将 p.StartInfo.RedirectStandardOutput 设置为 ture。像下面
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
//p.StartInfo.Arguments = @"/c D:\pdf2xml";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.Start();
p.StandardInput.WriteLine(@"cd D:\pdf2xml");
p.StandardInput.WriteLine("d:");
回答by Rami Shareef
回答by David Z.
To change the startup directory, you can change it by setting p.StartInfo.WorkingDirectory to the directory that you are interested in. The reason that your directory is not changing is because the argument /c d:\test. Instead try /c cd d:\test
要更改启动目录,您可以通过将 p.StartInfo.WorkingDirectory 设置为您感兴趣的目录来更改它。您的目录没有更改的原因是因为参数/c d:\test. 而是尝试/c cd d:\test
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WorkingDirectory = @"C:\";
p.StartInfo.UseShellExecute = false;
...
p.Start();
You can hide the command prompt by setting p.StartInfo.WindowStyle to Hidden to avoid showing that window.
您可以通过将 p.StartInfo.WindowStyle 设置为 Hidden 来隐藏命令提示符,以避免显示该窗口。
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.windowstyle.aspx
http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.windowstyle.aspx

