Note that there are some explanatory texts on larger screens.

plurals
  1. POSystem.IO.DirectoryNotFoundException after deleting an empty folder and recreating it
    primarykey
    data
    text
    <p>I want to copy a folder, and i want to delete destination folder first. So I am deleting destination folder then recreate it and then copy files. The problem is that i get the <code>An unhandled exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll</code> when trying to copy files. This is the code</p> <pre><code>static public void CopyFolder(string sourceFolder, string destFolder) { if (Directory.Exists(destFolder)) // check if folde exist { Directory.Delete(destFolder, true); // delete folder } Directory.CreateDirectory(destFolder); // create folder string[] files = Directory.GetFiles(sourceFolder); foreach (string file in files) { string name = Path.GetFileName(file); string dest = Path.Combine(destFolder, name); File.Copy(file, dest, true); FileInfo fileinfo = new FileInfo(dest); // get file attrib if (fileinfo.Attributes != FileAttributes.ReadOnly) // check if read only File.SetAttributes(dest, FileAttributes.Normal); }....... </code></pre> <p>I get the exception in this line <code>FileInfo fileinfo = new FileInfo(dest);</code>.</p> <p>It seems like there is a delay in the creation of the folder and in the mean time I try to copy a file into it. Any clue, what is the problem? The full exception message:</p> <blockquote> <p>An unhandled exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll</p> <p>Additional information: Could not find a part of the path 'C:\Users\joe\Desktop\destfolder\.buildpath'.</p> </blockquote> <h2>SOLUTION</h2> <p>As it been pointed out by good people, the reason for this exception is that I try recreating the folder before the deletion process is finished. So the solution is to add 2 lines of code after deletion: <code> GC.Collect(); GC.WaitForPendingFinalizers(); </code></p> <p>so the correct code will be </p> <pre><code>static public void CopyFolder(string sourceFolder, string destFolder) { if (Directory.Exists(destFolder)) // check if folde exist { Directory.Delete(destFolder, true); // delete folder GC.Collect(); // CODE ADDED GC.WaitForPendingFinalizers(); // CODE ADDED } Directory.CreateDirectory(destFolder); // create folder string[] files = Directory.GetFiles(sourceFolder); foreach (string file in files) { string name = Path.GetFileName(file); string dest = Path.Combine(destFolder, name); File.Copy(file, dest, true); FileInfo fileinfo = new FileInfo(dest); // get file attrib if (fileinfo.Attributes != FileAttributes.ReadOnly) // check if read only File.SetAttributes(dest, FileAttributes.Normal); }....... </code></pre> <p>This way, you wait with the creation until the deletion process is finished. Yhanks everyone and especially Saeed.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload