Dor Bracha
Dor Bracha

Reputation: 11

synchronization processes with read & write to file c#

I am trying to stop all other processes from reading and writing to a file so I need to synchronize between process,how can I do it?

It will be helpful if someone will give me a pattern for it, and show me where should I enter the read and write section in the code.

Upvotes: 1

Views: 922

Answers (1)

Jan Unld
Jan Unld

Reputation: 370

Alright. Easy thing thought if that's your aim. You can use a filestream to open up your file. Like..

using (var stream = new FileStream(
                            @"C:\files\yourFile.txt", 
                            FileMode.Open, 
                            FileAccess.ReadWrite, 
                            // that's the important parameter to set
                            // it locks the file from other processes
                            // as long as the stream persists
                            FileShare.None)) {
    // give it some logic ...
}

Messed up. Didn't note the questions intention, my bad. If you try to close a process from reading/writing to a file. Try to find out which and kill it.

Upvotes: 1

Related Questions