Reputation: 13636
I am using Directory.GetCurrentDirectory()
function. After I get current directory path I need to move two directories up and to get the path of this derictory to the path variable.
Any idea how I can do that?
Upvotes: 0
Views: 759
Reputation: 6358
If you actually want the Directory.GetCurrentDirectory()
method to return a different directory, you'll need to use the matching Directory.SetCurrentDirectory()
method to change the working directory of the application.
http://msdn.microsoft.com/en-us/library/system.io.directory.setcurrentdirectory.aspx
If you're wanting to move down, you can use the Directory.GetDirectories()
method to view all folders in the current path, and then use the Directory.SetDirectory()
method to set it to one of those. Repeat once more to get "two directories down". Sans error handling (and presuming you want the first directory each time):
var directory = Directory.GetCurrentDirectory();
var directories = Directory.GetDirectories(directory);
directory = directories.First();
directories = Directory.GetDirectories(directory);
directory = directories.First();
If you do actually mean UP rather than down, then as others have said, use the Directory.GetParent()
method to get the parent of the current directory... twice.
Upvotes: 1
Reputation: 150313
Assuming you meant moving up, not down:
var x = new DirectoryInfo(Directory.GetCurrentDirectory());
var result = x.Parent.Parent;
You can now get all the data of that directory like:
var fullName = result.FullName;
Upvotes: 2
Reputation: 7122
You mean two directories up? If you are moving up, use Directory.GetParent. You cannot arbitrarily move down, you need to know where you are going.
Upvotes: 5