3
How to block the opening, reading and editing of a file by the user during the period the application is running?
An application has a relatively long data collection period. This application reads data in real time. The data read is recorded in file every minute. At the end of the data collection, I must release the file to the user. Use the code below to save the data every minute. The file attributes Hidden and read only don’t help me!
string diret=saveFileDialog1.FileName;
Encoding sjisX=Encoding.GetEncoding("Shift_JIS");
StreamWriter arquivo=new StreamWriter(diret,true,sjisX);
arquivo.Write(tb_csv.Text); //salva os dados.
arquivo.Close(); // fecha o arquivo.
//fileProtec.Attributes=FileAttributes.Hidden; permite sua abertura, logo, não serve!
But when you give this writer command. Close(); releases the file ?
– silvio pontes
But, when you give this writer command. Close(); releases the file ? Every minute, will the app open the file, save and close? With the code I currently use; in the save interval the file can be opened. I need the file to remain closed until the end of the data collection and when finished release the file. Another thing, I need a contingency plan. If the program crashes or if the computer is disconnected from power how to access this file?
– silvio pontes
@silviopontes yes, when using the method
Close
the file is released. I think I understand what you want now. you will never get it if you keep working with files this way. One suggestion is to write the data in a database and generate the file only at the end of the data collection.– Oralista de Sistemas
I am doing this, but if the computer crashes or power goes out I lose the data.
– silvio pontes
@silviopontes with a database this would not happen.
– Oralista de Sistemas
Renan, how to use this solution you say of database? Something else; if the machine crashes how I recover the data and convert to csv?
– silvio pontes
@silviopontes this is too big a subject for comments, so I’ll just say that you should use Mysql or Postgre (both free) or MSSQL (the version Express is free). If you need to worry about crashing machines then it has already gotten bad - your application should be in the cloud if it is so sensitive - and to convert to CSV you use the same program with which you collect data (the conversion is on your own).
– Oralista de Sistemas
Grateful, really helped a lot.
– silvio pontes