追加文本文件内容时报“文件正由另一进程使用,因此该进程无法访问该文件”

General_Y 2012-08-20 11:27:56
求解:在追加文本文件内容时报:“System.IO.IOException: 文件“D:\WEBROOT\IntegrateOfficeSystem\WEB_TwoDimensionalCode\Log\APIPlatform\TwoDimensionalCode\FreezeChargebackFailure\2012-08-17.txt”正由另一进程使用,因此该进程无法访问该文件。 在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 在 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) 在 System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) 在 System.IO.StreamWriter.CreateFile(String path, Boolean append) 在 System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize) 在 System.IO.StreamWriter..ctor(String path, Boolean append) 在 System.IO.File.CreateText(String path) 在 IntegratedOfficeSystem.Tool.FileOperations.GetCreateFile(String FileName) 位置 D:\Work\IntegratedOfficeSystem\IntegratedOfficeSystem\IntegratedOfficeSystemTool\FileOperations.cs:行号 27 在 CreateLog.GetReturnGenerateLog(String Content, Int32 GenerateType, Int32 DirType) 在 OperatingTDC_TwoDimensionalCode.GetSendTwoDimensionalCode(String XML)

/// <summary>
/// 追加文件
/// </summary>
/// <param name="Path">文件路径</param>
/// <param name="strings">内容</param>
public static void FileAdd(string Path, string strings)
{
StreamWriter sw = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(Path));
sw.Write(strings);
sw.Flush();
sw.Close();
sw.Dispose();
}
...全文
213 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
General_Y 2012-08-20
  • 打赏
  • 举报
回复
解决啦,谢谢。
解决方法:

private static object m_lock = new object();

/// <summary>
/// 追加文件
/// </summary>
/// <param name="Path">文件路径</param>
/// <param name="strings">内容</param>
public static void FileAdd(string Path, string strings)
{
lock(m_lock)
{
StreamWriter sw = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(Path));
sw.Write(strings);
sw.Flush();
sw.Close();
sw.Dispose();
}
}
General_Y 2012-08-20
  • 打赏
  • 举报
回复
请问如何互斥,互斥代码怎么写
Net攻城狮 2012-08-20
  • 打赏
  • 举报
回复
try
{
string strFileName = DateTime.Now.ToString("yyyy年MM月dd日增删改");
string strurl = Server.MapPath("../UploadedExcel/" + strFileName + ".txt");
FileStream fs = null;
if (!Directory.Exists(strurl))
{
fs = new FileStream(strurl, FileMode.Append);

}
fs.Flush();
fs.Dispose();
fs.Close();
StreamWriter sw = File.AppendText(strurl);
sw.WriteLine("执行时间: " + DateTime.Now.ToString());
sw.WriteLine("存储过程名称: " + LSTR_StoreProcedure);
sw.WriteLine("数据: " + LSTR_QueryCondition);
sw.WriteLine("信息: " + mess);
sw.WriteLine("*****************************************************************************************************************");
sw.WriteLine("");
sw.WriteLine("");
sw.Flush();
fs.Dispose();
sw.Close();
}
catch
{

}


我是这样写的
rayyu1989 2012-08-20
  • 打赏
  • 举报
回复
多线程了 记得互斥
bdmh 2012-08-20
  • 打赏
  • 举报
回复
正在被使用,假如多个用户同时登陆这个界面,进行同一个操作,就可以引起这个错误

62,242

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧