Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3752360
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:13:49+00:00 2026-05-19T09:13:49+00:00

I want to copy a folder, and i want to delete destination folder first.

  • 0

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 An unhandled exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll
when trying to copy files. This is the 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);
        }.......

I get the exception in this line FileInfo fileinfo = new FileInfo(dest);.

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:

An unhandled exception of type
‘System.IO.DirectoryNotFoundException’
occurred in mscorlib.dll

Additional information: Could not find
a part of the path
‘C:\Users\joe\Desktop\destfolder\.buildpath’.

SOLUTION

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:

GC.Collect();
GC.WaitForPendingFinalizers();

so the correct code will be

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

This way, you wait with the creation until the deletion process is finished.
Yhanks everyone and especially Saeed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T09:13:50+00:00Added an answer on May 19, 2026 at 9:13 am

    You got it kinda wrong. The reason for the exception is that there’s still a resource that is accessing the folder (or file).

    The solution is never GC.collect() or Sleep()… That’s just a work around.
    What you’re doing is just letting the garbage collector dispose of the resource, and then giving it time to act.

    The RIGHT way is to manage your own resources. Instead of a static method that you have no control on, use a using block and dispose of the resource in the end of the block. This way there’s no overhead while your waiting for things that aren’t under your control (GC).

    Use an object that controls the resources, and the using block will dispose it at the end.

    In your case, using a single DirectoryInfo object that controls that resource should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to copy files that have timestamps from the time the script begins
I want to delete content from every file within a folder (Which includes files
Say I want to copy the contents of a directory excluding files and folders
I want to copy text files and only text files from src/ to dst/
I want to copy a txt file from one folder to another. While coping
I want to copy some file from one folder to another folder of my
On install I want to optionally copy some .ini files from SOURCEDIR to TARGETDIR
I've got problem with local copy of SVN folder $ svn up svn: REPORT
I want copy whole folder to another folder using Copy-Item . My source folder
I want to copy pictures clicked by the user to the Data folder for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.