none
Read a log file that is being written by an uncontrollable application RRS feed

  • Question

  • Dear All,

    My app will read a log file that is being written by another program in which i do not have control at all with by the following picece of code:

    using (FileStream fs = fi.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    {
    	using (StreamReader sr = new StreamReader(fs))
            {		
    		string value = string.Empty;		
    		while ((value = sr.ReadLine())!= null)                
    		{                	
    			// add the value into an ArrayList object
            	}
    	}
    }

    with above approach, would it impact the application that writes data into that particular log file.

    Many thanks.

     

    Tuesday, March 26, 2013 4:19 AM

Answers

  • I think you don't have another choice, even though you use FileShare.

    The following FileStream constructor opens an existing file and grants read-only access to other users (Read).

    FileStream s2 = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.Read);

    If you want to use share mode, the main program should use FileShare.Read mode to share you open this file.

    If not, you can not open it.

    The other method is ,you copy that file by rename,to read it


    If this is helpful
      { Please Mark as Answered }
    else if it is not helpful
      { Un-Mark as Answered }


    Best Regards
    Huan Li, Code Blog: Small is New Big In C#

    Wednesday, March 27, 2013 1:01 AM

All replies

  • If you just want to read then open the file in read only mode, would be better

    Visit my sites: http://www.umair.me AND http://www.microsoftarena.net

    Tuesday, March 26, 2013 11:10 PM
  • If you want read this file by another program, the another program should close FileStream

    FileStream fs1 = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write);
    fs1.write() // fs1 writes file
    fs1.closer();  // if you don't closer , the another program can not open this file


    If this is helpful
      { Please Mark as Answered }
    else if it is not helpful
      { Un-Mark as Answered }


    Best Regards
    Huan Li, Code Blog: Small is New Big In C#

    Wednesday, March 27, 2013 12:31 AM
  • it would throw "the file is used by another program" error if open by FileShare.Read.

    Wednesday, March 27, 2013 12:37 AM
  • as i said in my post, I DO NOT have control, whatsoever, with the other applications that access those log files. Hence, your suggestion wouldnt applicable in my situation.
    Wednesday, March 27, 2013 12:37 AM
  • I think you don't have another choice, even though you use FileShare.

    The following FileStream constructor opens an existing file and grants read-only access to other users (Read).

    FileStream s2 = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.Read);

    If you want to use share mode, the main program should use FileShare.Read mode to share you open this file.

    If not, you can not open it.

    The other method is ,you copy that file by rename,to read it


    If this is helpful
      { Please Mark as Answered }
    else if it is not helpful
      { Un-Mark as Answered }


    Best Regards
    Huan Li, Code Blog: Small is New Big In C#

    Wednesday, March 27, 2013 1:01 AM
  • Depending on how this application handle writing process, you can read it directly from memory or from temporary created file - but it will not be easy... Anyways, what is the purpose..?

    Classified SR-2 | 2x Xeon W5580 - 3.20 GHz | 12x 2GB Kingston KHX2000C9D3T1K3/6GX | 2x MARS II/2DIS/3GD5 | SAMSUNG 830 MZ-7PC512D/AM 2.5" 512GB SATA III MLC | 4x Spinpoint F3EG HD503HI 500GB 5400 16MB SATA 3.0Gb/s |

    Wednesday, March 27, 2013 1:07 AM
  • thanks a lot, but i think you miss the point.

    I am able to read the log file that currently is being writtine by an uncontrolled application without any issue by using the above code.

    what i want to know is would it impacts the application that writes data to log file, e.g. that application could be crashed etc...

    Wednesday, March 27, 2013 1:07 AM
  • Hi AndieDu,

    "what i want to know is would it impacts the application that writes data to log file"

    AFAIK it wouldn't.


    Bob Shen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, April 1, 2013 10:25 AM
    Moderator