This question already has answers here:
Should log file streams be opened/closed on each write or kept open during a desktop application's lifetime?
(13 answers)
Closed 6 years ago.
i need to write data into a CSV file each 600 msec using a c# application . The question: is better open and close file each time or keep it open until the end of write data actions? Note: i will change file name each day and each 60000 record
Thanck a lot for your opinions
CSV files are really easy to write to. If you don't know how to write to a file, the dotnetperls is your friend. You can simply call BinaryWriter.Write() to write anything. Write a value then a comma. That's it! If this file is going to be edited by the user at the time of running the application, then don't keep it open. Otherwise, keeping it open makes sure nothing unexpected happens.
Related
This question already has answers here:
How to ensure all data has been physically written to disk?
(7 answers)
Closed 6 years ago.
I am developing a windows service in C# which will likely write several times per second to a log. I am currently using System.IO.File.AppendAllText(time + message) for each operation and I am concerned so many writes may wear out the disk quickly.
Could this be a problem? Should I use a stream and flush every few seconds? Does the windows disk buffer protect against this?
Why you want to reinvent the wheel?
Better use some ready framework eg. Log4Net, NLog, SeriLog.
"Should I use a stream and flush every few seconds?"
What if your app crash with no flush stream? Log go away and you don't know what happens.
This question already has answers here:
Intercept windows open file
(4 answers)
Closed 7 years ago.
Is there an easy-to-implement method to detect whenever a user opens a file?(double click, right click, etc.)
I've read this but I think it only polls the file's lastaccess time. The main goal that I'm trying to achieve is whenever a user opens a file, the code picks up file name, location, size and all that good stuff.
I don't think I can ask this in any other way. I'm at a loss as to where to start.
Actually, the FileSystemWatcher from your linked question is the correct way to go. It does NOT poll anything, it listens to the OS events for file access. If there is no OS event, you cannot react to it.
This question already has answers here:
Read last line of text file
(6 answers)
Closed 8 years ago.
Scenario is the following:
A (weather) service dumps sensor data into a log file/text file.
The new readings are appended to the bottom of a given (existing) file
New data is added at regular intervals (interval may or may not be known)
I need to parse the new information/line and send it off to another service.
I don't want to read the whole file every time, unless I have to.
EDIT: Sorry for the bad wording. "unless I have to" should be understood as if there is no other way around. I have seen the post/answer referenced and it seems a little extensive.
Framework is 4.5.x.
Thank you.
To get the the last line of a text file you can use this
File.ReadLines(myFileName).Last();
This is the simplest method, but is inefficient. You can write your own parser as show here
This question already has an answer here:
How to detect a process start & end using c# in windows?
(1 answer)
Closed 8 years ago.
I am looking for a way to record when certain programs have been executed.
For instance, if Microsoft Word has been started I would like to write out a time and date stamp along with the program name.
The output I get. I may change it to an Excel spreadsheet. Just need a little guidance on where to look to capture user run programs.
It looks like you want to use the ManagementEventWatcher
example
msdn
As for creating an excel spreadsheet from data, you can easily find code for that.
like this
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is XMLDocument.Save an atomic operation?
I appear to have a Heisenbug in my program. I need to export an Xml file and then re-import it. The problem is that sometimes before the Xml file has finished saving the program will continue to the next line to try and re-import it and because it is not finished saving the application will crash. When I debug it there is enough time for the file to save so the program doesn't crash.
I could use Thread.Sleep but on slower computers it might still crash if it took too long.
I'm using XmlDocument.Save to save the file. I was wondering if there might be a way to implement a callback of some sort for when the file has been completely saved.
Suspect antivirus. Many virus scanners aren't written well and lock the file when you close it, preventing any program from opening it for a few seconds.
See this question:
Is it possible to reasonably workaround an antivirus scanning the working directory?