C# 几种选择文件Filter文件后缀很全

//第一种 打开
OpenFileDialog ofd = new OpenFileDialog();//新建打开文件对话框
            ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);//设置初始文件目录
           // ofd.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";//设置打开文件类型 文本文件(*.xls)|
            string filter = @"所有文件|*.*|
                           压缩文件|*.zip;*.rar;*.arj|
                           文本文档|*.xls;*.txt;*.pdf;*.doc;*.html;*.wps;*.rtf|
                           图片文件|*.jpg;*.png;*.gif;*.jpeg;*.bmp|
                           视频文件|*.avi;*.mp3;*.swf;*.mpg;*.mov|
                           系统文件|*.int;*.sys;*.dll;*.adt|
                           可执行文件|*.exe;*.com;*.bat;*.vbs";
            ofd.Filter = Regex.Replace(filter, @"\s", "");
            if (ofd.ShowDialog(this) == DialogResult.OK)
            {   //下边可以添加用户代码
                MessageBox.Show(FileName);

            }


//第二种  保存
 if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                string selUrl = folderBrowserDialog1.SelectedPath;
                Console.WriteLine(selUrl);
            }



//第三种  保存
            FolderBrowserDialog P_File_Folder = new FolderBrowserDialog();
            if (P_File_Folder.ShowDialog() == DialogResult.OK)
            {
                Console.WriteLine(P_File_Folder.SelectedPath + "11.xls");
            }
//第四种 打开 遍历文件夹 
  FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string foldPath = dialog.SelectedPath;
                DirectoryInfo theFolder = new DirectoryInfo(foldPath);

                //theFolder 包含文件路径

                FileInfo[] dirInfo = theFolder.GetFiles();
                //遍历文件夹                
                foreach (FileInfo file in dirInfo)
                {
                    MessageBox.Show(file.ToString());
                }
            }
//第五种  选择文件保存输入名称
 SaveFileDialog dialog = new SaveFileDialog
            {
                Filter = "Excel文件(*.xls)|*.xls",
                Title = "数据文件导出",
                RestoreDirectory = true,
               // FileName = this.OpenFileFullName.Remove(this.OpenFileFullName.LastIndexOf(".gsmr"))
            };

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                MessageBox.Show(dialog.FileName);
                

            }

 

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