System.IO.IOException: 'The process cannot access the file '@.txt' because it is being used by another process.'

后端 未结 6 598
野的像风
野的像风 2021-01-11 13:15

I am new to programming and I have a question. If I have two functions, one creates a text file and writes into it, while the other opens the same text file and reads from i

相关标签:
6条回答
  • 2021-01-11 13:27

    In one of the unit tests, I had to create a temp file and then remove it after, and I was getting the above error.

    None of the answers worked. Solution that worked was:

    var path = $"temp.{extension}";
            using (File.Create(path))
            {
            }
    
    
            File.Delete(path);
    
    0 讨论(0)
  • 2021-01-11 13:31

    after writing your text file, you should close it first before proceeding to your second function:

    var myFile = File.Create(myPath);
    //some other operations here like writing into the text file
    myFile.Close(); //close text file
    //call your 2nd function here
    

    Just to elaborate:

    public void Start() {
        string filename = "myFile.txt";
        CreateFile(filename); //call your create textfile method
        ReadFile(filename); //call read file method
    }
    
    public void CreateFile(string filename) {
        var myFile = File.Create(myPath); //create file
        //some other operations here like writing into the text file
        myFile.Close(); //close text file
    }
    
    public void ReadFile(string filename) {
        string text;
        var fileStream = new FileStream(filename, FileMode.Open, 
        FileAccess.Read); //open text file
        //vvv read text file (or however you implement it like here vvv
        using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
        {
            text = streamReader.ReadToEnd();
        }
        //finally, close text file
        fileStream.Close();
    }
    

    The point is, you have to close the FileStream after you are done with any operations with your file. You can do this via myFileStream.Close().

    Moreover, File.Create(filename) returns a FileStream object which you can then Close().

    0 讨论(0)
  • 2021-01-11 13:32

    You will have to close the file after editing it.

    var myFile = File.Create(myPath);
    //myPath = "C:\file.txt"
    myFile.Close();
    //closes the text file for eg. file.txt
    //You can write your reading functions now..
    

    After closing it you can again use it(for reading)

    0 讨论(0)
  • 2021-01-11 13:39

    The issue is sometimes file locks don't get released immediately after they are closed.

    You can try run a loop to read the file. Inside the loop put a try catch statement and if the file reads successfully break from the loop. Otherwise, wait a few milliseconds and try to read the file again:

    string originalText = null;
    while (true)
    {
        try
        {
            originalText = File.ReadAllText(@"C:\Users\...\fileName.txt", Encoding.Default);
            break;
        }
        catch 
        { 
            System.Threading.Thread.Sleep(100);
        }
    }
    
    0 讨论(0)
  • 2021-01-11 13:48

    Actually this is not a problem of closing/disposing the stream, File.WriteAllText and File.ReadAllText does that internally.

    The issue is because a wrong use of the async/await pattern. GET is async but never awaited, thus causing function1 to finish and move on to function2 before all content was actually written to the file.

    The way it is written GET is not awaitable because it is async void which should never be used unless you're dealing with event or really know what you're doing.

    So, either remove the use of async/await completely or be async all the way:

    1. Change GET to be awaitable:

      async Task GET(string link, string fileName)
      
    2. await it in the now async function1:

      async Task function1()
      {
          ...
          for (int x = 0; x < fileNames.Count; x++)
          {
              await GET(linki[x], fileNames[x]);
              //System.Threading.Thread.Sleep(6000);
          }
          ...
      
    3. await function1 in the Elapsed event:

      private async void tickTimera1(object source, ElapsedEventArgs e)
      {
          await function1();
          function2();
      }
      
    0 讨论(0)
  • 2021-01-11 13:50

    Create a file and then close it. After can save data into that file. I did as below.

    if (!File.Exists(filePath))
    {
        File.Create(filePath).Close();
    }
    File.WriteAllText(filePath, saveDataString)
    
    0 讨论(0)
提交回复
热议问题