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);
            }
        }
相关推荐
gregmankiw22 分钟前
C#调用Rust动态链接库DLL的案例
开发语言·rust·c#
阿蒙Amon1 小时前
06. C#入门系列【自定义类型】:从青铜到王者的进阶之路
开发语言·c#
钢铁男儿5 小时前
C# 表达式和运算符(表达式和字面量)
开发语言·c#
林鸿群5 小时前
C#子线程更新主线程UI及委托回调使用示例
开发语言·c#
o0向阳而生0o5 小时前
63、.NET 异常处理
c#·.net·异常处理
SteveDraw8 小时前
C++动态链接库封装,供C#/C++ 等编程语言使用——C++动态链接库概述(总)
开发语言·c++·c#·封装·动态链接库
Kookoos8 小时前
性能剖析:在 ABP 框架中集成 MiniProfiler 实现性能可视化诊断
后端·c#·.net·abp vnext·miniprofiler
阿翰11 小时前
自动 GitHub Readme 20 种语言翻译平台 - OpenAiTx 开源免费
c#·.net
枫叶kx14 小时前
1Panel运行的.net程序无法读取系统字体(因为使用了docker)
c#
军训猫猫头20 小时前
96.如何使用C#实现串口发送? C#例子
开发语言·c#