简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM