gkket 发表于 2021-9-4 11:57:16

Winform 选择文件、文件夹、打开指定目录的方法

Winform 选择文件、文件夹、打开指定目录的方法

using System;

using System.Windows.Forms;



namespace WindowsFormsApp1

{

    public partial class Form1 : Form

    {

      public Form1()

      {

            InitializeComponent();

      }



      /// <summary>

      /// 选择文件

      /// </summary>

      /// <param name="sender"></param>

      /// <param name="e"></param>

      private void button1_Click(object sender, EventArgs e)

      {

            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.Multiselect = true;

            openFileDialog1.Title = "请选择文件";

            openFileDialog1.Filter = "所有文件(*.*)|*.*";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)

            {

                string filename = openFileDialog1.FileName;

                MessageBox.Show("已选择文件:" + filename, "选择文件提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

      }



      /// <summary>

      /// 选择文件夹、路径

      /// </summary>

      /// <param name="sender"></param>

      /// <param name="e"></param>

      private void button2_Click(object sender, EventArgs e)

      {

            FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();

            folderBrowserDialog1.Description = "请选择文件路径";

            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)

            {

                string path = folderBrowserDialog1.SelectedPath;

                MessageBox.Show("已选择文件夹:" + path, "选择文件夹提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

      }



      /// <summary>

      /// 打开目录

      /// </summary>

      /// <param name="sender"></param>

      /// <param name="e"></param>

      private void button3_Click(object sender, EventArgs e)

      {

            System.Diagnostics.Process.Start("Explorer.exe", "C:\\Windows");

      }

    }

}

来源:C#社区
原文:https://www.hicsharp.com/a/56eb4f0c4f2c4b98af522634d2e1789c



li781388 发表于 2021-9-4 12:03:59

这个类目最活跃了!赞一个!

詹姆斯 发表于 2021-9-4 12:09:17

真是被感动的痛哭流涕……

漠北 发表于 2025-11-17 09:31:23

求个链接 / 教程,楼主好人一生平安~

mark_qM1gr 发表于 2025-11-17 09:42:26

同款经历!我当初也这么过来的😂

@Xizi_yE0Mjfrw 发表于 2025-11-17 09:48:35

楼主太会说了,字字句句都在理

caixiaobao2020 发表于 2025-11-17 09:49:30

谁懂啊!真的被戳中笑点 / 泪点了

黎明 发表于 2025-11-17 09:51:09

楼主太会说了,字字句句都在理~

zcx 发表于 2025-11-17 09:52:33

学到了学到了,这波分享太实用啦!

国栋_dFz8Z 发表于 2025-11-17 09:56:08

蹲一波同款,有没有姐妹 / 兄弟推荐?
页: [1] 2
查看完整版本: Winform 选择文件、文件夹、打开指定目录的方法