private void Button_Click(object sender, RoutedEventArgs e) { /* win forms verios: ugly var dialog = new System.Windows.Forms.FolderBrowserDialog(); System.Windows.Forms.DialogResult result = dialog.ShowDialog(); */ 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(); }
private void button1_Click(object sender, EventArgs e) { string defaultpath = @"C:\Program Files\Altium"; 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 = (Directory.Exists(defaultpath)) ? defaultpath : @"C:\"; 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]; folderPath = selectedFolder; richTextBox1.Text += "You've selected: " + folderPath + "\n"; } } }
/// <summary> /// GetOpenFilename using FileDialog object since GetOpenFileName doesn't allow an initial name /// </summary> /// <param name="initialFile"></param> /// <param name="filter"></param> /// <param name="defaultExt"></param> /// <param name="title"></param> /// <returns></returns> private static string ShowOfficeOpenFileDialog( string initialFile, string filter, string defaultExt, string title) { Microsoft.Office.Interop.Excel._Application app; Microsoft.Office.Core.FileDialog fd; string fileName = ""; if (Lex.StartsWith(initialFile, "http")) // url fixups { if (IO.Path.GetFileName(initialFile).Contains(".")) { // if url with file name remove file name so all directory entries appear in dialog (only this one appears otherwise) initialFile = IO.Path.GetDirectoryName(initialFile); initialFile = initialFile.Replace(@":\", "://"); // fixup from GetDirectoryName initialFile = initialFile.Replace(@"\", "/"); } if (!initialFile.EndsWith("/")) { initialFile += "/"; // be sure we have final slash so web site is properly recognized } } app = new Microsoft.Office.Interop.Excel.Application(); fd = app.get_FileDialog(MsoFileDialogType.msoFileDialogOpen); // get the open dialog fd.Title = title; fd.InitialFileName = initialFile; SetupFilters(filter, defaultExt, fd); // setup the filters int rc = fd.Show(); if (rc == 0 || fd.SelectedItems.Count == 0) { fileName = ""; } else { foreach (object o in fd.SelectedItems) { fileName = o.ToString(); break; } string fileExt = IO.Path.GetExtension(fileName); if (fileExt == "" && defaultExt != "") // if no extension add default extension { if (!defaultExt.StartsWith(".")) { fileName += "."; } fileName += defaultExt; } } ReleaseObject(fd); ReleaseObject(app); return(fileName); }