System.Io.File.Create Locking a File

System.IO.File.Create locking a file

File.Create returns a FileStream. You should use it like this:

using (FileStream fs = File.Create(path))
{
//you can use the filstream here to put stuff in the file if you want to
}

Can I ReadLock a file while I create/modify it using System.IO.File.WriteAllText() method?

To acquire an exclusive lock you can use the file stream to do so

        using (FileStream fs = new FileStream("Test.txt", FileMode.Append, FileAccess.Write, FileShare.None))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine("test");
}
}

This way you are locking the file exclusively for the current file stream. Any other application or even a new instance of file stream from another thread within the same application attempts to read or write to the file will be denied by the operating system.

File.Create seems to lock the file. How can I avoid this?

File.Create returns a FileStream. So... close it:

using(File.Create(path)) {
// add contents here if needed
}

The using ensures it is Dispose()d, hence closed. Note: it is also possible that some AV systems will interfere with file access, but that is usually not a huge problem.

How to lock a file with C#?

As per 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);

Why is System.IO.File.AppendAllText locking my file?

File.AppendAllText close the file itself after writing it. But you are facing the issue probably because the file is shared across multiple session in your web application. Use lock to prevent concurrent access.

You should handle the code you provided in a different way for web application.

using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
System.IO.StreamWriter writer = new System.IO.StreamWriter(ms);
writer.Write("Some Text");
writer.Flush();
writer.Dispose();
ms.Position = 0;

System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Text.Plain);
System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(ms, ct);
attach.ContentDisposition.FileName = "Filename.txt";

SmtpClient mailClient = new SmtpClient();
MailMessage msg = new MailMessage();
msg.Attachments.Add(attach);
mailClient.Send(msg);
}

In the above example attachment is created in-memory insteed of storing in file-system.

Create file without opening/locking it?

Maybe you can try using File.WriteAllText Method (String, String)
with the file name and an empty string.

Creates a new file, writes the
specified string to the file, and then
closes the file. If the target file
already exists, it is overwritten.

File lock errors after moving files in code

See my comments below in code:

using(FileStream sourceStream = File.Open(sourceFileName, FileMode.Open))
{
using(FileStream destinationStream = File.Create(destinationFileName))
{
try
{
await sourceStream.CopyToAsync(destinationStream);
// The sourceFileName file is locked since you are inside the
// the using statement. Move statement for deleting file to
// outside the using.
File.Delete(sourceFileName);
return true;
}
catch
{
return false;
}
}
}
// Move it here

Writing in files ASP.NET C# and NOT locking them afterwards

I would like to thank everyone for the help.
In fact, apart from this code I found out that I had a stremReader still opened somewhere else after the code above. At the end I changed the code I had before for this:

using (FileStream fstr = File.Open(myfile, FileMode.OpenOrCreate, FileAccess.ReadWrite))

{
StreamWriter sw = new StreamWriter(fstr);
sw.Write(mystring);
sw.Flush();
sw.Dispose();
}

and on my StreamReader I did this:

StreamReader sr = new StreamReader(myfile);
string sometext = sr.ReadToEnd();
sr.Dispose();

I could also use this:

File.ReadAllText(myfile);

If there is something that I could have done in a better way please tell me.
Thank you very much.



Related Topics



Leave a reply



Submit