注册。按文件夹名称获取完整路径 [英] Reg. Get Full Path By Folder Name

查看:63
本文介绍了注册。按文件夹名称获取完整路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我希望通过传递文件夹名称获得完整路径



意味着例如:如果我的文件夹名称在C:\Home \ test中是test,如果我将通过唯一的测试,那么它应该回复C:\Home \ test

Hi,

I want to get the full path by passing the folder name

Means Ex: if my folder name is "test" inside the "C:\Home\test" and if I will pass the only "test " then it should reply "C:\Home\test"

推荐答案

使用 Path.GetFullPath方法 [ ^ ]:

Use the Path.GetFullPath Method[^]:
using System.IO; // add this at the top of your code file




string fullpath = Path.GetFullPath("test");



希望这会有所帮助。


Hope this helps.


这不完全是解决方案1建议什么。相反,你需要使用 System.IO.Path.CombinePath

http://msdn.microsoft.com/en-us/library/fyy7a5kt.aspx [ ^ ]。



另外,你可以做它精简:
This is not exactly what Solution 1 recommends. Rather, you need to use System.IO.Path.CombinePath:
http://msdn.microsoft.com/en-us/library/fyy7a5kt.aspx[^].

Alternatively, you can do it literately:
string fullPath = string.Format(
    "{0}{1}{2}",
    System.IO.Path.PathSeparator, // this is the key: don't hard-code '\'; it would be not platform-portable
    System,
    fileName);





祝你好运,

-SA


这个开始生活作为评论,但我想我要说的是保证将此作为解决方案发布。我认为很有可能我尊敬的同事谢尔盖和ProgramFox已经充分回答了这个问题(我已经对他们的回答进行了投票)。但是,本着不信任,质疑一切的精神,我会提出一个可能令人困惑的场景:因为我想知道,如果文件夹test包含在当前工作目录中,如果解决方案具有微不足道的性质,用户可能会问一个不同的问题。



无论如何我希望这个响应对OP对.NET目录的了解有所贡献:如果没有,请做:投票吧。




考虑到这里可能缺少一条非常重要的信息:当OP传入文件夹名称时,他们会:



a。 已经有文件夹(或桌面或任何地方)的路径传入的文件夹包含在某些变量的捕获中?



......或者......



b。他们确实知道,他们的应用程序的当前工作目录包含文件夹test。



如果他们已经将路径捕获,或者确定路径包含在当前工作目录中:那么这个问题就变得微不足道了:只需使用已捕获的路径,并将文件夹名称附加到其中。或者,只需使用Path.GetFullPath()来添加当前工作目录



但是,如果正在运行的程序没有知道文件夹的位置如果文件夹test包含在当前工作目录中,则找到测试并且正在运行的程序确定:这会成为完全不同的问题,因为:



Path.GetFullPath()是一个哑函数:你可以将任何文件或文件夹名称传递给它,它将返回当前工作目录路径前置... 是否该文件夹包含在当前工作目录路径中!



所以,如果OP在上面概述的场景所暗示的广义上提出了这个问题,然后Path.GetFullPath()可能会返回误报。



要处理这种替代方案:



1.首先:使用Path.GetFullPath(test)获取完全限定的文件路径。



2.然后:确定是否存在完全限定的文件路径。



3.如果存在:



a。如果文件路径存在,并且您确定所需的文件夹test的一个实例位于当前工作目录中:您已完成。



b。如果文件路径存在于当前工作目录中,但您认为可能是其他位置中名为test的其他文件夹,并且您想要从中选择,如果有:那么您已经得到某种搜索,得到结果,如果有多个名为test:的文件夹,请选择你想要的那个。



4.(误报)如果文件夹test,但当前工作目录中:那么你必须搜索某种类型,你必须考虑范围那个搜索(整个网络?所有硬盘?只有一个根目录?)。
This "began life" as a comment, but I think what I have to say warrants posting this as a solution. I consider it quite possible that my esteemed colleagues Sergey, and ProgramFox, have adequately answered this question (I've upvoted both their responses). But, in the spirit of "trust nothing, question everything," I'll pose a possible confounding scenario: because I wonder, given the trivial nature of the solution if the folder "test" is contained in the current working Directory, if the user might be asking a different question.

In any case I hope this response makes some contribution to the OP's knowledge of directories in .NET: if not, please do: vote it down.


Consider that a very important piece of information may be missing here: when the OP "passes in" a folder name do they:

a. already have the path to the folder (or desktop, or wherever) the folder passed-in is contained in "captured" in some variable ?

... or ...

b. do they know, for certain, that the current working Directory of their application contains the folder, "test." ?

If they already have the path "captured," or, are certain the path is contained in the current working Directory: then this problem becomes trivial: just use the path you have "captured," and append the folder name to it. Or, just use Path.GetFullPath() to prepend the current working Directory

However, if the running program doesn't "know" where the folder "test" is located, and the running program is not "sure" if the folder "test" is contained in the current working Directory: this becomes an entirely different problem because:

Path.GetFullPath() is a "dumb" function: you can hand any file, or folder, name to it, and it will return the current working Directory path prepended to it ... whether or not that folder is contained in the current working Directory path !

So, if the OP is asking this question in the broader sense implied by the scenario I've outlined above, then Path.GetFullPath() may return a "false positive."

To handle this alternate scenario:

1. first: use Path.GetFullPath("test") to get a fully qualified filepath.

2. then: determine if the fully qualified filepath exists.

3. if it exists:

a. if the filepath exists, and you are sure the one instance of the folder "test" you want is in the current working Directory: you're done.

b. if the filepath exists in the current working Directory, but you think there may be other folders named "test" in other locations, and, you want to choose from them, if there are: then you've got to do some kind of a search, get the results, and, if there are multiple folders named "test:" select which one(s) you want.

4. (false positive) if the folder "test," however, does not exist in the current working Directory: then you have to do a search of some type, and you have to consider the "scope" of that search (the whole network ? all hard-drives ? only a certain "root" Directory ?).


这篇关于注册。按文件夹名称获取完整路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆