Reading and writing to a file at the same time

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

+6
source share
5 answers

Just holding FileStream open with exclusive (not shared) access will prevent other files from accessing the file. This is the default value when opening a file for recording / recording access.

You can overwrite the file you are currently holding open by trimming it.

So:

 using (var file = File.Open("storage.bin", FileMode.Open)) { // read from the file file.SetLength(0); // truncate the file // write to the file } 

the method must be absolutely safe, since the program must run on 2000 devices at the same time

Depending on how often you write the file, this may become a chokepoint. You will probably want to check this out to see how scalable it is.

In addition, if one of the processes tries to work with the file at the same time as the other, an IOException will be IOException . There really is no way to “wait” in a file, so you probably want to coordinate access to files in a more orderly manner.

+9
source

You need one stream open for reading and writing.

 FileStream fileStream = new FileStream( @"c:\words.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None); 

Alternatively you can also try

 static void Main(string[] args) { var text = File.ReadAllText(@"C:\words.txt"); File.WriteAllText(@"C:\words.txt", text + "DERP"); } 

By http://msdn.microsoft.com/en-us/library/system.io.fileshare(v=vs.71).aspx

 FileStream s2 = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.None); 

You need to pass the value of the FileShare None enum to open the FileStream constructor in overloads:

 fs = new FileStream(@"C:\Users\Juan Luis\Desktop\corte.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.None); 
+2
source

You are probably looking for FileStream.Lock and FileStream.Unlock

+1
source

I think you just need to use the FileShare.None flag in the overloaded Open method.

 file = File.Open("storage.bin", FileMode.Open, FileShare.None); 
+1
source

I ended up writing this helper class to do this:

 public static class FileHelper { public static void ReplaceFileContents(string fileName, Func<String, string> replacementFunction) { using (FileStream fileStream = new FileStream( fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None)) { StreamReader streamReader = new StreamReader(fileStream); string currentContents = streamReader.ReadToEnd(); var newContents = replacementFunction(currentContents); fileStream.SetLength(0); StreamWriter writer = new StreamWriter(fileStream); writer.Write(newContents); writer.Close(); } } } 

which allows you to pass a function that will accept existing content and generate new content, and ensure that the file is not read or modified by anything else while this change occurs

+1
source

Source: https://habr.com/ru/post/921180/


All Articles