System.IO.IOException: file used by another process

前端 未结 10 2337
囚心锁ツ
囚心锁ツ 2020-12-14 16:59

I\'ve been working on this small piece of code that seems trivial but still, i cannot really see where is the problem. My functions do a pretty simple thing. Opens a file, c

10条回答
  •  既然无缘
    2020-12-14 17:49

    Sounds like an external process (AV?) is locking it, but can't you avoid the problem in the first place?

    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;
        }
    }
    

提交回复
热议问题