Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
File.Copy and lock file
Message
De
19/02/2014 03:42:57
 
 
À
19/02/2014 02:51:46
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01594636
Message ID:
01594643
Vues:
39
>>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

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); 
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform