How to detect real-time change of text files? - c#

I am stating to write a little PC tool to read log files using c# or java. The log files will be in .txt format. An application is running and writing logs, and I want my tool to open the log at the same time and refresh automatically when a new line is written to the log file.
My challenge is, how do I detect the log file changes so that my tool will have real-time displaying ability? This is a general question but pseudo codes will be greatly appreciated!

You can use the FileSystemWatcher class (MSDN page). Be careful though, if you try to open the file while the other process is writing the file you will probably be denied access.

There's a bunch of this questions here, and answers too:
Reading file content changes in .NET
Your target is FileStream object, I suppose.

I would poll the file and check it's metadata for the last changed date, or if you're using .NET, you could use the FileSystemWatcher class

Related

How to read a .sil (SmartInspect file) log file using c#

I am currently using SmartInspect logging tool in my c# application, and now I am able to get the log files, and the log file is having extension ".sil" (ex:log-2013-04-04-08-22-05.sil"). Now my requirement is I need to read this log file from my c# application. I have tried all file reading concepts to read the log file but it's not working . Please suggests me a code how I will read the .sil file from a c# application.
When I am copying the file contents from SmartInspect logging tool and pasting in a normal txt file then it is working to get the file contents, but I am not able to read the .sil file using c# application.Please help...
You need to implement your own Log file parser. The format of log file is documented here SmartInspect Log Formats and Protocols
SmartInspect uses its own format for ErrorLogging, as i noticed, i does not use XML or Plain Text
A typical *.sil file starts like
You can use a free Redistributable Console to read the file.
One of the SmartInspect developers here. We also have a ready-to-use SDK for .NET that allows you to read and process SmartInspect log files:
http://www.gurock.com/smartinspect/extras/

Reading an STG file created from MS ActiveSync

How can I parse an STG (Microsoft ActiveSync Mobile Device Backup) file (created by ActiveSync)?
I have an old .stg file that was created from backing up an old Windows Mobile device, and I would like to write a program to read it using C#. I have tried a few things but can't get anything but garbage when I read through it. From what I can find online it is stored in Unicode format but that's about it, any posts I find talking about it are ancient and all the links are dead.
You can open ActiveSync .stg files in an archiver tool such as 7-Zip or PeaZip. If the program does not recognize the file as an archive, you may need to rename the file to have an archive extension (e.g., .7z, .gz, .tar).
With PeaZip is also possible to try to open any file extension from "Open as archive" menu entry (also available in system's context menu), without changing the file's extension.
With many format being basically variations of Deflate-compressed containers, it is worth trying.
Anyway, some formats may introduce proprietary fields (e.g. extra checksums, comments, digital signing, etc) which are out of the scope for a general-purpose archiver as PeaZip or 7-Zip.
So, while it is harmless to try to read container files as archives, it is not adviceable to try to edit them as such.

Edit a .gziped log text file in C#

I receive logs which are .gz (zipped) and contain text files.
One field in the logs needs to be edited, present on every row, to contain some other data then what is currently present in the logs.
My thinking so far is to:
Unzip file
Read it
Edit it
Write it
Rezip it
But I guess there is a better way to do this, is there any on-the-fly reading/editing from .gz log files available in C#?
Thanks!
Unless you want to work at the bit-level, the method you suggest is the correct approach.
incase you are unfamiliar with the .Net libraries for this, here is a code project article.
http://www.codeproject.com/KB/files/GZipStream.aspx

Need to open file I log to but get file locked exception

hI,
I'm using LOG4NET to log to a txt file. However, one of my forms needs to open the txt file (using streamreader) to read the contents and display them in a .txt file. However, I keep getting an exception stating that the file is locked by another process.
Is there a way around this in LOG4NET?
Thanks
This may be help you.
http://www.ericbt.com/Blog/48
If you open the log file with ReadWrite sharing enabled (FileShare.ReadWrite) it will probably work.
You can always uses Process Monitor from http://www.sysinternals.com to see what is happening.

Write to file that is open in Excel

Suppose I have a program running that periodically adds information to a .CSV file. Is there a way to write to the file while it is already open in Excel? Obviously the changes wouldn't be noticed until the file was re-opened in Excel, but as it stands right now, I'm catching IOException and just starting a new .csv file if the current one is already open.
Excel seems to open the file in exclusive mode, so the only way I can think of would be to write your changes to a temporary file, and then use FileSystemWatcher to see when the file is closed, and overwrite the file.
Not a very good idea, as you could lose data. The whole reason excel locks the file is so that you don't accidentally overwrite changes made in excel.
It sounds like the file is locked. I doubt you will be able to write to that file if it is open in another process.
As a former (and sort of current) VB Programmer, I can tell you Jared is correct - there is no way to do this directly. You can try to copy the file first, make your edits, then attempt to save the file back to its original location until the locked file becomes free. You should be able to copy that file, even while locked.
What about using Excel's object model and automating the addition of the data into the open spreadsheet? You'd probably need to prompt the user somehow to let them know what was happening.

Categories