Reputation: 4662
This is probably simple but I'm trying to get the directory root in my code.
Here is my code:
var appdir = AppDomain.CurrentDomain.BaseDirectory;
var ppsdir = Directory.GetParent(appdir).FullName;
appdir is coming back as "C:\\Program Files (x86)\\PPS\\PpsUpdate\\"
but ppsdir is coming back as "C:\\Program Files (x86)\\PPS\\PpsUpdate"
I need the ppsdir to be "C:\Program Files (x86)\PPS" so I'm not sure what I am doing wrong.
Upvotes: 0
Views: 82
Reputation: 3762
Try this one instead:
var appdir = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);
var ppsdir = Directory.GetParent(appdir).FullName;
It should give you the correct dir.
The Path.GetDirectoryName will get the path name up until and not including the last DirectorySeparatorChar. See MSDN. The Directory.GetParent gets the parent directory from the string we hand it.
Upvotes: 2
Reputation: 32586
See Directory.GetParent in MSDN:
The string returned by this method consists of all characters in the path up to, but not including, the last DirectorySeparatorChar or AltDirectorySeparatorChar.
So in our case Directory.GetParent
seems just to cut the last \
.
As @CodeCaster suggested, you may use TrimEnd in order to get rid of trailing \
s.
Upvotes: 2