Hi Axel,
There is no way to extract the destination file name from the IOException if
the file already exists when we call File.Move.
This can be confirmed by digging into the File class's code.
The File.Move class calls MoveFile function in kernel32.dll internally, so
actually the Move method itself doesn't do the Move job. When the MoveFile
function returns, code in Move method checks the return value, if the return
value is zero, which means an error occurred during move, it then calls an
internal helper method to get the last win32 error, converts it to .NET
exception and throws it:
if (!Win32Native.MoveFile(fullPathInternal, dst))
{
__Error.WinIOError();
}
The helper method WinIOError has an overload which can take two parameters,
an error code and a path. But as we can see here, the Move method somehow
doesn't pass in any information to the WinIOError, which causes WinIOError
to pass a string.Empty as the file path:
internal static void WinIOError()
{
WinIOError(Marshal.GetLastWin32Error(), string.Empty);
}
So the information you need from the exception is lost here.
Being different from File.Move, the File.Copy method internally call the
WinIOError this way to throw an Exception:
__Error.WinIOError(errorCode, maybeFullPath);
And if we do something like this (if the target file already exists):
File.Copy(@"C:\Users\jiewan\Desktop\snippet.txt", @"D:\snippet.txt", false);
We'll get an IOException with the following message: The file
'D:\snippet.txt' already exists.
I understand this is inconvenient for your scenario, however it seems to me
that we have to track the file current being used for the log:
try
{
File.Move(srcFile, destPath);
}
catch (IOException ioEx)
{
Log(string.Format("{0} already exists.", destPath));
}
Please kindly let me know if this clarifies your question on File.Move
method.
Regards,
Jie Wang (
[email protected], remove 'online.')
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
Note: MSDN Managed Newsgroup support offering is for non-urgent issues where
an initial response from the community or a Microsoft Support Engineer
within 2 business days is acceptable. Please note that each follow up
response may take approximately 2 business days as the support professional
working with you may need further investigation to reach the most efficient
resolution. The offering is not appropriate for situations that require
urgent, real-time or phone-based interactions. Issues of this nature are
best handled working with a dedicated Microsoft Support Engineer by
contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.