该进程无法访问该文件,因为该文件正在被 FILE IO 中的另一个进程使用,使用 WCF 服务
我正在使用以下代码写入文件,但是当我尝试打开文件以在其中执行某些操作时出现问题。它给了我错误 该进程无法访问该文件,因为该文件正在被另一个进程使用。 我正在使用 WCF 获取参数值并使用 .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();
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 File.Create 之后,文件已经打开。
尝试这样的操作
也会自动关闭您的 Streamwriter。
After File.Create the file is already open.
Try something like
this also closes your streamwriter automatically.