• WinForm打开一个文件夹(得到文件夹路径 2种方法)


    方法一:

    Form窗体拖一个FolderBrowserDialog控件 在按钮事件中写入以下代码:

    //打开选择文件夹目录
            private void btnOpen_Click(object sender, EventArgs e)
            {

                folderBrowserDialog1.RootFolder = Environment.SpecialFolder.Desktop;
                folderBrowserDialog1.Description = "请选择文件夹";
                DialogResult d = folderBrowserDialog1.ShowDialog();
                if (d == DialogResult.OK)
                {

                    //txtFilesName指的是界面一个文本框获取路径
                    txtFilesName.Text = folderBrowserDialog1.SelectedPath;
                }
                else
                    txtFilesName.Text = "请选择目录!";
            }

            private void btnEXC_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }

    方法二:

    新建一个类名字为FolderDialog.cs代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms.Design;
    using System.Windows.Forms;

    namespace Read
    {
        public class FolderDialog:FolderNameEditor
        {
            FolderNameEditor.FolderBrowser fDialog = new System.Windows.Forms.Design.FolderNameEditor.FolderBrowser();
            public FolderDialog() { }
            public DialogResult DisplayDialog()
            {
                return DisplayDialog("请选择一个文件夹");
            }
            public DialogResult DisplayDialog(string description)
            {
                fDialog.Description = description;
                return fDialog.ShowDialog();
            }
            public string Path
            {
                get
                {
                    return fDialog.DirectoryPath;
                }
            }
            ~FolderDialog()
            {
                fDialog.Dispose();
            }

        }
    }

    在窗体的后台事件调用:

    //打开选择文件夹目录
            private void btnOpen_Click(object sender, EventArgs e)
            {
                FolderDialog openFolder = new FolderDialog();
                if (openFolder.DisplayDialog() == DialogResult.OK)
                {
                    txtFilesName.Text = openFolder.Path.ToString();
                }
                else
                    txtFilesName.Text = "请选择目录!";

  • 相关阅读:
    DevExpress gridcontrol添加了复选框删除选中的多行/批量删除的方法
    PowerDesigner中在生成的数据库脚本中用name列替换comment列作为字段描述的方法
    int.TryParse 与 int.Parse 的区别
    .NET winform 的keypress事件中判断当用户按下的是哪个键
    XtraReport改变纸张方向
    .NET的 DataTable中某列求和
    SQL UNION 操作符
    如何在VUE中使用leaflet地图框架
    eslint+prettier 的 VSCode配置项
    前端使用Git 切换分支 查看线上远程,本地切换
  • 原文地址:https://www.cnblogs.com/mfryf/p/2348679.html
Copyright © 2020-2023  润新知