string path = "C:\\mytest.txt";
if (File.Exists(path))
{
File.Delete(path);
}
using (FileStream fs = File.Create(path))
{
//fs.Close();
// fs.CanWrite = true;
TextWriter sw = new StreamWriter("C:\\mytest.txt");//error is coming here
sw.WriteLine("ddd", "dddd", "dsdsd");
--------------
-------------
--------
}
I am trying to create and write to text file but is giving error file is being used by another person
Creating and writting file
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.


Pravin GhadgeRank #1,340 · 1.3K reputationPosted Dec 20, 2011, 2:57 AM EST
try this:
string path = "C:\\mytest.txt";
if (File.Exists(path))
{
File.Delete(path);
}
using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
{
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine("ddd", "dddd", "dsdsd");
m_streamWriter.Flush();
}
Pankaj RanaRank #2,270 · 620 reputationPosted Dec 20, 2011, 3:01 AM EST
Sam HobbsRank #107 · 20.5K reputationPosted Dec 20, 2011, 11:05 AM EST