简体   繁体   中英

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.

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 .

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.

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

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?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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