Neo
Neo

Reputation: 16239

How to create file without lock in c#

I'm creating text file at location using following code.

File.Create("C:\mylog.txt").Close();

But by default it is locked mode

I tried similar file to create using below code in unlock mode but i failed.

 var outStream = new FileStream("C:\mylog.txt", FileMode.Create,
   FileAccess.Write, FileShare.ReadWrite);

How do I create file in no lock mode?

Upvotes: 2

Views: 5263

Answers (1)

Rahul Tripathi
Rahul Tripathi

Reputation: 172578

You can try to use the using statement

using(FileStream fs = File.Create(yourpath)) {
    // your code
}

When you create a file using File.Create then it returns the FileStream and opens the FileStream and hence your file is locked. When you use the using statement then the FileStream is closed automatically when you are done.

Upvotes: 6

Related Questions