Path.GetDirectoryName(fpath) c#

I am using Path.GetDirectoryName(fpath) to get me the full path of the file

but what I want to do is just grab the directory from a particular sub direcorty onwards

and also grab the last directory

se using the example below I currently get :

dpath = Path.GetDirectoryName(fpath);

gives me ...

"C:\\Users\\Documents\\Visual Studio 2008\\Projects\\MeasurLink7\\Mitutoyo.MeasurLink.RealTime"

but what I want is

1) from Projects onwards --
dpath1 = "\\MeasurLink7\\Mitutoyo.MeasurLink.RealTime"

2 the last directory

dpath2 = "\\Mitutoyo.MeasurLink.RealTime"

any help would be appreciated
Jayesh AcharyaTechnichal ConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

       dpath  = Path.GetDirectoryName(fpath);
        string dir = "Projects";
        string dpath1 = dpath.Substring(dpath.IndexOf(dir) + dir.Length, dpath.Length - (dpath.IndexOf(dir) + (dir.Length * 2)));

        IO.DirectoryInfo dirPath = New IO.DirectoryInfo(dpath);
        string dpath2 = dirPath.Name

You will have to test in C# as this is converted from on the fly

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
käµfm³d 👽Commented:
eeeewwww....   Substring() calls   : P

While the_o's solution is feasilbe, I think you would benefit from the readability from using something like FileInfo class:
FileInfo info = new FileInfo(path);
string dPath1 = info.FullName.Replace(info.Directory.Parent.FullName, string.Empty);
string dPath2 = info.FullName.Replace(info.Directory.FullName, string.Empty);

Open in new window

:) Wanted 2 give 2 solutions so used substring and directory info. I sometimes take things for granted and expected the user of the solution to interpret (ie if there is a directoryinfo there should be a fileinfo).

Will try to be more concise next time.
käµfm³d 👽Commented:

There's nothing wrong with your proposal. My personal preference is to favor intuitive code for the benefit of those following behind me. Both solutions accomplish the same thing, but I believe what I posted is more evident in its intent. The difference is very negligible though.

I was just kidding you about the Substring() call  = )
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.