2009-09-14 138 views
5

我想設計一個程序包含瀏覽按鈕,我們可以瀏覽到選定的文件夾並打開文件夾內的文件。如何瀏覽文件夾

我需要一個參考和閱讀在哪裏我可以解決我的問題?像我應該使用什麼方法/類。我不喜歡從MSDN那裏閱讀,因爲我很難理解他們的理論。僅供參考,我仍然是C#的初學者。

非常感謝您

P/S:這裏是代碼,我從互聯網上,其中u可以瀏覽/創建新的文件夾中找到。但我不知道爲什麼它使用的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; 
    } 
} 
+6

爲了記錄在案:不想要閱讀MSDN文檔for Windows或.NET開發時是你可以做最壞的打算。 MSDN涵蓋絕對一切Windows和.NET。更重要的是,它也告訴你你不應該做什麼,這對你來說可能是特別重要的,因爲你是初學者。如果您不確定MSDN中的位置,請使用Google(或Bing)在MSDN上進行搜索。我確信在MSDN中有這樣一個例子,谷歌會爲你找到。 – OregonGhost 2009-09-14 09:23:28

+0

我意識到,但問題是,我在自學,有我無法理解的解釋。所以這就是爲什麼我需要依賴其他資源。謝謝你的建議! – user147685 2009-09-15 01:29:33

+1

繼續閱讀,直到你明白。 MSDN是你最好的朋友。 – banging 2013-07-15 14:55:41

回答

9

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); 
     } 
    } 
} 
+1

更喜歡圍繞OpenFileDialog openFileDialog1 = new OpenFileDialog();使用標籤 – RvdK 2009-09-14 09:17:26

+0

Stream myStream = null; '流'是指哪個類或參考? – user147685 2009-09-15 08:15:45

+0

對於MSDN鏈接 – banging 2013-07-15 14:56:10

0

要插入一個名爲名爲「Browse_Button」,在文本框中輸入文件名,點擊按鈕的文件路徑「ARfilePath 「上面的代碼將被修改爲:一的FolderBrowserDialog組件

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); 
      } 
     } 
    } 
1

從工具箱中拖動到您的表單,並將它命名的FolderBrowserDialog。在瀏覽按鈕事件處理程序中輸入以下代碼。

private void btnBrowseBackupLocation_Click(object sender, EventArgs e) 
    { 
     DialogResult result = folderBrowserDialog.ShowDialog(); 
     if (result == DialogResult.OK) 
     { 
      txtboxBackupLocation.Text = folderBrowserDialog.SelectedPath; 
     } 
    } 
3
  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; 
      }