Vlad Schnakovszki
Vlad Schnakovszki

Reputation: 8601

Directory.CreateDirectory does not work

I am creating an application using C# 2010 whose purpose is to copy files that have a specified extension. It uses .NET Framework 3.5 .

I have the following piece of code:

    private void GetFiles()
    {
        bool validext;
        foreach (string filePath in Directory.GetFiles(SourcePath, "*.*", SearchOption.AllDirectories))
        {
            //Check if the file has a specified extension
            validext = false;
            foreach (string extension in ext)
                if (HasExtension(filePath, extension))
                {
                    validext = true;
                    break;
                }

            if ((validext && mode == 1) || (!validext && mode == 2))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                to_copy.Add(filePath);
            }
        }
        totalcount = to_copy.Count;
    }

The problem is on this command:

Directory.CreateDirectory(TrimFile(filePath));

It doesn't raise any exception, the directory path it should create is valid and the application runs with administrator privileges. Nevertheless, the directory is not created. Searching the internet didn't give me any answers, so my question is:

Why is Directory.CreateDirectory(TrimFile(filePath)); not working?

EDIT: I have replaced the FileTrim function with Path.GetDirectoryName(filePath) .

Upvotes: 1

Views: 19142

Answers (2)

Gonzo345
Gonzo345

Reputation: 1323

I've recently fallen in this as well, and silly me I wasn't concreting the right path, so it was creating the directory I concreted... inside the VSProjects/ProjectName/bin/Debug/ :'D

Before:

if (!Directory.Exists(Path.GetDirectoryName(properties.ApiConfigRoute)))
      Directory.CreateDirectory("ConfigFolder");

After:

if (!Directory.Exists(Path.GetDirectoryName(properties.ApiConfigRoute)))
      Directory.CreateDirectory(Path.GetDirectoryName(properties.ApiConfigRoute));

Doh! Hope it helps. Just make sure you're creating it with the absolute route.

Upvotes: 1

Renatas M.
Renatas M.

Reputation: 11820

Lets "debug" code

private void GetFiles()
{
    //1. Lets say SourcePath = "C:\some\kind\of\directory\"
    foreach (string filePath in Directory.GetFiles(SourcePath, "*.*", SearchOption.AllDirectories))
    {
        //2. Lets say we have file filePath = "C:\some\kind\of\directory\file.txt"

        //.. all validation

        //3. At this point Path.GetDirectoryName("C:\some\kind\of\directory\file.txt")
        //   returns "C:\some\kind\of\directory\"
        //4. Lets create that directory...but wait its already exist and contains some files! 
        //   Why create what already exists?
        Directory.CreateDirectory(Path.GetDirectoryName(filePath));
        to_copy.Add(filePath);
    }
}

So the truth is that your CreateDirectory call is useless, because you already looping through existing files in directories. Unless you want to create lets say in all directories new sub directory "Temp" then you need to do this:

Directory.CreateDirectory(Path.Combine(Path.GetDirectoryName(filePath), "Temp"));

After that you will get C:\some\kind\of\directory\Temp directory created.

To copy file to other directory you need to do this:

string directoryToCopyFiles = @"C:\My\directory\of\file\copies";
if(!Directory.Exists(directoryToCopyFiles))
   Directory.CreateDirectory(directoryToCopyFiles);

File.Copy(Path.Combine(directoryToCopyFiles, Path.GetFileName(filePath)), filePath);

After that directory C:\My\directory\of\file\copies will be created if it didn't existed before and file with the same name will be copied there.

Upvotes: 5

Related Questions