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 8792237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:57:08+00:00 2026-06-13T22:57:08+00:00

I have found that using Path.GetFileName() in the code below works as I intend

  • 0

I have found that using Path.GetFileName() in the code below works as I intend and gives me the name of the correct folder, but if I use Path.GetDirectoryName() it returns the name of the parent (UserGeneratedContent) folder instead. Why does this occur when both methods are passed the same path as a string? And why does Path.GetFileName() work on directories?

When I use Path.GetFileName() the text of the nodes in the Treeview are those of the folders it finds – this is what I want to happen, but if I use Path.GetDirectoryName() the text is the full path from @”UserGeneratedContent” on down for each node. Why does that happen?

And lastly, can my code be improved?

private void CheckForBaseFolder()
{
    if (Directory.Exists(@"UserGeneratedContent"))
    {
        DirectoryInfo info = new DirectoryInfo(@"UserGeneratedContent");
        DirectoryInfo[] subdirs = info.GetDirectories();

        if (subdirs.Length != 0)
        {
            string path = Path.Combine(@"UserGeneratedContent", subdirs[0].ToString());
            treeView1.Nodes.Add(CheckForSubFolders(path));
            treeView1.SelectedNode = treeView1.Nodes[0];
        }
        else { MessageBox.Show("No User-Generated Folders Or Files Found"); }    
    }
    else { Directory.CreateDirectory(@"UserGeneratedContent"); }
}

private TreeNode CheckForSubFolders(string path)
{
    TreeNode folder = new TreeNode(path);

    folder.Text = Path.GetFileName(path); // Works as intended, but.....
    folder.Text = Path.GetDirectoryName(path); // Returns the parent folder 

    foreach(var subdirectory in Directory.GetDirectories(path))
    {
        folder.Nodes.Add(CheckForSubFolders(subdirectory));                
    }

    folder.ImageIndex = 0;
    folder.SelectedImageIndex = 1;

    return folder;
}
  • 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-06-13T22:57:09+00:00Added an answer on June 13, 2026 at 10:57 pm

    Check your code, you are passing the path that doesn’t contain filename but the last part of the path is directory UserGeneratedContent. Path.GetFileName returns the “The characters after the last directory character in path” so it retuns the last directory name instead of filename (you can make a file without extension). When you call Path.GetDirectoryName() on the same path string it returns “Directory information for path”.

    Check this code to see what I’m referring to:

    Suppose you have directory “one” on C partition, and directory “two” in directory “one”, and a file named “three.txt” in directory “two”, when you execute this code it will produce:

    string directory = Path.GetFileName(@"C:\one\two");
    directory = Path.GetDirectoryName(@"C:\one\two");
    

    directory will hold first “two” and then “C:\one”

    string filename = Path.GetFileName(@"C:\one\two\three.txt");
    directory = Path.GetDirectoryName(@"C:\one\two\three.txt");
    

    but now, filename will hold “three.txt” and directory will hold “C:\one\two”

    EDIT:This is later edit after the comments. I would modify the CheckForSubFolders method this way:

    private TreeNode CheckForSubFolders(string path)
        {
    
            TreeNode folder = new TreeNode(path);
            string dir = path.TrimEnd(new char[] { '\\' });
            int index = dir.LastIndexOf('\\');
            folder.Text = dir.Substring(index + 1);
    
            //But I think that it is OK to use folder.Text = Path.GetFileName(path); 
            //since the filename of some file will never be passed to the CheckForSubFolders method         
    
            foreach(var subdirectory in Directory.GetDirectories(path))
            {
    
                folder.Nodes.Add(CheckForSubFolders(subdirectory));
            }
    
            folder.ImageIndex = 0;
            folder.SelectedImageIndex = 1;
            return folder;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have found that I have no problem using require to load something like
I have found that creating a zip file using the Zip task provided by
I'm using DataMapper in a Rails project, and have found that calling to_json on
I am using Happy to generate a parser. I have found that when I
I have just started spring, I found that somewhere we are using handlerequest() method
I have just found a script we are using which has a sub that
I am using UBUNTU, from /etc/mongod.conf, I found that /var/lib/mongdb is the path for
I'm using NetBeans as my IDE. Whenever I have some code that uses another
I have found that writing PHP code within classes can become rather long: $this->parser->parse_syntax($this->get_language_path($this->language),
I found here that Magento is using these tags as custom config variables, but

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.