简体   繁体   English

C#中文件夹的完整路径

[英]full path of a folder in c#

我需要使用c#在Windows项目中获取完整的文件夹路径。我尝试使用path.getFulPath(filename).bt来返回应用程序路径+文件名。我如何获取实际路径,例如“ D:\\ eclipse_files \\ ads_data” ?

A relative path such as myfile.txt is always resolved in relation to the current working directory. 始终会相对于当前工作目录解析相对路径(例如myfile.txt )。

In your case the current working directory seems to be D:\\eclipse_files\\ads_data so your relative file path gets resolved to D:\\eclipse_files\\ads_data\\myfile.txt when you call Path.GetFullPath . 在您的情况下,当前工作目录似乎是D:\\eclipse_files\\ads_data因此,当您调用Path.GetFullPath时,您的相对文件路径将解析为D:\\eclipse_files\\ads_data\\myfile.txt

To solve the problem, either make sure that you start with an absolute path from the beginning, or, that your working directory is set correctly. 要解决该问题,请确保从头开始使用绝对路径,或者确保正确设置了工作目录。

You can get/set the working directory using the Directory.GetCurrentDirectory and Directory.SetCurrentDirectory methods. 您可以使用Directory.GetCurrentDirectoryDirectory.SetCurrentDirectory方法获取/设置工作目录。

您的问题不是很清楚,但我认为您正在寻找这个:

string path = Path.GetDirectoryName(filename);

If I have understood correctly, you have a filename, for example 'doc.txt', and you want to have a method to return the full path of this file regardless of where the application runs from? 如果我正确理解,那么您有一个文件名,例如'doc.txt',并且想要有一种方法可以返回此文件的完整路径,而不管应用程序从何处运行?

If this is what you ask it is not possible. 如果这是您的要求,那是不可能的。 Have you considered that there might be several files called 'doc.txt' on your harddrives? 您是否考虑过硬盘上可能存在多个名为“ doc.txt”的文件?

The best you can hope to do it to search all harddrives, and return a list of all files found with the same name, but that will just be ridicously slow. 您最好希望这样做可以搜索所有硬盘驱动器,并返回找到的所有具有相同名称的文件的列表,但这将非常缓慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM