hey guys, I've been trying to find a way to read a .txt file in real time. I've developed a client/server console app program in Visual Studio 2010. I can open and send txt files to my sever program with my client program (tcp, netstream). However, the file that I am calling is constantly being updated with new data. I want to sent that updated data straight to my File Sharing Watcher function but this merely tells me if my file or directory has changed not the actual string of data. Does anyone know of any other function or way of achieving this?
It would help to know how the file may be changed. If data is being appended then it is simple just keep track of the filesize and read to the end starting at the position of the size of the file at the last read. If data is being changed within the file then it is a bit more complicated.
Your server program could write each incoming block of text to a new file. Eg: 1.txt, 2.txt, 3.txt (or even get the system to generate a temp file name for you, so you don't have to keep a counter). Then your FileSystemWatcher could just look for files being created. These files will just have the new text and of course they can be deleted afterwards or appended to a main file and deleted.
I've done some work with FileSystemWatcher in the past and when opening one of these new files, you may find that it is still locked by the process writing to it and have to catch the exception (only way to do this) wait a second or 2 and then open it again.
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
I appreciate the replies. DataMiser, I basically want to mimic the function of the Tail application. I am working with sensor readings. These readings are sent to a file in intervals of 8 seconds. My client program reads the .txt file with these sensor readings and sends them to my server. Therefore, I want my program to read the .txt file until the end and wait for the next string to be added. rliq, I understand what you are saying about writing new data to a temp file. However, FileSystemWatcher would not be able to tell me the exact string that was updated right?
So new data will always be appended to the end of the file which makes it fairly simple to know what was written.
Basically what you could do is read the file initially and store the size of the file at that point.
When the file changes look at the stored file size then read the file starting at that location to the end of the file. Which will in effect be reading only the new data. Update your variable to reflect the new file size as of this read.
I would use a filestream and binaryreader to do this since it is simple to read based on byte position and number of bytes to read but there are other options.
If you write each block of data to a different temp file each time, then the whole of its contents is the string that was written. Then append the whole of that text to the end of another file and delete the temp file. It's not quite the UNIX "tail -f" command, but it may serve your purpose.
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
Thank you both for your suggestions, they have helped me big time. Yes Datamiser, the file is actually being written by another program. The sensor readings I was telling you guys about actually send the data to the computer via serial communication. Therefore, I figured it would be easier to develop a COM port listener and have my client program read that data as the event happens. I'm having some issues with my COM port listener. Could you help? Thanks.
Ok so as I mentioned before, I have some sensors sending data to a file. I want to listen to the port directly (COM 4). I set all the parameters and when I try to listen for incoming data, my application shuts down. It doesn't listen at all. Its a small program. I have attached my entire COM listener program. I know the problem is in the part where I receive data, I just dont know why? it seems like everyone is using the DataReceive Method.
I have not tried writing this kind of app as a console app and I am far from an expert in C# but I do not see any thing in your program to keep it from just dropping through main and exiting.
Am I missing something that allows the program to stay running?
Normally I would do this with a windows forms app and the form being open would stop the project from closing while I wait for events on the port.
Im new at C# as well. I'll try doing this same project with a windows form app. I am assuming the program will keep running or listening since I open the port and never close it. It's actually what I what, but Im not sure if I still need that (close port) to have a working program. Thanks for your insight. I appreciate it.
In a forms app the program keeps running until you close the form. The port will keep receiving data so long as the port is open and data is coming into the port.
How are you intercepting the data from the port? Are you tied into the cable with a splitter or something so you can get the data and the other program still get it as well?
So the sensors are transmitting to a device and this device is connected to my pc via rs 232. The data that is being transmitted by this device is in the for of strings. I have a little third party terminal app that listens to the COM port and it is receiving data so I know its transmitting. My app is the one not receiving anything.
I was wondering how you are connected. Have you disabled the other program and now using yours to monitor the port or is the other program still running at the same time? Have you converted to a forms app and still not receive anything? Have you set the receive threshold to a value >0 so as to trigger the data received event when data arrives?
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.