欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C# 选择文件 选择文件夹

程序员文章站 2022-06-08 22:24:50
...

C# 选择文件 选择文件夹

        //选择文件夹
        private String selectFolder()
        {
            String path = "";
            System.Windows.Forms.FolderBrowserDialog folderChooser = new System.Windows.Forms.FolderBrowserDialog();
            folderChooser.Description = "请选择文件夹";
            if (folderChooser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                path = folderChooser.SelectedPath;
            }
            return path;
        }

        //选择文件
        private String selectFile()
        {
            String path = "";
            System.Windows.Forms.OpenFileDialog fileDialog = new System.Windows.Forms.OpenFileDialog();
            //设置是否可以多选
            fileDialog.Multiselect = true;
            //设置标题
            fileDialog.Title = "请选择文件";
            //设置初始打开目录,默认为空
            fileDialog.InitialDirectory = "";
            //设置文件过滤,不同格式用分号隔开
            fileDialog.Filter = "文本1(*.txt)|*.txt;*.py|文本2(*.csv)|*.csv";
            if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                path = fileDialog.FileName;
                //获取多个文件路径
                String[] paths = fileDialog.FileNames;
            }
            return path;
        }