1

I am using the FolderBrowserDialog to let the user select a location to save files, and/or create a new folder. It is working 99% of the time, however in some instances when the user clicks the Create New Folder button, changes the name, then clicks okay an exception will be thrown that "New Folder" does not exist.

It seems the code is still looking for a folder with the name "New Folder" even though the user renamed it. What could I change in my code to handle this issue so that the files are always saved in the folder the user selects?

//Declaring Filename
FolderBrowserDialog folderDlg = new FolderBrowserDialog();
folderDlg.ShowNewFolderButton = true;
folderDlg.Description = "Choose the location to save Files";
DialogResult result = folderDlg.ShowDialog();
if (result == DialogResult.OK)
{
    savelocation = folderDlg.SelectedPath;
}

// Choose whether to write header. Use EnableWithoutHeaderText instead to omit header.
dataGridExport.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
// Select all the cells
dataGridExport.SelectAll();
// Copy selected cells to DataObject
DataObject dataObject = dataGridExport.GetClipboardContent();
// Get the text of the DataObject, and serialize it to a file
File.WriteAllText(savelocation + "\\ExcelExport.csv", dataObject.GetText(TextDataFormat.CommaSeparatedValue));
Smith Stanley
  • 461
  • 1
  • 8
  • 25
  • @CaiusJard - do you have an alternative method to achieve allowing the user to select a save location? – Smith Stanley Dec 19 '17 at 16:36
  • 1
    BTW, always use Path.Combine to create your file paths. – LarsTech Dec 19 '17 at 16:36
  • You don't have code to handle the case where the dialog result isn't `DialogResult.OK`, could the issue occur when the default value of `savelocation` is used because of a different `DialogResult`? – 0liveradam8 Dec 19 '17 at 16:37
  • @0liveradam8 - I stepped through my code to check that, this is occuring when DialogResult.OK and the user creates a new folder – Smith Stanley Dec 19 '17 at 16:38

1 Answers1

2

It's unlikely to be a bug in your code; the user creates a new folder inside the FBD, FBD captures path as ...\new folder, user clicks it again (to rename it), renames it, then doesn't click it yet again (or click off it and back on) for FBD to realise the name has changed. Not your fault/problem; PEBKAC/PICNIC. Every windows software that used FBD suffers this.

As an side, FBD is a horrible thing, avoid using it (you can't always quickly paste a path into it to go there.. You must laboriously find your way through many directories using the mouse. It gets even more irritating when the initial directory selected by the FBD is reset to some default/not near what the user chose last time etc)

Use an OpenFileDialog instead and simultaneously prompt the user for a file name to save, or perhaps take a look at an SO question like How do you configure an OpenFileDialog to select folders? to see what people have done to get around the limitations of the FBD

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • I only want the user to select a save location. I do not want to allow them to be able to input a file name – Smith Stanley Dec 19 '17 at 16:39
  • 1
    I know, so you think that using the FBD is doing the user a favour. I implore you to use several pieces of software that employ the FBD and see just how shit a user experience it is for yourself. Please don't put your users through the pain of using it. Either find a way to use the OFD (do you really care if the user wants their own name rather than yours?), or provide the power users with a textbox to paste a path into (to overcome the FBD being a horrible thing intended only for duck egg users) and have the FBD update that textbox as the save path..and then put up with the New Folder problem – Caius Jard Dec 19 '17 at 16:42
  • Hmmm - I did not realize that FBD is so dreadful. I like the idea of a textbox or a formatted winform to allow the user to input a file path – Smith Stanley Dec 19 '17 at 16:44
  • Perhaps you could take a look at some other implementations of OpenFileDialog type dialogs that select folders - https://stackoverflow.com/questions/31059/how-do-you-configure-an-openfiledialog-to-select-folders - that answer also contains some libs that offer additional customisation to the default FBD, to take away some of the pain points - windows itself is quite flexible in terms of what can be done with an FBD, it's just the .net FBD control that is limited – Caius Jard Dec 19 '17 at 16:48
  • Some of those posts date back to 2008, is there no better methodology, lol – Smith Stanley Dec 19 '17 at 16:50
  • 1
    If you go to install a driver in WIndows 10 and choose "Browse manually..." then "Let me pick" then "have disk..." you get to the "select a driver from the floppy drive" dialog box that was present in win95/nt3.5.. Some parts of windows are ancient! – Caius Jard Dec 19 '17 at 16:53