开发者

How to determine a full path of a named folder?

开发者 https://www.devze.com 2022-12-16 13:47 出处:网络
I want to determine the full path of certain folders. In my array, I just have the names of the folders, but when my application will get installed on another user\'s machine, my program must be able

I want to determine the full path of certain folders. In my array, I just have the names of the folders, but when my application will get installed on another user's machine, my program must be able to determine the full-path of these folders.

How to get 开发者_如何学Pythonthe fullpath?


You can check the method Path.GetFullPath, it could be useful to what you're trying to do.

Path.GetFullPath Method


Did you mean the My Documents folder and the rest? It's not obvious for me from your question.

The My Documents folder is:

 Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

And the rest of the special system folders can be retrieved in a similar way.


By prefixing it with another Path. Which Path depends on your application.

string path = ...
string fullPath = System.IO.Path.Combine(path, folderNames[i]);

You could take a look at Environment.GetFolderPath(...)


Sounds to me that you mean actually the current path plus an additional path. I.e., suppose your application is installed in c:\installations and you need the relative path of resource\en-US, you want to find c:\installations\resource\en-US.

Normally I would go for getting the current path, but in Windows it is possible to start an application as if it is executing from a different path. A fool-proof way of getting the path of the current application (where it is installed) is as follows:

// gets the path of the current executing executable: your program
string path = Assembly.GetExecutingAssembly().Location;

// transform it into a real path...
FileInfo info = new FileInfo(path);

// ...to make it easier to retrieve the directory part
string currentPath = info.Directory.FullName;

Now it becomes trivial to get new paths.

string someRelativePath = @"reource\en-US";
string someFullPath = Path.Combine(currentPath, someRelativePath);

I know, it looks a bit contrived, but it is safer then using the current path.


FileInfo.FullName, if you are lucky and the constructor finds your file somehow (e.g. current working directory).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号