如何浏览文件夹

我想设计一个包含浏览按钮的程序,我们可以浏览到所选文件夹并打开文件夹内的文件。

我需要一个参考和阅读,我可以解决我的问题? 喜欢我应该使用什么方法/类。 我不喜欢从MSDN上读书,因为我很难理解他们的理论。 仅供参考我仍然是C#的初学者。

非常感谢你

P / s:这是我从互联网上找到的代码,你可以浏览/创建新文件夹。 但我不知道为什么它使用Shell32.dll ..

private void button1_Click(object sender, EventArgs e) { string strPath; string strCaption = "Select a Directory and folder."; DialogResult dlgResult; Shell32.ShellClass shl = new Shell32.ShellClass(); Shell32.Folder2 fld = (Shell32.Folder2)shl.BrowseForFolder(0, strCaption, 0, System.Reflection.Missing.Value); if (fld == null) { dlgResult = DialogResult.Cancel; } else { strPath = fld.Self.Path; dlgResult = DialogResult.OK; } } 

来自msdn

 private void button1_Click(object sender, System.EventArgs e) { Stream myStream = null; OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.InitialDirectory = "c:\\" ; openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ; openFileDialog1.FilterIndex = 2 ; openFileDialog1.RestoreDirectory = true ; if(openFileDialog1.ShowDialog() == DialogResult.OK) { try { if ((myStream = openFileDialog1.OpenFile()) != null) { using (myStream) { // Insert code to read the stream here. } } } catch (Exception ex) { MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message); } } } 
  string folderpath = ""; FolderBrowserDialog fbd = new FolderBrowserDialog(); fbd.ShowNewFolderButton = false; fbd.RootFolder = System.Environment.SpecialFolder.MyComputer; DialogResult dr = fbd.ShowDialog(); if (dr == DialogResult.OK) { folderpath = fbd.SelectedPath; } if (folderpath != "") { txtBoxPath.Text = folderpath; } 

从工具箱中将FolderBrowserDialog组件拖到表单中,并将其命名为folderBrowserDialog。 在浏览按钮事件处理程序中编写以下代码。

  private void btnBrowseBackupLocation_Click(object sender, EventArgs e) { DialogResult result = folderBrowserDialog.ShowDialog(); if (result == DialogResult.OK) { txtboxBackupLocation.Text = folderBrowserDialog.SelectedPath; } } 

要在名为“Browse_Button”的按钮上单击名为“ARfilePath”的文本框中的文件名来插入文件路径,上面的代码将被修改为:

  private void Browse_Button_Click(object sender, EventArgs e) { Stream myStream = null; OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.InitialDirectory = "c:\\"; openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; openFileDialog1.FilterIndex = 2; //openFileDialog1.RestoreDirectory = true; Boolean FileExist=openFileDialog1.CheckFileExists; Boolean PathExist=openFileDialog1.CheckPathExists; openFileDialog1.FileName = null; if (openFileDialog1.ShowDialog() == DialogResult.OK) { try { if ((myStream = openFileDialog1.OpenFile()) != null) { using (myStream) { if (FileExist == true && PathExist == true) { // Insert code to read the stream here. string Pathname = openFileDialog1.FileName; ARfilePath.Text = Pathname; ARfilePath.Enabled = false; /*DISABLED SO THAT THE USER CANNOT MAKE UNNECESSARY CHANGES IN THE FIELD*/ } } } } catch (Exception ex) { /*SHOW ERRORS TO USER*/ error_label.Text = "Error: Could not read file from disk. Original error: " + ex.Message; //MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message); } } }