File.Open throws Invalid handle excpetion

Feb 13, 2014 at 9:08 PM
I'm trying to open a FileStream to a log file that already exists, but I am getting an "Invalid handle" exception. I am using a wrapper class which allows me to switch between .NET and AlphaFS code. When I switch to using .NET, everything works. When I switch to using AlphaFS, I get the exception.

I've tried using FileAccess.ReadWrite, but the results are the same. Any suggestions on how to fix this?

The file name is: C:\Users\Public\Desktop\Test\Logs\Test.log

Here's my code:
       public static FileStream Open(string path, FileMode mode, FileAccess access = FileAccess.ReadWrite, FileShare share = FileShare.None)
            if (LongPath.UseDotNet)
                return File.Open(path, mode, access, share);
                return Alphaleonis.Win32.Filesystem.File.Open(path, mode, access, share);

        // Open the log file.
        FileStream fileStream = LongFile.Open(this.FilePath, FileMode.Append, FileAccess.Write, FileShare.Read);
Here's the stack trace:
   at Alphaleonis.Win32.Filesystem.NativeMethods.IsValidHandle(SafeHandle handle, Boolean raiseException) in C:\Data\Test\Projects\SDK\AlphaFS\Latest\Development\AlphaFS\Filesystem\NativeMethods.cs:line 170
   at Alphaleonis.Win32.Filesystem.FileSystemInfo.CreateFileInternal(Nullable`1 isFile, KernelTransaction transaction, String path, EFileAttributes attributes, FileSecurity fileSecurity, FileMode mode, FileSystemRights rights, FileShare share) in C:\Data\Test\Projects\SDK\AlphaFS\Latest\Development\AlphaFS\Filesystem\FileSystemInfo.cs:line 355
   at Alphaleonis.Win32.Filesystem.File.OpenInternal(KernelTransaction transaction, String path, FileMode mode, FileSystemRights rights, FileAccess access, FileShare share, EFileAttributes attributes) in C:\Data\Test\Projects\SDK\AlphaFS\Latest\Development\AlphaFS\Filesystem\File.cs:line 3052
   at Alphaleonis.Win32.Filesystem.File.Open(String path, FileMode mode, FileAccess access, FileShare share) in C:\Data\Test\Projects\SDK\AlphaFS\Latest\Development\AlphaFS\Filesystem\File.cs:line 854
   at Test.IO.LongFile.Open(String path, FileMode mode, FileAccess access, FileShare share) in C:\Data\Test\Projects\Common\TestCommon\IO\LongFile.cs:line 136
   at Test.Logging.LogFile.OpenCreateFile() in C:\Data\Test\Projects\Common\TestCommon\Logging\LogFile.cs:line 547
Feb 13, 2014 at 9:24 PM
Edited Feb 13, 2014 at 9:28 PM
I was using 1.5, but just tried the latest (1.6)...same exception. If I delete the file first and using the following code, I do not get an exception:
FileStream fileStream = LongFile.Open(this.FilePath, FileMode.CreateNew, FileAccess.Write, FileShare.Read);
Feb 14, 2014 at 10:38 AM

You're trying to open an existing log file using: public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share)
which eventually throws an invalid handle, right?

Down the road, the static method: FileSystemInfo.CreateFileInternal() is called.
Could place a breakpoint on the line: int lastError = Marshal.GetLastWin32Error(); to see which Win32 error that would be?

Feb 14, 2014 at 11:00 AM
Feb 14, 2014 at 4:18 PM
I also get the same exception when calling File.OpenRead(path).
Feb 18, 2014 at 7:23 PM
This issue seems to be the result of my app opening a file with FileShare.Read and later opening it with FileAccess.ReadWrite which caused a sharing violation. After clearing this up, I do not see the above errors occurring.
Marked as answer by Yomodo on 2/19/2014 at 2:18 AM
Feb 18, 2014 at 7:54 PM
Edited Feb 19, 2014 at 9:24 AM

I've modified the code to return: ERROR_SHARING_VIOLATION exception instead of Invalid Handle
for clarity.

So, your issue is now resolved?
Feb 18, 2014 at 10:14 PM
Yes. I found the sharing violation using ProcMon and was able to find the issue in my code which caused it. Now that it is fixed, I do not get the invalid handle exceptions. Your fix should make this process much quicker.