可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I use this code for finding the debug directory
public string str_directory = Environment.CurrentDirectory.ToString();
"C:\\Users\\Masoud\\Documents\\Visual Studio 2008\\Projects\\MyProj\\MyProj\\bin\\Debug"
How can I find the parent folder as shown below?
"C:\\Users\\Masoud\\Documents\\Visual Studio 2008\\Projects\\MyProj\\MyProj"
回答1:
You can use System.IO.Directory.GetParent() to retrieve the parent directory of a given directory.
回答2:
string parent = System.IO.Directory.GetParent(str_directory).FullName;
See BOL
回答3:
If you append ..\..
to your existing path, the operating system will correctly browse the grand-parent folder.
That should do the job:
System.IO.Path.Combine("C:\\Users\\Masoud\\Documents\\Visual Studio 2008\\Projects\\MyProj\\MyProj\\bin\\Debug", @"..\..");
If you browse that path, you will browse the grand-parent directory.
回答4:
I've found variants of System.IO.Path.Combine(myPath, "..")
to be the easiest and most reliable. Even more so if what northben says is true, that GetParent requires an extra call if there is a trailing slash. That, to me, is unreliable.
Path.Combine makes sure you never go wrong with slashes.
..
behaves exactly like it does everywhere else in Windows. You can add any number of \..
to a path in cmd or explorer and it will behave exactly as I describe below.
Some basic ..
behavior:
- If there is a file name,
..
will chop that off:
Path.Combine(@"D:\Grandparent\Parent\Child.txt", "..")
=> D:\Grandparent\Parent\
- If the path is a directory,
..
will move up a level:
Path.Combine(@"D:\Grandparent\Parent\", "..")
=> D:\Grandparent\
..\..
follows the same rules, twice in a row:
Path.Combine(@"D:\Grandparent\Parent\Child.txt", @"..\..")
=> D:\Grandparent\
Path.Combine(@"D:\Grandparent\Parent\", @"..\..")
=> D:\
- And this has the exact same effect:
Path.Combine(@"D:\Grandparent\Parent\Child.txt", "..", "..")
=> D:\Grandparent\
Path.Combine(@"D:\Grandparent\Parent\", "..", "..")
=> D:\
回答5:
To get a 'grandparent' directory, call Directory.GetParent() twice:
var gparent = Directory.GetParent(Directory.GetParent(str_directory).ToString());
回答6:
Like this:
System.IO.DirectoryInfo myDirectory = new DirectoryInfo(Environment.CurrentDirectory);
string parentDirectory = myDirectory.Parent.FullName;
Good luck!
回答7:
Directory.GetParent
is probably a better answer, but for completeness there's a different method that takes string and returns string: Path.GetDirectoryName
.
string parent = System.IO.Path.GetDirectoryName(str_directory);
回答8:
You might want to look into the DirectoryInfo
.Parent
property.
回答9:
You shouldn't try to do that. Environment.CurrentDirectory gives you the path of the executable directory. This is consistent regardless of where the .exe file is. You shouldn't try to access a file that is assumed to be in a backwards relative location
I would suggest you move whatever resource you want to access into a local location. Of a system directory (such as AppData)
回答10:
To avoid issues with trailing \, call it this way:
string ParentFolder = Directory.GetParent( folder.Trim('\\')).FullName;
回答11:
This is the most common way -- it really depends on what you are doing exactly:
(To explain, the example below will remove the last 10 characters which is what you asked for, however if there are some business rules that are driving your need to find a specific location you should use those to retrieve the directory location, not find the location of something else and modify it.)
// remove last 10 characters from a string
str_directory = str_directory.Substring(0,str_directory.Length-10);
回答12:
No one has provided a solution that would work cross-form. I know it wasn't specifically asked but I am working in a linux environment where most of the solutions (as at the time I post this) would provide an error.
Hardcoding path separators (as well as other things) will give an error in anything but Windows systems.
In my original solution I used:
char filesep = Path.DirectorySeparatorChar;
string datapath = $"..{filesep}..{filesep}";
However after seeing some of the answers here I adjusted it to be:
string datapath = Directory.GetParent(Directory.GetParent(Directory.GetCurrentDirectory()).FullName).FullName;
回答13:
IO.Path.GetFullPath(@"..\..")
If you clear the "bin\Debug\
" in the Project properties -> Build -> Output path, then you can just use AppDomain.CurrentDomain.BaseDirectory
回答14:
To get your solution
try this
string directory = System.IO.Directory.GetParent(System.IO.Directory.GetParent(Environment.CurrentDirectory).ToString()).ToString();