for an application that uses the file as a kind of global storage for backing up devices in the firm. I need a way to read and write to a file (or lock the file, read it, write to it and unlock it). A small piece of code will shoot, which I mean:
FileStream in = new FileStream("storage.bin", FileMode.Open); //read the file in.Close(); //!!!!! //here is the critical section since between reading and writing, there shouldnt //be a way for another process to access and lock the file, but there is the chance //because the in stream is closed //!!!!! FileStream out = new FileStream("storage.bin", FileMode.Create); //write data to file out.Close();
it should get something like this
LockFile("storage.bin"); //read from it... //OVERwrite it.... UnlockFile("storage.bin");
the method must be absolutely safe, since the program must run on 2000 devices at the same time
source share