How to find the Name of the Subfolder in a Directory

15,730

Solution 1

            string rootDir = folderBrowserDialog.SelectedPath;
            string fileDir = Path.GetDirectoryName(fileName);
            if (rootDir.Length < fileDir.Length)
                row.FOLDER = fileDir.Substring(rootDir.Length + 1);

That did the trick.

Solution 2

Based on the question and various comments, I infer that:

  • You have a file path in hand
  • That file is at the level above Plugins, i.e.:

C:\Program Files (x86)\EdisonFactory\NetOffice

  • You need to get the directories under that directory, but not that directory itself

If these assumptions hold true, then this will do what you ask:

var directoryName = "C:\Program Files (x86)\EdisonFactory\NetOffice";

var directory = new DirectoryInfo(directoryName);

row.FOLDER = directory
    .GetDirectories()
    .Select(subDirectory => subDirectory.Name)
    .Single();

Solution 3

For example: you have the Sub Folder Plugins in Folder NetOffice, with the path:

C:\Program Files (x86)\EdisonFactory\NetOffice\Plugins.

And you are trying to only get the name of the folder plugins, you can try the following code:

string path = @"C:\Program Files (x86)\EdisonFactory\NetOffice";
DirectoryInfo Dictiontory = new DirectoryInfo(path);
DirectoryInfo []Dir = Dictiontory.GetDirectories();// this get all subfolder //name in folder NetOffice.
string dirName = Dir[0]; //var dirName get name from array Dir;
Share:
15,730
Baked Potato
Author by

Baked Potato

gaming.stackexchange.com Just looking around some old classic games trying to help people out with questions so that one day when i need help with a game they will help me! P.S. I mostly answer questions about Terraria and Minecraft. stackoverflow.com I am an intern at programming and am kinda new at it so some of my questions might be a bit general. english.stackexchange.com I am trying to improve my English by using this site. I find doing this helpful because unlike teachers where everything is general knowledge , this site gives me some advanced and some beginner questions to strengthen my vocabulary, grammar, and way to express thoughts/ideas.

Updated on June 04, 2022

Comments

  • Baked Potato
    Baked Potato almost 2 years

    I have asked this type of a question before but this one IS different. I need to find the root folder of a folder.

    For example:

    I get is

    C:\Program Files (x86)\EdisonFactory\NetOffice
    C:\Program Files (x86)\EdisonFactory\NetOffice
    C:\Program Files (x86)\EdisonFactory\NetOffice
    C:\Program Files (x86)\EdisonFactory\NetOffice
    C:\Program Files (x86)\EdisonFactory\NetOffice\Plugins
    C:\Program Files (x86)\EdisonFactory\NetOffice\Plugins
    C:\Program Files (x86)\EdisonFactory\NetOffice\Plugins
    

    I used this code before:

    DsVersions.ASSEMBLY2Row row = dsVersions.ASSEMBLY2.NewASSEMBLY2Row();
    row.FOLDER = Path.GetDirectoryName(fileName);
    

    And then i found this code and thought it was the answer:

    DsVersions.ASSEMBLY2Row row = dsVersions.ASSEMBLY2.NewASSEMBLY2Row();
    DirectoryInfo directoryName = new DirectoryInfo(Path.GetDirectoryName(fileName));
    row.FOLDER = directoryName.Name;
    

    After this code i was getting this:

    NetOffice
    NetOffice
    NetOffice
    NetOffice
    Plugins
    Plugins
    Plugins
    

    It's close but again all i need is Plugins. I have tried doing Path and File but it won't work, I always get an error.