2012-06-12 60 views
16

Vui lòng xem ảnh chụp bên dưới. Điều này được lấy từ quy trình làm việc "Tạo dự án mới" trong Visual Studio 2008.Hộp thoại trình duyệt thư mục như hộp thoại mở tập tin

Cửa sổ này được sử dụng để chọn thư mục mà dự án sẽ được lưu trữ. Làm cách nào để tạo một cửa sổ tương tự trong ứng dụng C# của tôi?

enter image description here

+1

Xem [câu hỏi này] (http://stackoverflow.com/questions/1250991/visual-studio-2008-folder-browser-dialog?rq=1). Về cơ bản họ dường như phân lớp các tập tin chuẩn mở dialg. –

+0

@UweKeim: Phân lớp con được giải thích ở đâu trong chuỗi câu hỏi đó? Ông đã trả lời rằng ông đã kết thúc bằng cách sử dụng VistaBridge – Rockstart

+1

Một số thời gian trở lại tôi đã tải về VistaBride và kiểm tra các nguồn về cách họ đã làm nó. IIRC nó đã được phân lớp của hộp thoại mở tập tin chuẩn. –

Trả lời

7

Giống như trong Office, hộp thoại cho phép chọn thư mục. Sự khác biệt duy nhất là nút Chọn thư mục có tên "OK" thay vì "Chọn thư mục".

Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application(); 
Microsoft.Office.Core.FileDialog fileDialog = app.get_FileDialog(Microsoft.Office.Core.MsoFileDialogType.msoFileDialogFolderPicker); 
fileDialog.InitialFileName = "c:\\Temp\\"; //something you want 
int nres = fileDialog.Show(); 
if (nres == -1) //ok 
{ 
    Microsoft.Office.Core.FileDialogSelectedItems selectedItems = fileDialog.SelectedItems; 

    string[] selectedFolders = selectedItems.Cast<string>().ToArray(); 

    if (selectedFolders.Length > 0) 
    { 
     string selectedFolder = selectedFolders[0]; 
    } 
} 

Tất nhiên, bạn cần phải thêm tham chiếu đến Microsoft.Office.Core (Microsoft Office 14.0 Object Library) và Microsoft.Office.Interop.Excel (Microsoft Excel 14.0 Object Library).

3

Tôi tìm thấy một bài viết tốt về mặc định FolderBrowserDialog và những hạn chế của nó: http://www.ssware.com/articles/folderbrowserdialog-unmasked-everything-you-wanted-to-know-about-the-folder-browser-component-from-dotnet-framework.htm

Có một compoment bên thứ ba "Shell MegaPack" (http://www.ssware.com/megapack.htm) từ ssware trong đó cung cấp Windows Explorer như file và thư mục trình duyệt-điều khiển cho WinForms, ASP.net và WPF.

0

tôi sửa đổi mã từ C# để VB, và env của tôi là VS2015 + Office 2010. Mã của tôi là hơi khác so với Daniel, như một số chức năng từ mã Daniel hỗ trợ để chỉ Office 2003/2007

Bằng cách sử dụng một Ví dụ excel mới, nó sẽ chậm hơn so với việc chỉ mở OpenFileDialog hoặc OpenFolderDialog, nhưng nó có thân thiện với người dùng hơn không. Chương trình của tôi chỉ gọi mã này một lần, vì vậy việc giao dịch cho hiệu suất thân thiện với người dùng không phải là mối lo ngại trong trường hợp của tôi.

Imports Microsoft.Office 
Imports Excel = Microsoft.Office.Interop.Excel 

Private Sub Button_select_raw_dir_Click(sender As Object, e As EventArgs) Handles Button_select_raw_dir.Click 
    Dim raw_app As Excel.Application = New Excel.Application 
    Dim raw_data_open_folder_dialog As Microsoft.Office.Core.FileDialog 
    raw_data_open_folder_dialog = raw_app.FileDialog(Microsoft.Office.Core.MsoFileDialogType.msoFileDialogFolderPicker) 
    raw_data_open_folder_dialog.AllowMultiSelect = False 
    raw_data_open_folder_dialog.Title = "Please select the raw data's dir " 
    Dim nres As Integer = raw_data_open_folder_dialog.Show() 
    Dim sz_SelectedPath As String = Nothing 
    If nres = -1 Then '-1 means open... lol 
     For Each selectedItems As Object In raw_data_open_folder_dialog.SelectedItems 
      sz_SelectedPath = selectedItems.ToString() 
     Next 
     TextBox_raw_data_dir.Text = sz_SelectedPath 
    End If 

    raw_app.Quit() 
    ReleaseComObject(raw_app) 
    GC.Collect() 
    GC.WaitForPendingFinalizers() 
End Sub 

' Release excel objects to avoid memory leak 
Public Sub ReleaseComObject(ByRef obj As Object) 
    Try 
     System.Runtime.InteropServices.Marshal.ReleaseComObject(obj) 
     obj = Nothing 
    Catch ex As Exception 
     obj = Nothing 
     MsgBox("Exception! Failed to release com obj, debug your code.") 
    End Try 
End Sub 

Nếu bạn muốn có một phiên bản C#, tôi tin rằng bạn đủ thông minh để cổng vào thư mục C# :)

0

Nếu bạn là ok với việc thêm một gói NuGet, Microsoft.WindowsAPICodePack.Shell có CommonOpenFileDialog rằng có thể được sử dụng trong "chế độ thư mục" phù hợp với mức sử dụng mong muốn của bạn.

var directoryDialog = new CommonOpenFileDialog 
    { 
    IsFolderPicker = true, 
    Title = "Select Folder" 
    }; 
Các vấn đề liên quan