Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C#” How to read text file without locking it, so other application could write in it.

Posted on 2013-06-11
4
Medium Priority
?
1,625 Views
Last Modified: 2013-06-11
Dear experts

I wrote a Windows Form Application which should read a log text file, while it is being written from other application ¿ This other application in every 10-100 sec. add a row to this text file. I have no idea what access mode this other application use with the file, but I guess it open the file with add mode, wrote new lines info and then close the file open pointer. It is on C++ or Deplhi

 So I’m wondering what is my best tactics? I guess it is better I to read all the file at once, to close it and then to start cycle between its line and add my logic.

 Or it will be better first to copy the file on other direction on HDD and then to read this copied file?

 The text file I should read is with 200-2000 lines, not so good as every day it create a new one.

Second question – I plan to use StreamReader , this is the right choice? I need .NET library which read the text files without being locked it at all. I wrote in my DB which is the last row I have been read, so my only problem is to NOT block the other application access.
0
Comment
Question by:dvplayltd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 2000 total points
ID: 39236932
since u need the last lines written to the file u might want to check ReverseLineReader implementation here:
http://stackoverflow.com/questions/452902/how-to-read-a-text-file-reversely-with-iterator-in-c-sharp
0
 
LVL 15

Expert Comment

by:Minh Võ Công
ID: 39236987
using (var fileStream = new FileStream("foo.bar", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
using (var textReader = new StreamReader(fileStream))
{
    var content = textReader.ReadToEnd();
}

http://stackoverflow.com/questions/3448230/how-can-i-read-a-text-file-without-locking-it
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39237028
Have you actually tried to read it?  If it is failing maybe it is the other app attempts to use an exclusive access to the file - which means the other app is coded so what you want would never work.
0
 

Author Closing Comment

by:dvplayltd
ID: 39237234
10x
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question