net C#如何调用EXE文件

.net C#如何调用EXE文件

C#代码 2009-03-18 15:59:24 阅读647 评论0 字号:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Diagnostics;

namespace Shell

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)

        {

            ProcessStartInfo info = new ProcessStartInfo(textBox1.Text);

            try

            {

                Process pro = Process.Start(info);

                this.Dispose(true);

            }

            catch (Exception ee) { MessageBox.Show(ee.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

        }

        private void button2_Click(object sender, EventArgs e)

        {

            this.Dispose(true);

        }

        private void button3_Click(object sender, EventArgs e)

        {

            openFileDialog1.FileName = string.Empty;

            openFileDialog1.Filter = "可执行文件|*.exe|所有文件(*.*)|*.*";

            DialogResult DR = openFileDialog1.ShowDialog();

            if (DR == DialogResult.OK) textBox1.Text = openFileDialog1.FileName;

        }

        private void textBox1_TextChanged(object sender, EventArgs e)

        {

            if (textBox1.Text == string.Empty) button1.Enabled = false;

            else button1.Enabled = true;

        }

    }

}

 

2

 

例如下面是调用windows中的计算器文件.

Help.ShowHelp(this,"C://WINDOWS//system32//calc.exe");

 

作者:xingzhe516      发表时间:2005-5-11 12:56:00

 2楼  

静态方法Process.Start();

 

作者:heilong05      发表时间:2005-5-13 17:07:00

 3楼  

这里是一个简单的调用例子,可以效仿:
*  功        能:通过C#程序调用  Windows  记事本程序  编辑一个
*  名为  test.txt  的文本文件。
*
*  在整个程序中  System.Diagnostics.Process.Start(Info)  
*  为主要语句。
*  如果只是单独执行一个外部程序,可用一条如下代码即可:
*  System.Diagnostics.Process.Start(
*  "外部程序名","启动参数");
*/

using  System;

class  test
{
static  void  Main()
{

//声明一个程序信息类
System.Diagnostics.ProcessStartInfo  Info  =  new  System.Diagnostics.ProcessStartInfo();

//设置外部程序名
Info.FileName  =  "notepad.exe";

//设置外部程序的启动参数(命令行参数)为test.txt
Info.Arguments  =  "test.txt";

//设置外部程序工作目录为  C:/
Info.WorkingDirectory  =  "C://";

//声明一个程序类
System.Diagnostics.Process  Proc  ;

try
{
//
//启动外部程序
//
Proc  =  System.Diagnostics.Process.Start(Info);
}
catch(System.ComponentModel.Win32Exception  e)
{
Console.WriteLine("系统找不到指定的程序文件。/r{0}",  e);
return;
}

//打印出外部程序的开始执行时间
Console.WriteLine("外部程序的开始执行时间:{0}",  Proc.StartTime);

//等待3秒钟
Proc.WaitForExit(3000);

//如果这个外部程序没有结束运行则对其强行终止
if(Proc.HasExited  ==  false)
{
Console.WriteLine("由主程序强行终止外部程序的运行!");
Proc.Kill();
}
else
{
Console.WriteLine("由外部程序正常退出!");
}
Console.WriteLine("外部程序的结束运行时间:{0}",  Proc.ExitTime);
Console.WriteLine("外部程序在结束运行时的返回值:{0}",  Proc.ExitCode);
}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值