繁体   English   中英

如何使用C#更改命令提示符中的目录位置?

[英]how to change the directory location in command prompt using C#?

我通过以下代码使用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();

但是,我也通过命令来更改目录。

问题:

  1. 如何更改目录位置?
  2. Cmd提示将在打开后始终显示...

请指导我摆脱这些问题......

要更改启动目录,可以通过将p.StartInfo.WorkingDirectory设置为您感兴趣的目录来更改它。目录未更改的原因是参数/cd:\\test 而是尝试/c cd d:\\test

 Process p = new Process();
 p.StartInfo.FileName = "cmd.exe";
 p.StartInfo.WorkingDirectory = @"C:\";
 p.StartInfo.UseShellExecute = false;
 ...
 p.Start();

您可以通过将p.StartInfo.WindowStyle设置为Hidden来隐藏命令提示符,以避免显示该窗口。

 p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.windowstyle.aspx

您可以使用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:");

请改用System.IO.Directory.SetCurrentDirectory

你也可以检查一下

这篇文章

processStartInfo .WorkingDirectory = @"c:\";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM