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));