Please see the snapshot below. This was taken from "New project creation" workflow in Visual Studio 2008.
This window is used for selecting a folder in which the project will be stored. How do I create a similar window in my c# application?
Please see the snapshot below. This was taken from "New project creation" workflow in Visual Studio 2008.
This window is used for selecting a folder in which the project will be stored. How do I create a similar window in my c# application?
It is something similar in Office, a dialog which allows to select a folder. The only difference is that the Select folder button is named "OK" instead of "Select folder".
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];
}
}
Of course, you need to add references to Microsoft.Office.Core (Microsoft Office 14.0 Object Library) and Microsoft.Office.Interop.Excel (Microsoft Excel 14.0 Object Library).
I found a good article about the default FolderBrowserDialog and its limitations: http://www.ssware.com/articles/folderbrowserdialog-unmasked-everything-you-wanted-to-know-about-the-folder-browser-component-from-dotnet-framework.htm
There is a third party compoment "Shell MegaPack" (http://www.ssware.com/megapack.htm) from ssware which offers windows explorer like file and folder browser-controls for WinForms, ASP.net and WPF.
If you are ok with adding a nuget package, Microsoft.WindowsAPICodePack.Shell has a CommonOpenFileDialog
that can be used in "folder mode" which should match your desired usage.
var directoryDialog = new CommonOpenFileDialog
{
IsFolderPicker = true,
Title = "Select Folder"
};
I modified the code from C# to VB, and my env is VS2015 + Office 2010. My code is slightly different than Daniel's, as some function from Daniel's code supports to only Office 2003/2007
By using a new excel instance, it will be slower than just opening a OpenFileDialog or OpenFolderDialog, but is it way more user friendly. My program is only calling this code once, so trading off the performance for user-friendliness is not a concern in my case.
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
If you want a C# version, I believe you are smart enough to port it to C# :)
Kindly check out BetterFolderBrowser. It provides just what you need and so much more.
BetterFolderBrowser is a .NET component library that was written to help developers provide a better folder-browsing and selection experience to users by employing a similar browser dialog as the standard OpenFileDialog in place of the current FolderBrowserDialog which only allows for single-folder selections with its tree-view display format. This allows for a much easier viewing, modification, searching and selection experience using the standard Windows Explorer dialog.
© 2022 - 2024 — McMap. All rights reserved.
SaveFileDialog
with the zero-width space symbol inserted as a filename. Then I just stripped off the path withPath.GetFullPath
. – Arlenarlena