请教大家个问题,就是执行cmd命令,返回值如何不要Microsoft等这些字符呀?比如命令tasklist,然后只返回进程 请教大家个问题,就是执行cmd命令,返回值如何不要Microsoft等这些字符呀?比如命令tasklist,然后只返回进程谢谢了. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用正则表达式,或者SubString()函数过滤。 没有直接的方法了吗?我记得好像以前用了个什么方法可以了的..比如什么先运行下cls什么的 那就使用管道比如tasklist > 1.txt 或者你不要执行cmd命令,直接执行tasklist。 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那些的么 其实你根本没有必要调用 cmd 因为tasklist只是个列子嘛....说不定用户会写ping,net start什么的命令呀. 对啊。无论什么命令,都直接执行好了,为什么要cmd去转发呢? 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; }这样就可以了...呵呵..还是得做字符的处理. C#鼠标单击事件代码问题 无法连接到数据库 一个小问题,困扰我几天了。请帮忙解决 请教,号对于编译的结构有任何影响么? 數據上傳問題 【100分】关于asp.net页面优化的问题? W高手来看看:MI编程实现用户名,用户组的获取 如何建立公用对象 又是浏览器,好急,急得花儿也谢了! 一年没接触.net了,怎样从2个txt文档随机取出两行组成一句话? 怎么分析数据包,并分析数据包内的内容 新年快快快快快快快快快快快快快快快乐 请教关于C#中字符串去重的处理
没有直接的方法了吗?我记得好像以前用了个什么方法可以了的..比如什么先运行下cls什么的
{
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那些的么
因为tasklist只是个列子嘛....说不定用户会写ping,net start什么的命令呀.
{
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;
}这样就可以了...呵呵..还是得做字符的处理.