OpenFileDialog 和 FolderBrowserDialog
OpenFileDialog 和 FolderBrowserDialog: --------------------------------------------------------------------------------------------------------- 【 FolderBrowserDialog 的使用】: 提示用户选择文件夹; //设置根在桌面 folderBrowserDialog1.RootFolder = SpecialFolder.Desktop; //设置当前选择的路径 folderBrowserDialog1.SelectedPath = "C:"; //允许在对话框中包括一个新建目录的按钮 folderBrowserDialog1.ShowNewFolderButton = true; //设置对话框的说明信息 folderBrowserDialog1.Description = "请选择输出目录"; //默认路径是程序所在目录的Data文件夹 folderBrowserDialog1.SelectedPath = System.Environment.CurrentDirectory.ToString() + @"/Data"; //弹出框用于选择路径,完成后校验路径是否正确 if(folderBrowser.Dialog1.ShowDialog() == DialogResult.OK) { //获取选择的路径 String szSelectedPath = folderBrowserDialog1.SelectedPath; } 例: //点击浏览按钮后,填出框用于选择路径 private void ButtonScanClick(object sender, EventArgs e) { FolderBrowserDialog fbd = new FolderBrowserDialog (); fbd.Description = "选择文件夹"; fbd.RootFolder = Environment.SpecialFolder.MyComputer; fbd.ShowNewFolderButton = true; if(fbd.ShowDialog() != DialogResult.OK) return; string path=fbd.SelectedPath; textBox1.Text=path; } --------------------------------------------------------------------------------------------------------- 【 OpenFileDialog 的使用】: 提示用户打开文件,使用此类可检查某个文件是否存在并打开该文件; 例: private void Button1Click(object sender, EventArgs e) { //类的实例化 OpenFileDialog openFileDialog1 = new OpenFileDialog(); //打开位置 openFileDialog1.InitialDirectory =Environment.GetFolderPath(Environment.SpecialFolder.MyComputer); //文件类型 openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; //表示默认筛选情况 openFileDialog1.FilterIndex = 1; //获取或设置一个值,该值指示对话框在关闭前是否还原当前目录。 openFileDialog1.RestoreDirectory = true; //文件有效性验证ValidateNames,验证用户输入是否是一个有效的Windows文件名 openFileDialog1.ValidateNames = true; //验证路径有效性 openFileDialog1.CheckFileExists = true; //验证文件有效性 openFileDialog1.CheckPathExists = true; //弹出框用于选择路径,完成后校验路径是否正确 if (openFileDialog1.ShowDialog() != DialogResult.OK) return; string path=openFileDialog1.SafeFileName; this.textBox1.AppendText(path); } ---------------------------------------------------------------------------------------------------------
posted on 2012-11-21 10:14 xuejianhui 阅读(599) 评论(0) 编辑 收藏 举报