Dear friends
I am creating on .txt file like this
FileStream fileStream = new FileStream(@"C:\mayur.txt", FileMode.Create);
file is created
on next line i am trying to initialise one writer
TextWriter sw = new StreamWriter(@"C:\\mayur.txt");
but it is giving error file is being used by another person on this line
when I create file manually and then this code works fine but this i am not creating file in code
How to solve this problem
Creting and writting text file
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.


Anuja PawarRank #1,404 · 1.2K reputationPosted Dec 1, 2011, 9:27 PM EST
use
fileStream.Close();
Try this
FileStream fs = new FileStream("test.txt", FileMode.Create);
StreamWriter w = new StreamWriter(fs, Encoding.UTF8);
w.WriteLine(1.2M);
w.WriteLine("string");
w.WriteLine('!');
w.Flush();
w.Close();
fs.Close();
fs = new FileStream("test.txt", FileMode.Open);
StreamReader r = new StreamReader(fs, Encoding.UTF8);
Console.WriteLine(Decimal.Parse(r.ReadLine()));
Console.WriteLine(r.ReadLine());
Console.WriteLine(Char.Parse(r.ReadLine()));
r.Close();
fs.Close();