10 November 2017

System.IO.IOException: The process cannot access the file 'file_name'

Quando cancelli o sposti un file:
GC.Collect()
GC.WaitForPendingFinalizers()

Se non basta, provare anche questo (non testato):
public static System.Boolean FileInUse(System.String file)
{
    try
    {
        if (!System.IO.File.Exists(file)) // The path might also be invalid.
        {
            return false;
        }

        using (System.IO.FileStream stream = new System.IO.FileStream(file, System.IO.FileMode.Open))
        {
            return false;
        }
    }
    catch
    {
        return true;
    }
}

Also, to wait for a file I have made:
public static void WaitForFile(System.String file)
{
    // While the file is in use...
    while (FileInUse(file)) ; // Do nothing.
}

Via.