ShaqD
ShaqD

Reputation: 63

C# Could not find a part of the path with Directory.GetDirectories

I'm trying to make an application that copies volume to a selected location in the computer. The volume is an external hard disk that includes a copy of my previous computer C volume.

First of all, when I tried to copy the directories and the files I made an recursive function that gets the directories and the files and copy them to the new location, the recursive function gets the directories with Directory.GetDirectories function, after I get the sub directories I make the same function to get the sub directories of the sub directories and keeps doing that untill there are no directories to get, all works fine but when I tried to use the application on my volume I got an infiniate loop with "Application Data" folder, that means that the GetDirectories function found "Application Data" folder in the previous "Application Data" again and again. In order to fix that I checked if the path does not include "Application Data\Application Data" and just the I used the GetDirectories function. Maybe that solution caused the problem I'm going to ask about.

The problem is that when I use the function GetDirectories I get an exception: "Could not find a part of the path" but my code looks like this:

if(Directory.Exist(path))
{
    string[] subdirs = Directory.GetDirectories(path);
}

So how is that possible that the Exist function finds the folder but the GetDirectories function does not find it? By the way, the application works properly on directories that are not part of windows system. So what is the problem? And how can I solve it or how can I make copy application that will copy C volume? Thanks a lot

Upvotes: 2

Views: 7467

Answers (1)

Yael
Yael

Reputation: 1600

From Microsoft support:

A service that runs under a LocalSystem account or under a local user account can only access mapped drives that the service creates. Mapped drives are stored for each logon session. If a service runs under a LocalSystem account or under a local user account that does not create certain mapped drives, the service cannot access these mapped drives. Additionally, a service that runs under a local user account that creates certain mapped drives also receives a new set of mapped drives if you log off and then you log on again as the same local user.

There is a workaround but it not necessary, just use try-catch for those libs:

if(Directory.Exist(path))
{
    try
    {
        string[] subdirs = Directory.GetDirectories(path);
    }
    catch (Exception ex)
    {

    }
}

Or, alternatively, use Directory.GetDirectories SearchOptions so you can disclude certin sub-directories.

Upvotes: 2

Related Questions