Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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
Posted
Updated 20-Dec-11 2:14am
v2
Comments
Pete O'Hanlon 20-Dec-11 7:45am    
What error are you getting? If you wrap this section of code in a try/catch block, you can actually find out what the problem is. I suspect the issue is a permissions issue, but only you can verify that. This is your opportunity to show off your debugging skills.
mayur csharp G 20-Dec-11 7:48am    
file is being used by another person

That's natural since you're trying to open the file two times: What's the purpose of the
C#
using (FileStream fs = File.Create(path))


if you then open again the file (with new StreamWriter("C:\\mytest.txt");)?

See, for instance: "C# Using StreamWriter"[^].
 
Share this answer
 
The issue you are getting is because you've just created the file using the line:
C#
using (FileStream fs = File.Create(path))
The file is open at this point, and you then attempt to open the same file again using:
C#
TextWriter sw = new StreamWriter("C:\\mytest.txt");//error is coming here
Use one or the other. Not both.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900