1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| /// <include file='doc\File.uex' path='docs/doc[@for="File.InternalCopy"]/*' />
/// <devdoc>
/// Note: This returns the fully qualified name of the destination file.
/// </devdoc>
internal static String InternalCopy(String sourceFileName, String destFileName, bool overwrite) {
if (sourceFileName==null || destFileName==null)
throw new ArgumentNullException((sourceFileName==null ? "sourceFileName" : "destFileName"), Environment.GetResourceString("ArgumentNull_FileName"));
if (sourceFileName.Length==0 || destFileName.Length==0)
throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), (sourceFileName.Length==0 ? "sourceFileName" : "destFileName"));
String fullSourceFileName = Path.GetFullPathInternal(sourceFileName);
new FileIOPermission(FileIOPermissionAccess.Read, new String[] { fullSourceFileName }, false, false ).Demand();
String fullDestFileName = Path.GetFullPathInternal(destFileName);
new FileIOPermission(FileIOPermissionAccess.Write, new String[] { fullDestFileName }, false, false ).Demand();
bool r = Win32Native.CopyFile(fullSourceFileName, fullDestFileName, !overwrite);
if (!r) {
// Save Win32 error because subsequent checks will overwrite this HRESULT.
int hr = Marshal.GetLastWin32Error();
if (hr != Win32Native.ERROR_FILE_EXISTS) {
if (!InternalExists(fullSourceFileName))
__Error.WinIOError(Win32Native.ERROR_FILE_NOT_FOUND,sourceFileName);
if (Directory.InternalExists(fullDestFileName))
throw new IOException(Environment.GetResourceString("Arg_DirExists"));
}
__Error.WinIOError(hr, destFileName);
}
return fullDestFileName;
} |
Partager