I am unable to open the file just after creating the file in c# webservice code .
The directory is not present initially. So the program creates the directory and the file. Then tries to open the file to write a line. But it fails to open the file. The error is the file is open in another process.
I tried to open the file from windows explorer. the file opens but error message is still coming.
I tried to delete the folder C:\Test, it says file is open is in WebDev.WebDevServer40.exe
Any help please.
[WebMethod]
public string saveFileUploaderName(string name)
{
string path = "c:\\Test";
string filename = "Test.txt";
string completeFileName = Path.Combine(path,filename);
if(!File.Exists(completeFileName))
{
Directory.CreateDirectory(path);
File.Create(completeFileName);
}
StreamWriter writer = File.AppendText(completeFileName);
writer.WriteLine(name);
writer.Flush();
writer.Close();
return "success";
}
File.Create returns a stream, but you’re discarding it.
So (I think!) you either need to use the returned stream, or explicitly close it before trying to open a new stream to the same file.