'Alternative to File.WriteAllText? [closed]
My app has to write a text into a file thousands of time per second using File.WriteAllText. There is less than 10 bytes each time (a number string to be exact). I test my app by ending its process using Task Manager. Some time I get a empty file.
I think the function empty the file before writing to it. And the process has been ended in the middle.
Which is better, reliable way to save a small text into a file?
long ServerSequence
{
get
{
lock (_serverSeqFlag)
{
return _serverSequence;
}
}
set
{
lock (_serverSeqFlag)
{
_serverSequence = value;
File.WriteAllText(_DayOutputPath + "server.seq", _serverSequence.ToString());
}
}
}
Solution 1:[1]
File is not a good choice for high frequency reading and writing. It does not make sense to write a file thousands of times per second. Other processes hardly can read it as the file is occupied by your writing process. Why not use database? Or You can place the data in a variable in the memory and write it into the file when the process ends. Redis maybe a good choice for your scenario.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dream Land |
