日期:2014-05-17  浏览次数:20513 次

追加文本文件内容时报“文件正由另一进程使用,因此该进程无法访问该文件”
求解:在追加文本文件内容时报:“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)
C# code

        /// <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();
        }



------解决方案--------------------
正在被使用,假如多个用户同时登陆这个界面,进行同一个操作,就可以引起这个错误
------解决方案--------------------
多线程了 记得互斥
------解决方案--------------------
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
{

}


我是这样写的