System.IO.IOException: file used by another process

前端 未结 10 2343
囚心锁ツ
囚心锁ツ 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:33

    It worked for me.

    Here is my test code. Test run follows:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                FileInfo f = new FileInfo(args[0]);
                bool result = modifyFile(f, args[1],args[2]);
            }
            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(originalPath + ".tmp", originalPath, true); 
                    result = true; 
                } 
                catch (Exception ex) 
                { 
                    Console.WriteLine(ex); 
                } 
                return result; 
            }
        }
    }
    
    
    C:\testarea>ConsoleApplication1.exe file.txt padding testing
    input :         
    replaced String         
    

提交回复
热议问题