RTRT~!

解决方案 »

  1.   

    参考:
    http://hi.baidu.com/dongdongjiao/blog/item/0d479303209814ef08fa93d7.html
    http://topic.csdn.net/t/20060418/16/4695743.html
    http://download.csdn.net/source/1212489
      

  2.   

    你是想自己实现telnet?还是用现成的吧,自己写太惨了通过调用telnet程序,可以实现输入输出
    C#中process类的使用 Start 启动进程资源将其与process类关联Kill立即关闭进程waitforExit 在等待关联进程的退出Close 释放与此关联的所有进程/*
    * Created by SharpDevelop.
    * User: Administrator
    * Date: 2007-6-17
    * Time: 16:20

    * To change this template use Tools | Options | Coding | Edit Standard Headers.
    */using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;
    //process类的名空间
    using System.Diagnostics;namespace process
    {
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public partial class MainForm
    {
       [STAThread]
       public static void Main(string[] args)
       {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MainForm());
       }
      
       public MainForm()
       {
        //
        // The InitializeComponent() call is required for Windows Forms designer support.
        //
        InitializeComponent();
       
        //
        // TODO: Add constructor code after the InitializeComponent() call.
        //
       }
       //启动IE主页http://www.baidu.com/
       void Button1Click(object sender, System.EventArgs e)
       {
        Process.Start("IExplore.exe","http://www.baidu.com/");
       }
       //启动资源管理器
       void Button2Click(object sender, System.EventArgs e)
       {
        Process.Start("explorer.exe");
       }
       //启动office中的EXCEl
       void Button3Click(object sender, System.EventArgs e)
       {
        Process.Start("EXCEL.exe");
       }
       //启动WINDOWS播放器
       void Button4Click(object sender, System.EventArgs e)
       {
        Process.Start("dvdplay.exe");
       }
    }
    }
    源码下载:http://download.csdn.net/source/195507http://download1.csdn.net/down3/20070617/17164940911.rar 用C#来实现相同的效果,发现C#本身方便的进程线程机制使工作变得简单至极,随手记录一下。2.首先,我们可以通过设置Process类,获取输出接口,代码如下:      Process proc = new Process();
         proc .StartInfo.FileName = strScript;
         proc .StartInfo.WorkingDirectory = strDirectory;
         proc .StartInfo.CreateNoWindow = true;
         proc .StartInfo.UseShellExecute = false; 
         proc .StartInfo.RedirectStandardOutput = true;
         proc .Start();然后设置线程连续读取输出的字符串:     eventOutput = new AutoResetEvent(false);
         AutoResetEvent[] events = new AutoResetEvent[1];
         events[0] = m_eventOutput;     m_threadOutput = new Thread( new ThreadStart( DisplayOutput ) );
         m_threadOutput.Start();
         WaitHandle.WaitAll( events );线程函数如下:   private void DisplayOutput()
       {
        while ( m_procScript != null && !m_procScript.HasExited ) 
        {
         string strLine = null;
         while ( ( strLine = m_procScript.StandardOutput.ReadLine() ) != null) 
         {
          m_txtOutput.AppendText( strLine + "\r\n" );
          m_txtOutput.SelectionStart = m_txtOutput.Text.Length;
          m_txtOutput.ScrollToCaret(); 
         }
         Thread.Sleep( 100 ); 
        }
        m_eventOutput.Set();
       }这里要注意的是,使用以下语句使TextBox显示的总是最新添加的,而AppendText而不使用+=,是因为+=会造成整个TextBox的回显使得整个显示区域闪烁     m_txtOutput.AppendText( strLine + "\r\n" );
         m_txtOutput.SelectionStart = m_txtOutput.Text.Length;
         m_txtOutput.ScrollToCaret();为了不阻塞主线程,可以将整个过程放到一个另一个线程里就可以了
    3.bat文件控制参数的方法:
    将你的net use \\172.16.17.1 /user:username password写到bat文件中,然后运行下面代码就可以了。
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                process.StartInfo.CreateNoWindow = false;
                process.StartInfo.FileName = "d:\\netuse.bat";
                process.Start();程序控制参数方法:System.Diagnostics.ProcessStartInfo psi = 
           new System.Diagnostics.ProcessStartInfo();
    //prompt
    psi.FileName = @"C:\WINDOWS\system32\cmd.exe"; // Path for the cmd prompt
    psi.Arguments =@"net use \\172.16.17.1 /user:username password";
    psi.WindowStyle=System.Diagnostics.ProcessWindowStyle.Hidden;
    System.Diagnostics.Process.Start(psi);
    就是用进程启动cmd.exe使用Process类运行ShellExecute的一个问题(点击查看引用) 
    只有在STA线程上ShellExecute 才能确保工作无误。在Process的实现中,并没有考虑到这个问题,所以使用Process类运行ShellExecute可能会出错。如果你不能保证调用Process.Start的线程的ApartmentState,可以使用如下的代码来避免这个问题:using System;
    using System.Threading;
    public class Foo {
        public static void OpenUrl()    {
            System.Diagnostics.Process.Start(@"http://www.google.com");
        }
        public static void Main() {
            ThreadStart openUrlDelegate = new ThreadStart(Foo.OpenUrl);
            Thread myThread = new Thread(openUrlDelegate);
            myThread.SetApartmentState(ApartmentState.STA);    
            myThread.Start();
            myThread.Join();
        }
    }