简体   繁体   English

如何从代码运行命令行命令

[英]How do I run Command line commands from code

I need to do 2 things: run a batch file (works fine), and run a command (does not work). 我需要做两件事:运行批处理文件(工作正常),并运行命令(不起作用)。 The method for the command throws the exception 'file not found'. 该命令的方法抛出异常'file not found'。 If I open a cmd window, and type the command, it works perfectly. 如果我打开一个cmd窗口,并键入命令,它可以正常工作。

  private static void Rescan()
    {
        //System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("DEVCON ReScan");
        //psi.RedirectStandardOutput = true;
        //psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        //psi.UseShellExecute = false;
        //System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);
        System.Diagnostics.Process proc = new System.Diagnostics.Process();
        proc.StartInfo.FileName = "DEVCON ReScan";
        proc.StartInfo.RedirectStandardError = true;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.StartInfo.UseShellExecute = false;
        proc.Start();
        proc.WaitForExit();
        System.IO.StreamReader myOutput = proc.StandardOutput;
        proc.WaitForExit(4000);
        if (proc.HasExited)
        {
            string output = myOutput.ReadToEnd();
            FileIO.WriteLog(_writePath, output);
        }

    }

The commented code also throws the same exception. 注释的代码也会抛出相同的异常。

DEVCON ReScan is really the name of the executable? DEVCON ReScan真的是可执行文件的名称吗? I guess the executable is DEVCON, while ReScan is a parameter. 我猜可执行文件是DEVCON,而ReScan是一个参数。 This means you have to set StartInfo.FileName to "DEVCON" and StartInfo.Arguments to "ReScan". 这意味着您必须将StartInfo.FileName设置为“DEVCON”并将StartInfo.Arguments设置为“ReScan”。

Is the DEVCON application actually in the working directory ? DEVCON应用程序实际上是在工作目录中吗? Otherwise it won't work unless you specify the full path to it. 否则,除非您指定它的完整路径,否则它将无法工作。

Furthermore you have to specify the extension, so I suppose you'd go for "Devcon.exe", and specify the parameters not in the filename but in the parameters :) 此外,你必须指定扩展名,所以我想你会选择“Devcon.exe”,并指定不在文件名中但参数中的参数:)

Try this: 尝试这个:

        ProcessStartInfo psi = new ProcessStartInfo();            
        psi.FileName = Environment.GetEnvironmentVariable("comspec");
        psi.CreateNoWindow = true;
        psi.RedirectStandardError = true;
        psi.RedirectStandardInput = true;
        psi.RedirectStandardOutput = true;
        psi.UseShellExecute = false;

        Process p = Process.Start(psi);

        ConsoleColor fc = Console.ForegroundColor;

        StreamWriter sw = p.StandardInput;
        StreamReader sr = p.StandardOutput;

        char[] buffer = new char[1024];
        int l = 0;

        sw.Write("DEVCON ReScan");
        sw.Write(sw.NewLine);

        Console.Write(">> ");

        l = sr.Read(buffer, 0, buffer.Length);

        for (int n = 0; n < l; n++)
            Console.Write(buffer[n] + " ");

        p.Close();

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

相关问题 如何编码我的 Nunit 测试以从命令行运行? - How do I code my Nunit tests to run from command line? 如何从 web 应用程序运行命令行进程? - How do I run a command line process from a web application? 我可以从命令行运行.NET程序集中的代码吗? - Can I run code from a .NET assembly from a command line? 如何从命令行从nunit-console运行nunit 2测试? - How do I run nunit 2 tests from nunit-console from the command line? 如何使用NUnit 2.6从命令行运行代码覆盖 - How to run code-coverage from the command line with NUnit 2.6 如何在WPF应用程序中从命令行运行Python脚本 - How do I run a Python script from command line in a WPF application 如何通过 C# 在命令行中运行 WireShark 命令 - How to Run WireShark Commands in command line through C# 如何从Mono的命令行运行SpecFlow? - How can I run SpecFlow from the command line on Mono? 从命令行运行并行命令时,如何区分它们的输出? - When running parallel commands from the command line, how can I tell their outputs apart? 如何在命令行中运行通常在Visual Studio测试资源管理器中运行的.cs测试? - How do I run .cs tests that I normally run in the Visual Studio Test Explorer, in the command line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM