Link to home
Create AccountLog in
Avatar of Jayesh Acharya
Jayesh AcharyaFlag for United States of America

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of the_o
the_o

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of kaufmed
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

SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
@the_o

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  = )