Как я могу редактировать (файл .txt) с помощью обработчика generi c (ASHX)? - PullRequest
0 голосов
/ 19 апреля 2020

Я использую обработчик Asp. net generi c (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, 0, rawBytes.Length);

            ////var fs = File.AppendText(ServerPath);
            //byte[] b = File.ReadAllBytes(ServerPath);
            ////UTF8Encoding temp = new UTF8Encoding(true);
            ////fs.Write("ffffffffffffffff", 0, 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";
            }
...