C#_选择目录

一、添加

  在VS里打开程序包管理控制台,输入“Install-Package WindowsAPICodePack-Shell”
C#_选择目录_第1张图片

二、代码

        public string[] file()
        {
            string[] arr = new string[0];
            CommonOpenFileDialog dialog = new CommonOpenFileDialog();
            dialog.IsFolderPicker = false;//设置为选择文件夹
            dialog.Multiselect = true; //文件夹多选
            dialog.Title = "选择文件:";
            if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
            {
                arr = ((string[])dialog.FileNames.ToArray());
            }
            return arr;
        }        
   public string[] dir()
        {
            string[] arr = new string[0];
            CommonOpenFileDialog dialog = new CommonOpenFileDialog();
            dialog.IsFolderPicker = true;//设置为选择文件夹
            dialog.Multiselect = false; //文件夹多选
            dialog.Title = "选择目录:";
            if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
            {
                arr = Directory.GetFiles(dialog.FileName, "*.*", SearchOption.AllDirectories);//搜索当前目录及其所有的子目录
            }
            return arr;
        }

你可能感兴趣的:(C#,c#)