C#如何编写程序运行exe可执行文件
点击按钮,实现功能:打开选择文件对话框,只能选择后缀为.exe的文件,选中后打开运行即可。
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
namespace SelectExe
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog(); //打开选择文件对话框
fileDialog.Filter = "*|*.exe"; //加入限制条件,只能选择后缀为.exe的文件
Process process = null;
if (fileDialog.ShowDialog() == DialogResult.OK)
{
process = new Process();
process.StartInfo.FileName = Path.GetFullPath(fileDialog.FileName);
process.Start(); //启动选择的exe文件
}
}
}
}
运行结果如下:
双击选择需要运行的.exe文件或选中该exe点击打开按钮即可