请教大家个问题,就是执行cmd命令,返回值如何不要Microsoft等这些字符呀?比如命令tasklist,然后只返回进程
谢谢了.

解决方案 »

  1.   

    用正则表达式,或者SubString()函数过滤。
      

  2.   


    没有直接的方法了吗?我记得好像以前用了个什么方法可以了的..比如什么先运行下cls什么的
      

  3.   

    那就使用管道比如tasklist > 1.txt
      

  4.   

    或者你不要执行cmd命令,直接执行tasklist。
      

  5.   

    public static string cmd(string command)
            {
                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;
                p.Start();
                p.StandardInput.AutoFlush = true;
                p.StandardInput.WriteLine("@echo off");
                p.StandardInput.WriteLine("cls");
                p.StandardInput.WriteLine(command);
                p.StandardInput.WriteLine("exit");
                string strRst = p.StandardOutput.ReadToEnd();
                p.WaitForExit();
                p.Close();
                return strRst;
            }这样子下来的不是应该没有Microsoft那些的么
      

  6.   

    其实你根本没有必要调用 cmd
      

  7.   


    因为tasklist只是个列子嘛....说不定用户会写ping,net start什么的命令呀.
      

  8.   

    对啊。无论什么命令,都直接执行好了,为什么要cmd去转发呢?
      

  9.   

            public static string cmd(string command)
            {
                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;
                p.Start();
                p.StandardInput.AutoFlush = true;
                p.StandardInput.WriteLine("@echo off");
                //p.StandardInput.WriteLine("cls");
                p.StandardInput.WriteLine(command);
                p.StandardInput.WriteLine("exit");
                string strRst = p.StandardOutput.ReadToEnd();
                strRst = strRst.Substring(strRst.IndexOf(command) + command.Length);
                strRst = strRst.Substring(0,strRst.Length - 8);
                p.WaitForExit();
                p.Close();
                return strRst;
            }这样就可以了...呵呵..还是得做字符的处理.