我正在使用以下代码将循环中的文件从一个位置移动到另一个位置,而我每次都在循环中创建一个新文件,但引发了以下异常:将文件从一个位置移动到C#中的其他位置
System.IO.IOException: Cannot create a file when that file already exists.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileInfo.MoveTo(String destFileName).
这是我的代码:
string strFile = strFileName;
try
{
string strFinalPath = ApplicationConfiguration.FinalInvoiceFolder;
if (!Directory.Exists(strFinalPath))
{
Directory.CreateDirectory(strFinalPath);
}
if (File.Exists(strPrintedFilePath))
{
objFile.MoveTo(strFinalPath + strFile);
}
}
catch (Exception ex2)
{
WriteLogCustom(ex2.ToString() + ex2.InnerException.ToString(), true);
}
http://www.dotnetperls.com/file-move –
要将2个或更多的字符串结合到一个更好的路径Path.Combine(strFinalPath,strFile) – Relax
哪些是文件的文件名? – Tinwor