leora
leora

Reputation: 196891

Parse directory name from a full filepath in C#

If I have a string variable that has:

"C:\temp\temp2\foo\bar.txt"

and I want to get

"foo"

what is the best way to do this?

Upvotes: 38

Views: 65252

Answers (6)

Callie J
Callie J

Reputation: 31336

Building on Handleman's suggestion, you can do:

Path.GetFileName(Path.GetDirectoryName(path))

This doesn't touch the filesystem (unlike FileInfo), and will do what's required. This will work with folders because, as the MSDN says:

Return value: The characters after the last directory character in path. If the last character of path is a directory or volume separator character, this method returns String.Empty. If path is null, this method returns null.

Also, looking at the reference source confirms that GetFilename doesn't care if the path passed in is a file or a folder: it's just doing pure string manipulation.

Upvotes: 4

Shameegh Boer
Shameegh Boer

Reputation: 31

I had an occasion when I was looping through parent child directories

string[] years = Directory.GetDirectories(ROOT);
foreach (var year in years)
{
    DirectoryInfo info = new DirectoryInfo(year);
    Console.WriteLine(info.Name);
    Console.WriteLine(year);
    //Month directories
    string[] months = Directory.GetDirectories(year);
    foreach (var month in months)
    {
        Console.WriteLine(month);
        //Day directories
        string[] days = Directory.GetDirectories(month);
        foreach (var day in days)
        {
            //checkes the files in the days
            Console.WriteLine(day);
            string[] files = Directory.GetFiles(day);
            foreach (var file in files)
            {
                Console.WriteLine(file);                               
            }
        }
    }
}

using this line I was able to get only the current directory name

DirectoryInfo info = new DirectoryInfo(year);
Console.WriteLine(info.Name);

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1504062

Use:

new FileInfo(@"C:\temp\temp2\foo\bar.txt").Directory.Name

Upvotes: 79

Ananth
Ananth

Reputation: 10730

I think most simple solution is

DirectoryInfo dinfo = new DirectoryInfo(path);

string folderName= dinfo.Parent.Name;

Upvotes: 9

rysama
rysama

Reputation: 1796

It'll depend on how you want to handle the data, but another option is to use String.Split.

string myStr = @"C:\foo\bar.txt";
string[] paths = myStr.Split('\\');
string dir = paths[paths.Length - 2]; //returns "foo"

This doesn't check for an array out of bounds exception, but you get the idea.

Upvotes: 1

Handleman
Handleman

Reputation: 754

Far be it for me to disagree with the Skeet, but I've always used

Path.GetFileNameWithoutExtension(@"C:\temp\temp2\foo\bar.txt")

I suspect that FileInfo actually touches the file system to get it's info, where as I'd expect that GetFileNameWithoutExtension is only string operations - so performance of one over the other might be better.

Upvotes: 11

Related Questions