寻找那个目录 [英] Seeking that Directory
问题描述
嗨.
正在遍及System.IO命名空间.我在错误的道路上吗?我只想通过仅知道现有目录名称来获取我选择的任何目录的完整限定路径(字符串).
有什么建议吗?
祝你有美好的一天.
Hi.
Being looking all over the System.IO namespace. Am I on the wrong path her? I just want to get a full qualified path (string) to any directory i choose by knowing just that existing directorys name.
Please, any suggestions?
Have a nice day.
推荐答案
如果您要的是:
提供目录名称(例如"MyExcelSpreadheets2010")
调用一个方法,该方法将返回该目录的标准路径.
If what you are asking for is:
Supply a directory name ("MyExcelSpreadheets2010" for example)
Call a method which returns the fully qualified path to that directory.
string fileName = "MyExcelSpreadheets2010";
string path = GetFullPathFromJustTheDirectoryName(fileName);
包含"C:\ WhereIKeepThings \ Documents \ Mine \ ThisYear \ MyExcelSpreadheets2010"的预期路径
那就不要.它不存在.您将不得不编写它.
考虑一下:
如果不是在C:上,而是在DVD驱动器上,该怎么办?还是其他硬盘?还是网络驱动器? USB驱动器,哪一个恰好在您的口袋里?
如果您有两个名称相同的目录怎么办?我会-很多. "bin"和"debug"是主要示例,但还有很多其他示例.
那只是垫子.我不能为茂密的森林找到所有的树木.我的意思是.编写无需考虑所提到问题即可获得路径的代码.无论是否存在问题,我都可以解决.让我们说这是在C:\上.我现在需要的是朝正确的方向发展.按什么顺序使用什么分类和方法可以有很大帮助."
啊!你应该说的!
您需要设置一个递归方法来查看给定路径中的所有目录.
这会有点简短,因为今天我被抽空了(工作只是阻碍了,不是吗?)您需要查看Directory
类:
expected path to contain "C:\WhereIKeepThings\Documents\Mine\ThisYear\MyExcelSpreadheets2010"
Then no. It doesn''t exist. You will have to write it.
Think about it:
What if it isn''t on C:, but on your DVD drive? Or your other HDD? Or a networked drive? USB drive, which happens to be in your pocket?
What if you have two directories with the same name? I do - lots. "bin" and "debug" are prime examples, but there are loads of others.
"Thats just the mattar. I cant se all the trees for the hole forest. What I mean is. Writing the code that gets the path without consideration to the mentiond problems. The problems where it is or is not, I can firure out. Lets just say it is on C:\. What I need just now is a push in the right direction. What Clases and methods, to use in what order can be a great help."
Ah! You should have said!
You need to set up a recursive method which looks at all the directories in a given path.
This will be a bit brief, because I''m pushed for time today (work just gets in the way, doesn''t it?) You need to look at the Directory
class:
bool Directory.Exists(path)
string[] Directory.GetDirectories(path)
在MSDN上查找它们-可能有示例.
您可能需要为递归方法使用某种找到它-中止"标志,并且我建议在C:\的一个小子分支上进行测试-可能值得建立一个具有已知结构的小型测试目录进行测试.
Look them up on MSDN - there are probably examples.
You will probably need to have some kind of "found it - abort" flag for your recursive method, and I recommend testing on a small sub-branch of C:\ - it is probably worth setting up a small test directory with a known structure for testing.
这篇关于寻找那个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!