Winform 打开文件夹、文件、拖拽上传

参考原文:https://blog.csdn.net/u012543266/article/details/21834073

1、打开文件

cs 复制代码
    private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog dialog = new OpenFileDialog();
        dialog.Multiselect = true;//该值确定是否可以选择多个文件
        dialog.Title = "请选择文件夹";
        dialog.Filter = "所有文件(*.*)|*.*";
        if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            string file = dialog.FileName;
        }
    }

2、打开文件夹

cs 复制代码
    private void button1_Click(object sender, EventArgs e)
    {
        System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
        dialog.Description = "请选择Txt所在文件夹";
        if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            if (string.IsNullOrEmpty(dialog.SelectedPath))
            {
                MessageBox.Show(this, "文件夹路径不能为空", "提示");
                return;
            }
        }
    }

3、拖拽

cs 复制代码
        private void Form1_Load(object sender, EventArgs e)
        {
            this.AllowDrop = true;
        }

        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Copy;
            else
                e.Effect = DragDropEffects.None;
        }

        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

            // 对拖放的文件进行处理
            foreach (string file in files)
            {
                // 处理文件
                Console.WriteLine(file);
            }
        }
相关推荐
张人玉19 小时前
C#WPF UI路由事件:事件冒泡与隧道机制
ui·c#·wpf
雪域迷影21 小时前
C#中通过get请求获取api.open-meteo.com网站的天气数据
开发语言·http·c#·get
yue00821 小时前
C#类继承
java·开发语言·c#
AI柠檬1 天前
几种排序算法的实现和性能比较
数据结构·算法·c#·排序算法
code bean1 天前
【C#】Channel<T>:现代 .NET 中的异步生产者-消费者模型详解
c#
yue0081 天前
C# XML文件的读写V2.0
xml·开发语言·c#
睡前要喝豆奶粉1 天前
.NET Core Web API开发需引入的三个基本依赖配置说明
oracle·c#·.netcore
张人玉1 天前
C# TCP 服务器和客户端
服务器·tcp/ip·c#
睡前要喝豆奶粉1 天前
.NET Core Web API中数据库相关配置
数据库·c#·.netcore
周杰伦fans1 天前
C# 中 Entity Framework (EF) 和 EF Core 里的 `AsNoTracking` 方法
开发语言·c#