如何运行命令行从code命令 [英] How do I run Command line commands from code

查看:482
本文介绍了如何运行命令行从code命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做两件事:运行一个批处理文件(正常工作),并运行一个命令(不工作)。 该方法的指令引发异常找不到文件。如果我打开一个CMD窗口,并键入命令,它完美的作品。

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'. If I open a cmd window, and type the command, it works perfectly.

  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);
        }

    }

本评论code也抛出了同样的异常。

The commented code also throws the same exception.

推荐答案

DEVCON重新扫描是真正的可执行文件的名称?我猜的可执行文件是DEVCON,而重新扫描参数。这意味着你必须设置StartInfo.FileName为DEVCON和StartInfo.Arguments以重新扫描。

DEVCON ReScan is really the name of the executable? I guess the executable is DEVCON, while ReScan is a parameter. This means you have to set StartInfo.FileName to "DEVCON" and StartInfo.Arguments to "ReScan".

这篇关于如何运行命令行从code命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆