-3

I have created a software, which save some Information in an Excel File. The software has two possibilities:

  1. To select the folder and then the software will create an Excel File with random name and there will save the data , or

  2. To select an existed Excel File which is created from the user and there will save the data.

How to create a ONE ShowDialog to ask the user do you want to choose the folder or the file?!

Code that I have used to choose a Folder:

using (var fbd = new FolderBrowserDialog())
           {
               DialogResult result = fbd.ShowDialog();

               if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
               {

                   textBox3.Text = fbd.SelectedPath;

               }
          }

Code that I have used to choose a File:

OpenFileDialog excelFilename = new OpenFileDialog();

           if (excelFilename.ShowDialog() == DialogResult.OK)
           {
               textBox3.Text = excelFilename.FileName;

           }

kn1ghtxx
  • 39
  • 7
  • Ask the user first and then do what you need. Another option is roll your own. IMHO go with the first. – Trevor Apr 19 '21 at 12:30
  • @Codexer What do you excatly mean with Ask the user!? To make two buttons if the user want one or another Option ? – kn1ghtxx Apr 19 '21 at 12:32
  • You can ask the user how ever you want. You could have a default of choosing a folder, if they don't want to choose a folder, go the file route. – Trevor Apr 19 '21 at 12:33
  • @Codexer ...but this is not what I asked for !! – kn1ghtxx Apr 19 '21 at 12:35
  • Does this answer your question? [How to use OpenFileDialog to select a folder?](https://stackoverflow.com/questions/11624298/how-to-use-openfiledialog-to-select-a-folder) – JonasH Apr 19 '21 at 12:38

1 Answers1

-1

Maybe something like this?:

MessageBoxResult result = MessageBox.Show("Ask user what he wants to do", "", MessageBoxButton.YesNo);

string path = null;

//MessageBoxResult.Yes means File, .No means Folder
if (result == MessageBoxResult.Yes)
{
    //File-Picker
    OpenFileDialog filePicker = new OpenFileDialog()
    {
        FileName = "Excel File",
        DefaultExt = ".xls",
        Filter = "Excel Document | *.xls"
    };

    if (filePicker.ShowDialog() == true) 
    {
        path = filePicker.FileName;
    }
}
else if (result == MessageBoxResult.No)
{
    //Folder-Picker
    CommonOpenFileDialog folderPicker = new CommonOpenFileDialog()
    {
        IsFolderPicker = true,
        Multiselect = false
    };

    path = Path.Combine(folderPicker.FileName, DateTime.Now.ToString("yyyy-MM-ddTHH-mm-ss"));
    
    //Create file in folder
    File.Create(path);
}

if (path != null)
{
    // Do something with path (file user selected/program created)
}

Make sure you have the using System.Windows, using Microsoft.Win32; and using System.IO; using's at the beginning of your program.

You also have to add the WindowsAPICodePack library to your project to use the folder-picker. You can do that with the NuGet manager. Here's the link to the library: WindowsAPICodePack - 1.1.1

baltermia
  • 1,151
  • 1
  • 11
  • 26