Cообщество разработчиков на платформе Microsoft > Форумы > Visual C# General > How to read files that are exclusively locked by other applications?
Задайте вопросЗадайте вопрос
 

ОтвеченоHow to read files that are exclusively locked by other applications?

  • 2 мая 2008 г. 6:11John DeSouza Медали пользователяМедали пользователяМедали пользователяМедали пользователяМедали пользователя
     С кодом

    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

    • ИзмененоJohn DeSouza 3 июня 2008 г. 9:48Added snipped block
    •  

Ответы

Все ответы