0

I'm writing my filesystem using C# and Dokan library and got stuck on WriteFile.

 public int WriteFile(
            String filename,
            Byte[] buffer,
            ref uint writtenBytes,
            long offset,
            DokanFileInfo info)
        {           
            try
            {
                FileStream fs = File.OpenWrite( GetFullPath(filename) , FileMode.Open );              
                fs.Seek(offset, SeekOrigin.Begin);
                fs.Lock(offset, buffer.Length);
                fs.Write(buffer, 0, buffer.Length);
                fs.Unlock(offset, buffer.Length);
                writtenBytes = (uint)buffer.Length;
                return 0;
            }
            catch (Exception e)
            {
                Console.WriteLine("FAIL WriteFile {0}", e.Message);
                return -1;
            }
        }

When I run app and open txt file from Dokan's virtual drive, add some line and try to save it. I get error from notepad "The parameter is incorect".

In code I get exception on File.OpenWrite() call. The exception is System.IO.IOException. Message: "The process cannot access the file foo.txt because it is being used by another process."

  • File is opened only by notepad.
  • Same behavior can be observed with Mirror example delivered with Dokan library
  • I added admin permissions to my program in the manifest, it didn't help

Dokan is supposed to work as proxy, allowing to call WriteFile defined by user, right? How can I do this if it's locked for writing?

Please help. Maybe you have any experience with Dokan or any clue why it's not working.

I'm using - Win 7 Pro x64 - 64 bit Dokan driver - App is compiled as x86

WannabeCoder
  • 77
  • 2
  • 7
  • Is the driver compiled for 32-bit, 64-bit or AnyCPU (for both your code and Dokan)? What bitness is the PC you're testing on? – M.Babcock Feb 18 '12 at 01:42
  • 1
    Maybe I'm misreading, but it looks like Notepad asks Windows to save to Y:\text.txt, Windows forwards it to Dokan, Dokan forwards it to you, you pass it back to Windows with the same path? If you want to proxy an existing file, shouldn't you make sure you use a different path? –  Feb 18 '12 at 01:44
  • hvd I don't know if windows gets it from VirtualDriveLetter:\foo.txt or from actual path. Filename I get in WriteFile is "\foo.txt" then I add full path to it ActualDriveLetter:\path\file and try to open it with File.OpenWrite – WannabeCoder Feb 18 '12 at 02:08

1 Answers1

1

You must call Dispose on stream before leaving the method. That's not the best solution. You shuld construct file stream in CreateFile , pass it to info.Context and the use it in ReadFile , WriteFile and call dispose on it in Cleanup.

user629926
  • 1,910
  • 2
  • 18
  • 43
  • I tried your solution. It works, but.. The but is I can open text file in notepad, do some changes, close notepad and save those changes. But when I do it second time notepad says "Invalid parameter". The issue is I handled disposing FileSteam in CleanUp only, when I do Dispose in both CleanUp and CloseFile everything seems to work just fine. Please tell me what is the right way to do close file. Should the same code for disposing FileStream be in both CleanUp and CloseFile? Which one gets called first. Thank in advance! – WannabeCoder Feb 22 '12 at 22:48
  • CloseFile sometimes doesn't get called but Cleanup is always take an look here http://liquesce.codeplex.com/SourceControl/changeset/view/73752 – user629926 Feb 26 '12 at 14:51