The process cannot access the file because it is being used by another process in FILE IO, using WCF service
I am using following code to write in file in, but problem occurred when I try to OPEN the file to do some operations in it.. IT give me error The process cannot access the file because it is being used by another process. I am using WCF to get parameter values and using .net 4.0
string strTemp = Penn.Common.Shared.GlobalConstants.tempFolder;
string str = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
string format= "MM dd yyyy HH mm ss开发者_如何学运维";
string filename = "XMLFile" + DateTime.Now.ToString(format) + ".xml";
StringBuilder strbuilder= new StringBuilder();
strbuilder.Append(str);
strbuilder.Append(@"\");
strbuilder.Append(strTemp);
strbuilder.Append(@"\");
strbuilder.Append(filename);
File.Create(strbuilder.ToString());
File.Open(strbuilder.ToString(), FileMode.Open, FileAccess.Write, FileShare.Write);
System.IO.StreamWriter objwriter = new StreamWriter(strbuilder.ToString());
objwriter.Write(xml);
objwriter.Close();
After File.Create the file is already open.
Try something like
using (StreamWriter sw = File.CreateText(strbuilder.ToString()))
{
sw.WriteLine(xml);
}
this also closes your streamwriter automatically.
精彩评论