System.IO.IOException:另一个进程使用的文件

sro*_*uez 25 c# io file-io ioexception

我一直在研究这段似乎微不足道的小代码,但我仍然无法确定问题出在哪里.我的功能很简单.打开文件,复制其内容,替换内部的字符串并将其复制回原始文件(然后在文本文件中进行简单的搜索和替换).我真的不知道怎么做,因为我在原始文件中添加了行,所以我只创建了一个文件副本,(file.temp)副本也备份(file.temp)然后删除原始文件(文件)并将file.temp复制到文件.我在删除文件时遇到异常.以下是示例代码:

private static bool modifyFile(FileInfo file, string extractedMethod, string modifiedMethod)
    {
        Boolean result = false;
        FileStream fs = new FileStream(file.FullName + ".tmp", FileMode.Create, FileAccess.Write);
        StreamWriter sw = new StreamWriter(fs);

        StreamReader streamreader = file.OpenText();
        String originalPath = file.FullName;
        string input = streamreader.ReadToEnd();
        Console.WriteLine("input : {0}", input);

        String tempString = input.Replace(extractedMethod, modifiedMethod);
        Console.WriteLine("replaced String {0}", tempString);

        try
        {
            sw.Write(tempString);
            sw.Flush();
            sw.Close();
            sw.Dispose();
            fs.Close();
            fs.Dispose();
            streamreader.Close();
            streamreader.Dispose();

            File.Copy(originalPath, originalPath + ".old", true);
            FileInfo newFile = new FileInfo(originalPath + ".tmp");
            File.Delete(originalPath);
            File.Copy(fs., originalPath, true);

            result = true;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }

        return result;
    }`
Run Code Online (Sandbox Code Playgroud)

和相关的例外

System.IO.IOException: The process cannot access the file 'E:\mypath\myFile.cs' because it is being used by another process.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.File.Delete(String path)
   at callingMethod.modifyFile(FileInfo file, String extractedMethod, String modifiedMethod)
Run Code Online (Sandbox Code Playgroud)

通常这些错误来自未封闭的文件流,但我已经处理过了.我想我已经忘记了一个重要的步骤,但无法弄清楚在哪里.非常感谢您的帮助,

si6*_*618 24

听起来像一个外部进程(AV?)锁定它,但你不能首先避免这个问题?

private static bool modifyFile(FileInfo file, string extractedMethod, string modifiedMethod)
{
    try
    {
        string contents = File.ReadAllText(file.FullName);
        Console.WriteLine("input : {0}", contents);
        contents = contents.Replace(extractedMethod, modifiedMethod);
        Console.WriteLine("replaced String {0}", contents);
        File.WriteAllText(file.FullName, contents);
        return true;
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • +1整个复杂处理close,flush streamreader thingy(不使用using语句等等等)让我颤抖.它把一个简单的问题变成了一个不可避免的混乱. (13认同)
  • 谁能解释一下这段代码如何帮助避免问题? (2认同)

小智 19

我意识到我有点迟了,但迟到总比没有好.我最近遇到了类似的问题.我曾经XMLWriter随后更新XML文件并收到相同的错误.我找到了干净的解决方案:

XMLWriter基本用途FileStream访问修改后的文件.问题是当你调用XMLWriter.Close()方法时,底层流不会被关闭并锁定文件.您需要做的是实例化您XMLWriter的设置并指定您需要关闭该底层流.

例:

XMLWriterSettings settings = new Settings();
settings.CloseOutput = true;
XMLWriter writer = new XMLWriter(filepath, settings);
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.


chr*_*ean 5

我能说出最好的代码.我会启动Sysinternals进程资源管理器并找出文件打开的内容.它可能很适合Visual Studio.