C# 尝试不重写文件并将其保存在C中的新位置时发生意外的系统异常#

C# 尝试不重写文件并将其保存在C中的新位置时发生意外的系统异常#,c#,exception,save-as,C#,Exception,Save As,我在应用程序中使用savefiledialog来保存文件。如果相同的文件名已经存在,我会得到一个弹出窗口,询问我是否要替换。如果我给出否我会得到意外的系统异常。下面是代码的一部分 string fname1 = ""; saveFileDialog.Title = "Save the Proofer Report"; saveFileDialog.Filter = "Excel Files (*.xls)|*.xls"; saveFileDialog.FilterIndex = 0; save

我在应用程序中使用savefiledialog来保存文件。如果相同的文件名已经存在,我会得到一个弹出窗口,询问我是否要替换。如果我给出我会得到意外的系统异常。下面是代码的一部分

string fname1 = "";

saveFileDialog.Title = "Save the Proofer Report";
saveFileDialog.Filter = "Excel Files (*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.InitialDirectory = "MyDocuments";
saveFileDialog.FileName = "Proofer Report";
aveFileDialog.AddExtension = true;
saveFileDialog.ShowHelp = true;

// saveFileDialog.ShowDialog();

Invoke((Action)(() => { saveFileDialog.ShowDialog(); }));
fname1 = saveFileDialog.FileName;


                xlWorkBook.SaveAs(fname1, Excel.XlFileFormat.xlWorkbookNormal,       misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
 //system exception during save as
                xlWorkBook.Close(true, misValue, misValue);
                xlApp.Quit();
堆栈跟踪

在Microsoft.Office.Interop.Excel.\u工作簿.SaveAs(对象文件名, 对象文件格式、对象密码、对象写入密码、对象 ReadOnlyRecommended,对象CreateBackup,XLSaveAsAccess模式 AccessMode,对象冲突解决,对象添加,对象 TextCodepage、对象TextVisualLayout、对象本地)位于 ProoferXML.MainForm.ProcessDocument(BackgroundWorker-worker, DoWorkEventArgs e)在 D:\ProoferXML\WindowsFormsApplication1\WindowsFormsApplication1\MainForm.cs:line 665在ProoferXML.MainForm.prooferWorker_DoWork(对象发送方, DoWorkEventArgs e)在 D:\ProoferXML\WindowsFormsApplication1\WindowsFormsApplication1\MainForm.cs:line 1457


使用工作簿保存excel时。另存为从HRESULT获取异常:

很可能需要注意正在使用的线程模型。建议在调用Invoke之前检查invokererequired。 因此,您的代码应该如下所示:

 if (InvokeRequired)
 {
   Invoke((Action)(() => { saveFileDialog.ShowDialog(); }));
 }
 else
 {
   saveFileDialog.ShowDialog();
 }

一些更有用的阅读调用

我们能看到完整的代码吗(保存)?以及什么类型的异常?请发布您的异常如何向我们显示您获得的异常以及您获得异常的行我们无法分辨哪一行是行号1457.xl工作簿.SaveAs(fname1,Excel.XlFileFormat.xlWorkbookNormal,misValue,misValue,misValue,misValue,Excel.XlSaveAsAccessMode.xlExclusive,misValue,misValue,misValue,misValue,misValue);//保存时系统异常保存时出错--xlWorkBook.SaveAs(fname1,Excel.XlFileFormat.xlWorkbookNormal,misValue,misValue,misValue,misValue,Excel.XlSaveAsAccessMode.xlExclusive,misValue,misValue,misValue,misValue,misValue)使用工作簿保存excel时。saveAs从HRESULT获取异常:您可以尝试在调用saveAs之前使用Application.DisplayAlerts=false;以防止excel生成覆盖警告。