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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:37:37+00:00 2026-05-26T20:37:37+00:00

Given paths like this: C:\Temp\SomeDirectory\*.xml I’d like to be able to distinguish the *.xml

  • 0

Given paths like this:

C:\Temp\SomeDirectory\*.xml

I’d like to be able to distinguish the *.xml from C:\Temp\SomeDirectory

However, I do not want a path to a directory that does not have a trailing slash to return its parent directory.

This means I want the following behaviour:

// Wildcard paths return directory
C:\Temp\SomeDirectory\*.csv -> C:\Temp\SomeDirectory

// Trailing slash paths return full path
C:\Temp\SomeDirectory\ -> C:\Temp\SomeDirectory     

// Non-trailing slash paths to a directory return full path
C:\Temp\SomeDirectory -> C:\Temp\SomeDirectory

// Paths to a file return the directory
C:\Temp\SomeDirectory\SomeFileThatExists.csv -> C:\Temp\SomeDirectory

// Paths to a file without an extension (that exists) return the directory
C:\Temp\SomeDirectory\SomeFileThatExistsWithNoExt -> C:\Temp\SomeDirectory

// Paths to a non-existent path without a trailing slash are standard
// Either always clip the trailing part, or always leave it in
// (Can live with this one being C:\Temp\SomeDirectory)
C:\Temp\SomeDirectory\NonExistentObject -> C:\Temp\SomeDirectory\NonExistentObject 

// Paths to a non-existent path with a trailing slash return the full path
C:\Temp\SomeDirectory\NonExistentObject\ -> C:\Temp\SomeDirectory\NonExistentObject

// Paths to a non-existent path with a file extension return the directory
C:\Temp\SomeDirectory\NonExistentFile.Ext -> C:\Temp\SomeDirectory

(I am not fussed if the return value has a trailing slash or not, although the method I put below consistently does not return a trailing slash)

My current code is something like this, and handles these cases:

public string GetDirectory(string path)
{
    try
    {
        var f = new FileInfo(path); // Throws if invalid path, e.g. wildcards

        // Existent directory
        if (Directory.Exists(path))
        {
            // Full path must be a directory, so return full path
            // Ensure to add a trailing slash, as if it's missing it will return parent directory
            return Path.GetDirectoryName(path + '/');
        }

        // Non-existent directory (or ambiguous path without an extension or trailing slash)
        if (!System.IO.File.Exists(path) && String.IsNullOrEmpty(Path.GetExtension(path)))
        {
            // Path is to a non-existent file (without an extension) or to a non-existent directory.
            // As the path does not exist we will standardise and treat it as a directory.
            return Path.GetDirectoryName(path + '/');
        }

        // Path is to a file, return directory
        return Path.GetDirectoryName(path);
    }
    catch (ArgumentException)
    {
        // For wildcards/invalid paths, return the directory
        // This maps C:\Dir\*.csv to C:\Dir
        // Also maps C:\Dir&*A*#$!@& to C:\
        return Path.GetDirectoryName(path);
    }
}

Is there a better way to achieve this behaviour, or my ultimate goal of being able to get the “directory” from a path that may include wildcards?

  • 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-05-26T20:37:37+00:00Added an answer on May 26, 2026 at 8:37 pm

    Do you have any control over the generation of the list of paths? If you could ensure that all paths to directories end with a trailing slash (which I believe is the convention) then the simple Path.GetDirectoryName(path) will work for all these cases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have lots of code like this: FileStream fs = File.Open(@C:\Temp\SNB-RSS.xml, FileMode.Open); using (XmlTextReader
Given paths like this: /data/mirrors/third-party/centos/5/projectA/x86_64 /data/mirrors/third-party/centos/5/projectA/i386 /data/mirrors/third-party/centos/5/projectA/noarch /data/mirrors/third-party/centos/4/projectB/x86_64 /data/mirrors/third-party/centos/4/projectB/i386 /data/mirrors/third-party/centos/4/projectB/noarch /data/mirrors/third-party/centos/4/projectC/x86_64 /data/mirrors/third-party/centos/4/projectC/i386 /data/mirrors/third-party/centos/4/projectC/noarch How
Given file names like these: /the/path/foo.txt bar.txt I hope to get: foo bar Why
Given an absolute or relative path (in a Unix-like system), I would like to
Given that: The asset pipeline is fairly complex (coming from ASP.NET MVC, this is
in my DB I have a column given paths like : \myserver\mydir1\dir2\Atest.txt \myserver\mydir1\dir2\Btest.txt At
/one_path/some_page.php?var1=value1&var2=value2&var3=value3 Suppose I'm given something like the above. Not just the query variable string,
How do I create a directory at a given path, and also create any
Given a path such as "mydir/myfile.txt" , how do I find the file's absolute
Given a path and filename how can I get the Image object: Image image

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.