Beispiel #1
0
        private static int?LoadFileLockViolationHResult()
        {
            if (Constants.WINDOWS)
            {
                return(WIN_HRESULT_FILE_LOCK_VIOLATION);
            }

            // Skip provoking the exception unless we know we will use the value
            if (IS_FILESTREAM_LOCKING_PLATFORM)
            {
                return(FileSupport.GetFileIOExceptionHResult(provokeException: (fileName) =>
                {
                    using (var lockStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
                    {
                        lockStream.Lock(0, 1); // Create an exclusive lock
                        using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Write, FileShare.ReadWrite))
                        {
                            // try to find out if the file is locked by writing a byte. Note that we need to flush the stream to find out.
                            stream.WriteByte(0);
                            stream.Flush();   // this *may* throw an IOException if the file is locked, but...
                                              // ... closing the stream is the real test
                        }
                    }
                }));
            }

            return(null);
        }
Beispiel #2
0
        private static int?LoadFileShareViolationHResult()
        {
            if (Constants.WINDOWS)
            {
                return(WIN_HRESULT_FILE_SHARE_VIOLATION);
            }

            return(FileSupport.GetFileIOExceptionHResult(provokeException: (fileName) =>
            {
                using (var lockStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read, 1, FileOptions.None))
                    // Try to get an exclusive lock on the file - this should throw an IOException with the current platform's HResult value for FileShare violation
                    using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Write, FileShare.None, 1, FileOptions.None))
                    {
                    }
            }));
        }