出于某种原因,我正在使用Asp.net通用处理程序(ASHX)来编辑(.txt文件)。
我尽一切努力只是编辑一个令人兴奋的.txt文件的文本。我总是收到错误消息
该进程无法访问文件'E:bla bla bla \ Home_0.txt',因为该文件正在被另一个进程使用。
此消息继续弹出,尽管我没有打开物理文件或以任何其他方式使用它!
我的尝试代码如下所示
string ShortFilePath = "Pages_Texts/";
FullFilePath = ShortFilePath + "Home_" + Text_Naming_Order + ".txt";
string appPath = context.Request.PhysicalApplicationPath;
string ServerPath = appPath + FullFilePath;
context.response.contenttype = "application/octet-stream";
context.Response.WriteFile(ServerPath);
//byte[] rawBytes = File.ReadAllBytes(ServerPath); // Insert loading file with IDDownload to byte array
//context.Response.OutputStream.Write(rawBytes,rawBytes.Length);
////var fs = File.AppendText(ServerPath);
//byte[] b = File.ReadAllBytes(ServerPath);
////UTF8Encoding temp = new UTF8Encoding(true);
////fs.Write("ffffffffffffffff",10);
//File.WriteAllBytes(ServerPath,b);
using (Stream stream = new FileStream(ServerPath,FileMode.Open))
{
stream.Dispose();
using (var writer = new StreamWriter(context.Server.MapPath(FullFilePath),false))
{
//File.Delete(context.Server.MapPath(FullFilePath));
//File.WriteAllText(context.Server.MapPath(FullFilePath),RawHtml);
writer.WriteLine(RawHtml);
writer.Dispose();
//writer.Flush();
writer.Close();
}
msg = msg + "\n The text File saved successfully";
}