解决方案 »
- Linq使用问题
- 编译不可通过?short s1 = 1;short s2 = 2;short s3 = s1 + s2; why??
- 如何批量插入数据
- 如何在服务器控件table中的TableRow 设定背景,不实用attribute方式设定
- C#播放器实现暂停和继续播放
- 动态添加的数组绑定的事件如何消除?
- 怎样用C#读取INI文件?
- 在C#的“ASP.NET应用”中怎么插入框架!
- 如何精确实现在线数 寻传说中的application+cooklie发 初学者问题之三
- 我想做一个文本编辑器,它可以显示行号,像VS.NET那样的,如何做呢?
- 求助大神!c#的winform怎么禁止textBox输入非法字符?为什么用KeyPress不起作用?
- C# winform中如何使用DataGrid?分不多求解救。
using System.IO;
using System.Threading;
using System.Diagnostics;namespace CmdProcess
{
class Program
{
public static void Main(string[] args)
{
InitProcess(@"C:\Windows\System32\cmd.exe", "", null);
p.StandardInput.WriteLine(@"C:Windows\plink.exe [email protected]");
Console.ReadKey(true);
}
private static Process p = new Process();
private static void InitProcess(string file, string args, string dir)
{
try
{
p.StartInfo.FileName = file;
p.StartInfo.Arguments = args;
if (!string.IsNullOrEmpty(dir))
{
p.StartInfo.WorkingDirectory = dir;
}
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();
// // Handle Standard Output
// p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
// p.BeginOutputReadLine();
//
// p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
// p.BeginErrorReadLine();
new Thread(new ThreadStart(GetStdErr)).Start();
new Thread(new ThreadStart(GetStdOut)).Start();
new Thread(new ThreadStart(SetStdIn)).Start();
}
catch (Exception e)
{
Console.Error.WriteLine("SetStdIn: " + e.Message);
//System.Diagnostics.Debug.Print("SetStdIn: " + e.Message);
}
}
private static void CloseProcess()
{
p.Close();
p.Dispose();
}
private static void SetStdIn()
{
string line = "";
try
{
StreamReader reader = new StreamReader(Console.OpenStandardInput());// p.StandardOutput;
while (!reader.EndOfStream)
{
line = reader.ReadLine();
p.StandardInput.WriteLine(line);
}
}
catch (Exception e)
{
Console.Error.WriteLine("SetStdIn: " + e.Message);
//System.Diagnostics.Debug.Print("SetStdIn: " + e.Message);
}
}
private static void GetStdOut()
{
string line = "";
try
{
StreamReader reader = p.StandardOutput;
while (!reader.EndOfStream)
{
line = reader.ReadLine();
Console.WriteLine(line);
//System.Diagnostics.Debug.Print(line);
}
}
catch (Exception e)
{
Console.Error.WriteLine("GetStdOut: " + e.Message);
//System.Diagnostics.Debug.Print("GetStdOut: " + e.Message);
}
}
private static void GetStdErr()
{
string line = "";
try
{
StreamReader reader = p.StandardError;
while (!reader.EndOfStream)
{
line = reader.ReadLine();
Console.WriteLine(line);
//System.Diagnostics.Debug.Print(line);
}
}
catch (Exception e)
{
Console.Error.WriteLine("GetStdErr: " + e.Message);
//System.Diagnostics.Debug.Print("GetStdErr: " + e.Message);
}
}
}
}