• Upgrade your Internet Experience
  • Sign in
  • Microsoft.com
  • United States (English)
    Brasil (Português)Česká republika (Čeština)Deutschland (Deutsch)España (Español)France (Français)Italia (Italiano)Россия (Русский)대한민국 (한국어)中华人民共和国 (中文)台灣 (中文)日本 (日本語)香港特别行政區 (中文)
 
 
Visual C# Developer Center
 
 
Home
 
 
Library
 
 
Learn
 
 
Downloads
 
 
Support
 
 
Community
 
 
Forums
 
 
 
Visual C# Developer Center > Visual C# Forums > Visual C# General > How to read files that are exclusively locked by other applications?
Ask a questionAsk a question
Search Forums:
  • Search Visual C# General Forum Search Visual C# General Forum
  • Search All Visual C# Forums Search All Visual C# Forums
  • Search All MSDN Forums Search All MSDN Forums
 

AnswerHow to read files that are exclusively locked by other applications?

  • Friday, May 02, 2008 6:11 AMJohn DeSouza Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Vote As Helpful
    0

    Hi,

    I am trying to read log files that are updated by a daemon process on the server. The daemon process is an COM application writing to the files exclusively. How can I read the files? To simulate the workflow, I have created the following snippet, which reproduces the problem I am facing.

    I get the exception : "The process cannot access the file 'C:\\Temp\\FileLockTest1.txt' because it is being used by another process."

     

     

    static void Main(string[] args)  
     
    {  
     
    FileStream exclusiveWriter = new FileStream(@"C:\Temp\FileLockTest1.txt", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);  
     
    for (byte counter = 0; counter < 100; counter++)  
     
    {  
     
    exclusiveWriter.WriteByte(counter);  
     
    }  
     
    FileStream sharedReader = new FileStream(@"C:\Temp\FileLockTest1.txt", FileMode.Open, FileAccess.Read, FileShare.Read);  
     
    sharedReader.Seek(0, SeekOrigin.Begin);  
     
    for (byte counter = 0; counter < 100; counter++)  
     
    {  
     
    Console.WriteLine(sharedReader.ReadByte());  
     
    }  
     
    exclusiveWriter.Close();  
     
    sharedReader.Close();  
     
    Console.ReadLine();  
     
    }  
     

     

    Thanks in advance,

    - Johnson

    • Edited byJohn DeSouza Tuesday, June 03, 2008 9:48 AMAdded snipped block
    •  
    • ReplyReply
    • QuoteQuote
     

Answers

  • Friday, May 02, 2008 4:34 PMShellShock Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Vote As Helpful
    0

    I can only see two options:

     

    1 Get the daemon process code changed so it allows read access. This assumes you have access to the source code, or you can get it changed. Why do people write logging code that exclusive locks the log file? This is very bad practice! Maybe they don't want anyone to ever look at the log?

     

    2. Copy the file as you are already doing.

    • ReplyReply
    • QuoteQuote
     

All Replies

  • Friday, May 02, 2008 7:12 AMEyal-Shilony Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
     John DeSouza wrote:

    Hi,

    I am trying to read log files that are updated by a daemon process on the server. The daemon process is an COM application writing to the files exclusively. How can I read the files? To simulate the workflow, I have created the following snippet, which reproduces the problem I am facing.

    I get the exception : "The process cannot access the file 'C:\\Temp\\FileLockTest1.txt' because it is being used by another process."

     

    static void Main(string[] args)

    {

    FileStream exclusiveWriter = new FileStream(@"C:\Temp\FileLockTest1.txt", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);

    for (byte counter = 0; counter < 100; counter++)

    {

    exclusiveWriter.WriteByte(counter);

    }

    FileStream sharedReader = new FileStream(@"C:\Temp\FileLockTest1.txt", FileMode.Open, FileAccess.Read, FileShare.Read);

    sharedReader.Seek(0, SeekOrigin.Begin);

    for (byte counter = 0; counter < 100; counter++)

    {

    Console.WriteLine(sharedReader.ReadByte());

    }

    exclusiveWriter.Close();

    sharedReader.Close();

    Console.ReadLine();

    }

     

    Thanks in advance,

    - Johnson

    Try to change FileShare.Read to FileShare.ReadWrite .

     

    Best Regards.

    • ReplyReply
    • QuoteQuote
     
  • Friday, May 02, 2008 8:23 AMJohn DeSouza Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

     

    Hi,

    Thanks for your reply. I tried changing the FileShare to "ReadWrite". But am still getting the same exception!!

     

    • ReplyReply
    • QuoteQuote
     
  • Friday, May 02, 2008 8:49 AMSasha GoldshteinMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    Of course you are getting the same exception.  If a process opens a file and specifies FileShare.None (which is equivalent to FILE_SHARE_NONE in Win32), then there is no documented way to access the file from another process.  That's the whole idea of the file share enumeration.
    • ReplyReply
    • QuoteQuote
     
  • Friday, May 02, 2008 10:08 AMJohn DeSouza Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    This is an important business requirement and I've got to implement it anyhow. Is there any "undocumented" way or any workaround to get this done?

    Currently I copy the file to a temp location and read it from there. Not the most elegant solution I know. Any help would be appreciated.

     

    Thanks in advance.

     

     

    • ReplyReply
    • QuoteQuote
     
  • Saturday, May 03, 2008 10:25 PMEyal-Shilony Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
     ShellShock wrote:

    I can only see two options:

     

    1 Get the daemon process code changed so it allows read access. This assumes you have access to the source code, or you can get it changed. Why do people write logging code that exclusive locks the log file? This is very bad practice! Maybe they don't want anyone to ever look at the log?

     

    2. Copy the file as you are already doing.

     

    1, Not really a bad practice, it might be a requirement to one of the process operations.

     

    2. Not the most elegant solution as the OP said but it should work. Smile

    • ReplyReply
    • QuoteQuote
     
  • Friday, May 09, 2008 8:29 AMJohn DeSouza Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    Hmm.. I have done it by copying the file locally and works fine for now.

    Thank you all for your time.

     

    • ReplyReply
    • QuoteQuote
     
  • Friday, May 09, 2008 8:29 AMJohn DeSouza Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    Hmm.. I have done it by copying the file locally and works fine for now.

    Thank you all for your time.

     

    • ReplyReply
    • QuoteQuote
     
Need Help with Forums? (FAQ)
 
© 2009 Microsoft Corporation. All rights reserved.
Terms of Use
|
Trademarks
|
Privacy Statement