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

时光毁灭记忆、已成空白 提交于 2019-12-05 11:01:59

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)

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().

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

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();
    }
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!