File.Create method isn't releasing the file
-
I am using the File.Create method to create a text file. My problem is that when I need to write to that file after it has been created I get the error message "The process cannot access the file "C:\2005 Sim R4 MedAlu Data\TN.txt" because it is being used by another process.".
-
I am using the File.Create method to create a text file. My problem is that when I need to write to that file after it has been created I get the error message "The process cannot access the file "C:\2005 Sim R4 MedAlu Data\TN.txt" because it is being used by another process.".
You must derive a FileStream von the Create method: FileStream fs = File.Create("C:\2005 Sim R4 MedAlu Data\TN.txt"); After that you can access the FileStream: fs.Write("Your Data"); and finally close it. fs.Close(); -- modified at 9:49 Thursday 15th December, 2005
-
I am using the File.Create method to create a text file. My problem is that when I need to write to that file after it has been created I get the error message "The process cannot access the file "C:\2005 Sim R4 MedAlu Data\TN.txt" because it is being used by another process.".
What code are you using? I suspect that you didn't call the
Close()
method right after you created the file. We can't do much without seeing the actual code you use. But anyways, here's a sample code I use when creating/writing text files:FileStream
fs
= null;
stringtmpFile
= "C:\\sample.txt";// Create file and close it so that it can be re-opened for writing further on
fs
= File.Create(tmpFile
, 1024);
fs.Close();
// Prepare file for writing by assigning a new textWriter to it
TextWriterwr
= new StreamWriter(tmpFile
);// Write stuff into the file
wr.WriteLine("Stuff 1"); wr.WriteLine("Stuff 2"); wr.WriteLine("Blah blah blah......");
// Eventually close the file
wr.Close();
Regards, Polis Can you practice what you teach? -- modified at 9:53 Thursday 15th December, 2005