在软件中体悟人生 在人生中感悟软件

专注Web项目设计、实现和管理
  博客园  :: 首页  :: 联系 :: 订阅 订阅  :: 管理

c#调用外部程序

Posted on 2010-08-23 14:31  王景  阅读(213)  评论(0编辑  收藏  举报
 private System.Timers.Timer m_timer;
   
    void Application_Start(object sender, EventArgs e)
    {
        m_timer = new System.Timers.Timer(1000);
        m_timer.Enabled = true;
        m_timer.Elapsed += new System.Timers.ElapsedEventHandler(m_timer_Elapsed);  //事件代理
        //开启时钟。
        m_timer.Start();

    }       

//定时执行 事件

 private void m_timer_Elapsed(object sender, System.Timers.ElapsedEventArgs args)
    {

string mypath = "cd " + HttpRuntime.AppDomainAppPath;
        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("c:");
        p.StandardInput.WriteLine(mypath);
        p.StandardInput.WriteLine("s.pl");
        p.StandardInput.WriteLine("md ok8");
        p.StandardInput.WriteLine("exit");
        string myreult = p.StandardOutput.ReadToEnd();
        p.Close();
        File.WriteAllText(@"C:\re.txt", myreult);

}