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# :)
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. –
@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
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. –