C# 之 选择并调用文件[winform]

winform 之 选择并调用文件

  1. form.cs[设计] 文件中选择一个button, 然后设置一个点击函数

  2. 将下方内容复制到函数中执行

c 复制代码
 private void push_btn_Click(object sender, EventArgs e)
 { 
 	 // 1. 打开文件管理器选择文件
     OpenFileDialog openFileDialog1 = new OpenFileDialog();  //显示选择文件对话框
     openFileDialog1.InitialDirectory = "c:\\";
     openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; //所有的文件格式
     openFileDialog1.FilterIndex = 2;
     openFileDialog1.RestoreDirectory = true;

	 // 2. 查看可执行文件路径
     if (openFileDialog1.ShowDialog() == DialogResult.OK)
     {
         updateTextBoxContent(openFileDialog1.FileName);// 这一步, 我是查看文件路径, 就是输出路径
         try
         {
             // 3. 根据可执行文件的路径, 调用该可执行文件
             string path = openFileDialog1.FileName;
             Process pro = new Process();
             FileInfo file = new FileInfo(path);
             pro.StartInfo.WorkingDirectory = file.Directory.FullName;
             pro.StartInfo.FileName = path;
             pro.StartInfo.CreateNoWindow = false;
             pro.Start(); // 启动进程资源并将其与 Process 组件关联。
             pro.WaitForExit(); // 指示 Process 组件无限期地等待关联进程退出。
             //MessageBox.Show("bat文件执行成功!");
         }
         catch (Exception ex)
         {
             MessageBox.Show("执行失败 错误原因:" + ex.Message);
         }
     }
 }

结果如下:

  1. 点击后, 会打开资源管理器
  2. 选择一个可执行文件(我使用的是.bat文件, 是可行的)
相关推荐
雨落倾城夏未凉3 天前
第四章c#方法-参数数组和可选参数(16)
后端·c#
唐青枫4 天前
线程不是越多越快:C#.NET Thread 生命周期、同步与后台工作线程实战
c#·.net
唐青枫5 天前
别只会反射:C#.NET Emit 动态生成代码实战详解
c#·.net
咕白m6255 天前
.NET 环境下 Word 超链接批量提取方案
c#·.net
用户91721561902115 天前
C# 通信协议增量解析:用状态机处理半包和粘包
c#
小码编匠6 天前
C# 工控上位机必备:数据转换工具类与十个核心模块
后端·c#·.net
唐青枫8 天前
别再乱用 StartNew:C#.NET TaskFactory 任务调度实战详解
c#·.net
Artech8 天前
[MAF预定义的AIContextProvider-03]ChatHistoryMemoryProvider——赋予Agent从经验中学习的能力
ai·c#·agent·memory·maf
Scout-leaf10 天前
C#摸鱼实录——IoC与DI案例详解
c#
咕白m62510 天前
使用 C# 在 Excel 中应用多种字体样式
后端·c#