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);
            }
        }
相关推荐
辜月廿七31 分钟前
C#字符串相关知识
c#
波波0071 小时前
使用.NET 四步玩转 AI 绘图|不用Python、不买显卡
开发语言·c#·.net
唐青枫1 小时前
深入理解 C#.NET 运算符重载:语法、设计原则与最佳实践
c#·.net
工程师0072 小时前
C# HSL 与欧姆龙 CIP 协议(EtherNet/IP)的详细通信
网络协议·tcp/ip·c#·欧姆龙cip协议·hsl
林杜雨都9 小时前
Action和Func
开发语言·c#
工程师00710 小时前
TPL如何自动调整执行效率
c#·tpl
CreasyChan11 小时前
C# 反射详解
开发语言·前端·windows·unity·c#·游戏开发
c#上位机11 小时前
halcon求区域交集——intersection
图像处理·人工智能·计算机视觉·c#·halcon
布谷歌12 小时前
在java中实现c#的int.TryParse方法
java·开发语言·python·c#
用户44884667106017 小时前
.NET进阶——深入理解Lambda表达式(2)手搓LINQ语句
c#·.net