我正在使用FolderBrowserDialog
让用户选择一个位置来保存文件和/或创建一个新文件夹。它在 99% 的时间都在工作,但是在某些情况下,当用户单击“创建新文件夹”按钮,更改名称,然后单击“确定”时,将抛出“新文件夹”不存在的异常。
即使用户将其重命名,代码似乎仍在寻找名称为“新文件夹”的文件夹。我可以在代码中进行哪些更改来处理此问题,以便文件始终保存在用户选择的文件夹中?
//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));