Reading from File!

Hi,

I'm facing a small problem in reading data from a file. I need to read data from the file based on the following conditions:

(1) If the file is not opened (by any application), read it;
(2) If the file is opened (by any application) but not in writing mode, read it;
(3) If the file is opened and is in saving/writing mode, don't read it.

Plz tell me how can I implement this. It would also be great if you could provide me with the code in C#.

Thanks & Regards,

Frenz



Answer this question

Reading from File!

  • XxxFG

    Thanks a lot dude! Your simply best!

    Frenz


  • Gaurav Sehgal

    It sounds like you allways want to read the file when posible:


    public string ReadFile( string filename )
    {
    try
    {
    using(FileStream fileStream = new FileStream( filename, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
    {
    StreamReader reader = new StreamReader( fileStream );
    return reader.ReadToEnd();
    }
    }
    catch( IOException caught )
    {
    // Return null because the file could not be opened.
    return null;
    }
    }




  • Reading from File!