I have taken an example straight out of the MSDN. I want to create a file and write to it straight away. I was hoping to use FileInfo objects. So I create a new FileInfo object, call the Create and then open a FileStream. When I try to open the FileStream I get an exception telling me that another process is using the file. (Yes me).
I copy-pasted the MSDN example straight and their code suffers from the same thing. What have I/they done wrong? (P.S. .Net 3.5)
Update: The code below is STRAIGHT from the MSDN
string path = @"c:\MyTest.txt";
FileInfo fi = new FileInfo(path);
if (!fi.Exists)
{
//Create the file.
fi.Create();
}
// Open the stream for writing.
using (FileStream fs = fi.OpenWrite())
{
Byte[] info =
new UTF8Encoding(true).GetBytes("This is to test the OpenWrite method.");
// Add some information to the file.
fs.Write(info, 0, info.Length);
}