2009-09-14 37 views
5

Tôi muốn thiết kế một chương trình có nút duyệt, nơi chúng tôi có thể duyệt đến thư mục đã chọn và mở tệp bên trong thư mục.Cách duyệt thư mục

Tôi cần tham chiếu và đọc nơi tôi có thể giải quyết được sự cố của mình? Giống như những phương pháp/lớp tôi nên sử dụng. Tôi không thích đọc từ MSDN coz khó khăn cho tôi để hiểu lý thuyết của họ. FYI tôi vẫn còn mới bắt đầu trong C#.

Cảm ơn bạn rất nhiều

P/s: Đây là mã tôi tìm thấy từ internet nơi bạn có thể duyệt/tạo thư mục mới. Nhưng tôi không biết lý do tại sao nó sử dụng 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

Đối với hồ sơ: Không muốn đọc tài liệu MSDN khi phát triển cho Windows hoặc NET là điều tồi tệ nhất bạn có thể làm. MSDN bao gồm hoàn toàn mọi thứ Windows và .NET. Quan trọng hơn, nó cũng cho bạn biết bạn không nên làm gì, điều này có thể đặc biệt quan trọng đối với bạn vì bạn là người mới bắt đầu. Nếu bạn không chắc chắn nơi trong MSDN để tìm, sử dụng Google (hoặc Bing) để tìm kiếm trên MSDN. Tôi chắc chắn có một mẫu cho điều này trong MSDN mà Google sẽ tìm thấy cho bạn. – OregonGhost

+0

Tôi nhận ra rằng, nhưng vấn đề là, tôi tự học, có những giải thích mà tôi không thể hiểu được. Vì vậy, đó là lý do tại sao tôi cần phải phụ thuộc vào các nguồn lực khác. Cảm ơn về lời khuyên! – user147685

+1

Tiếp tục đọc cho đến khi bạn hiểu. MSDN là người bạn tốt nhất của bạn. – banging

Trả lời

9

từ 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

cũng thích OpenFileDialog openFileDialog1 = new OpenFileDialog(); bằng cách sử dụng thẻ – RvdK

+0

Phát trực tuyến myStream = null; 'Luồng' là tham chiếu đến lớp hoặc tham chiếu nào? – user147685

+0

+1 cho liên kết MSDN – banging

0

Để chèn đường dẫn file trên nhấp chuột vào nút có tên là "Browse_Button" với tên file trong hộp văn bản của tên "ARfilePath "mã ở trên sẽ được sửa đổi là:

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

Từ hộp công cụ kéo một thành phần FolderBrowserDialog vào biểu mẫu của bạn và đặt tên nó làBrowserDialog. Trong trình xử lý sự kiện nút duyệt của bạn, hãy viết mã sau đây.

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; 
      } 
Các vấn đề liên quan