user2619395
user2619395

Reputation: 249

How to get the full path?

Overall I am trying to take off the file name off the end of a path and display the rest of the path. For example this path: C:\Users\Documents\Development\testing\11.0.25.10\AUW_11052_0_X.pts

I want to strip off the "AUW_11052_0_X.pts" file and only display "C:\Users\Documents\Development\testing\11.0.25.10\"

How exactly does one go about this? I'm not sure of how to make this into a regex pattern.

Heres my problem more detailed:

What I was going to do was create a regex function that took in two arguments, the original path then the file name (the program at some point loops through a list where all the files are stored and gets the file name from that) I was going to create a regex function that grabbed anything after the last "\" charachter and compared it with the file name argument. If it indeed matches than strip the file name off and if it doesnt then just leave it be. My problem is figuring out of how to do a regex pattern that finds anything after the last "\" charachter which I dont know how

ANSWER: I found the answer, and the pattern is ^(.*[\\\/]) this will grab every character up to the last "\"

Upvotes: 2

Views: 150

Answers (2)

Christopher Klein
Christopher Klein

Reputation: 2793

Why can't you just do something like this?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace directorysearch
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo di = new DirectoryInfo(@"c:\users\documents\development\testing");
            FileInfo[] files = null;
            DirectoryInfo[] dirs = null;
            dirs = di.GetDirectories();
            foreach (DirectoryInfo dir in dirs)
            {
                files = dir.GetFiles("*", SearchOption.TopDirectoryOnly);
            }
        }
    }
}

This allows you to enter the base path (c:\users\documents\development\testing) and then manipulate each individual directory under that and then each individual file under each directory?

Upvotes: 0

SLaks
SLaks

Reputation: 887453

You're looking for Path.GetDirectoryName().

Upvotes: 11

Related Questions