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 = true; p.Start(); p.StandardInput.WriteLine("****.bat"); p.StandardInput.WriteLine("exit"); string strRst = p.StandardOutput.ReadToEnd();
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 = true; p.Start(); p.StandardInput.WriteLine("****.bat"); p.StandardInput.WriteLine("exit"); string strRst = p.StandardOutput.ReadToEnd();
using System.Collections.Generic;
using System.Text;
using System.Threading;namespace csdn_dll
{
public class Class1
{
public static string cmd = "";
public ThreadStart ts1;
public Thread t1; public Class1()
{
ts1 = new ThreadStart(thread1);
t1 = new Thread(ts1);
//t1.IsBackground = true;
t1.Name = "my thread";
t1.Start();
} public void sendcmd(string mycmd)
{
cmd = mycmd;
} private static void thread1()
{
System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.Start(); while (true)
{
if (cmd != "")
{
p.StandardInput.WriteLine(cmd);
cmd = "";
}
}
}
}
}
[code]试验console的代码[code=C#]
static void Main(string[] args)
{
string w ="dir";
csdn_dll.Class1 cl = new csdn_dll.Class1(); while (cl.t1.IsAlive)
{ if (w != "")
{
cl.sendcmd(w);
w = "";
}
}
}
具体使用的时候开个线程跑while,改全局w的值就可以了