Mecki
Mecki

Reputation: 59

How to get the name of the directory from where an executable via link was started?

I have written a small program which should change some text files according to the name of the folder they are located in. This works fine as long as the executable is in the folder, too. But to not have many copies of the program I would like to start it via a link. How do I find find the location of the link which has triggered the executable? I tried:

let path_parts: Vec<_> = env::current_dir().unwrap()
    .components()
    .map(|part| part.as_os_str().to_ascii_lowercase())
    .collect();

But running under win10 this does only work as long as the executable is in the target directory where it was compiled. The executable moved to another location, it will give the directory name, where the executable is located, not the link.

Upvotes: 0

Views: 522

Answers (1)

Jmb
Jmb

Reputation: 23244

On Windows, links to programs contain the location in which the program should be started. This defaults to the folder of the executable, but can be changed by editing the link properties (e.g. with Alt-Enter or choosing "Properties" from the context menu).

Upvotes: 2

Related Questions