Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
File.Copy and lock file
Message
From
19/02/2014 09:08:16
 
 
To
19/02/2014 03:42:57
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01594636
Message ID:
01594656
Views:
33
>>>Is it possible that such a command does not lock the file?
>>>
>>>
>>>File.Copy(cSource, cDestination, lOverwrite)
>>>
>>>
>>>I am copying a file into a directory and in the middle of the copy, I have an access is denied. We believe this is because someone else took over the file. But, I assumed File.Copy would have locked the file during the process of the copy.
>>
>>Not sure what you mean by 'in the *middle* of the copy' ?
>>
>>If you are getting the UnauthorizedAccessException then, as Gregory suggests, I'd take it at face value and look at the permissions.
>
>Viv,
>
>I've had a look at the source. It doesn't open anything. Just calls a method, calling a method, finally calling CopyFile api
>Certainly CopyFile will open the source and the destination
>
>Maybe you can spot something different ? Maybe FileSecurityState/FileIOPermission - but I do not think so

Only have time for a quick look. Don't know which block is being used ( FEATURE_CORECLR && !FEATURE_LEGACYNETCFIOSECURITY) but FileIOPermission.Demand() can throw an exception and presumably FileSecurityState.EnsureState() could do the same - don't see any docs. for the latter class ?


>from File.cs (excerpts)
>
>        public static void Copy(String sourceFileName, String destFileName, bool overwrite) { 
>            if (sourceFileName == null)
>                throw new ArgumentNullException("sourceFileName", Environment.GetResourceString("ArgumentNull_FileName")); 
>            if (destFileName == null)
>                throw new ArgumentNullException("destFileName", Environment.GetResourceString("ArgumentNull_FileName"));
>            if (sourceFileName.Length == 0)
>                throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "sourceFileName"); 
>            if (destFileName.Length == 0)
>                throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "destFileName"); 
>            Contract.EndContractBlock(); 
>
>            InternalCopy(sourceFileName, destFileName, overwrite, true); 
>        }
>
>
>
>        [ResourceExposure(ResourceScope.Machine)]
>        [ResourceConsumption(ResourceScope.Machine)]
>        internal static String InternalCopy(String sourceFileName, String destFileName, bool overwrite, bool checkHost) {
>            Contract.Requires(sourceFileName != null); 
>            Contract.Requires(destFileName != null);
>            Contract.Requires(sourceFileName.Length > 0); 
>            Contract.Requires(destFileName.Length > 0); 
>
>            String fullSourceFileName = Path.GetFullPathInternal(sourceFileName); 
>            String fullDestFileName = Path.GetFullPathInternal(destFileName);
>
>#if FEATURE_CORECLR && !FEATURE_LEGACYNETCFIOSECURITY
>            if (checkHost) { 
>                FileSecurityState sourceState = new FileSecurityState(FileSecurityStateAccess.Read, sourceFileName, fullSourceFileName);
>                FileSecurityState destState = new FileSecurityState(FileSecurityStateAccess.Write, destFileName, fullDestFileName); 
>                sourceState.EnsureState(); 
>                destState.EnsureState();
>            } 
>            else {
>#endif
>            new FileIOPermission(FileIOPermissionAccess.Read, new String[] { fullSourceFileName }, false, false ).Demand();
>            new FileIOPermission(FileIOPermissionAccess.Write, new String[] { fullDestFileName }, false, false ).Demand(); 
>#if FEATURE_CORECLR && !FEATURE_LEGACYNETCFIOSECURITY
>            } 
>#endif 
>
>            bool r = Win32Native.CopyFile(fullSourceFileName, fullDestFileName, !overwrite); 
>            if (!r) {
>                // Save Win32 error because subsequent checks will overwrite this HRESULT.
>                int errorCode = Marshal.GetLastWin32Error();
>                String fileName = destFileName; 
>
>                if (errorCode != Win32Native.ERROR_FILE_EXISTS) { 
>                    // For a number of error codes (sharing violation, path 
>                    // not found, etc) we don't know if the problem was with
>                    // the source or dest file.  Try reading the source file. 
>                    using(SafeFileHandle handle = Win32Native.UnsafeCreateFile(fullSourceFileName, FileStream.GENERIC_READ, FileShare.Read, null, FileMode.Open, 0, IntPtr.Zero)) {
>                        if (handle.IsInvalid)
>                            fileName = sourceFileName;
>                    } 
>
>                    if (errorCode == Win32Native.ERROR_ACCESS_DENIED) { 
>                        if (Directory.InternalExists(fullDestFileName)) 
>                            throw new IOException(Environment.GetResourceString("Arg_FileIsDirectory_Name", destFileName), Win32Native.ERROR_ACCESS_DENIED, fullDestFileName);
>                    } 
>                }
>
>                __Error.WinIOError(errorCode, fileName);
>            } 
>
>            return fullDestFileName; 
>        } 
>
>
>
>Win32Native.cs (excerpt)
>
>        [DllImport(KERNEL32, SetLastError=true, CharSet=CharSet.Auto, BestFitMapping=false)]
>        [ResourceExposure(ResourceScope.Machine)]
>        internal static extern bool CopyFile(
>                    String src, String dst, bool failIfExists); 
>
Previous
Reply
Map
View

Click here to load this message in the networking platform