如何仅获取当前可执行文件路径的目录部分? [英] How to get only the directory portion of the current executable's path?

查看:83
本文介绍了如何仅获取当前可执行文件路径的目录部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从可执行文件所在目录的config文件夹中读取文件.我使用以下功能来做到这一点:

I want to read files from a config folder at the directory where the executable is located. I do that using the following functions:

use std::env;

// add part of path to te path gotten from fn get_exe_path();
fn get_file_path(path_to_file: &str) -> PathBuf {
    let final_path = match get_exe_path() {
        Ok(mut path) => {
            path.push(path_to_file);
            path
        }
        Err(err) => panic!("Path does not exists"),
    };
    final_path
}

// Get path to current executable
fn get_exe_path() -> Result<PathBuf, io::Error> {
    //std::env::current_exe()
    env::current_exe()
}

在我的情况下, get_exe_path()将返回 C:\ Users \ User \ Documents \ Rust \ Hangman \ target \ debug \ Hangman.exe .

In my case, get_exe_path() will return C:\Users\User\Documents\Rust\Hangman\target\debug\Hangman.exe.

使用 get_file_path("Config \ test.txt"),我要将 Config \ test.txt 附加到上述路径.然后,我得到文件的以下路径: C:\ Users \ User \ Documents \ Rust \ Hangman \ target \ debug \ Hangman.exe \ Config \ test.txt

With get_file_path("Config\test.txt"), I want to append Config\test.txt To the above path. Then I get the following path to the file: C:\Users\User\Documents\Rust\Hangman\target\debug\Hangman.exe\Config\test.txt

问题在于, std :: env :: current_exe()也将获得可执行文件的文件名,而我并不需要它.我只需要它所在的目录.

The problem is that std::env::current_exe() will get the file name of the executable also and I do not need that. I only need the directory where it is located.

问题

以下以下函数调用应返回 C:\ Users \ User \ Documents \ Rust \ Hangman \ target \ debug \ Config \ test.txt :

The following the following function call should return C:\Users\User\Documents\Rust\Hangman\target\debug\Config\test.txt:

let path = get_file_path("Config\\test.txt");

如上例所示,如何从当前目录获取没有可执行文件名的路径?除了使用 std :: env :: current_exe()

How can I get the path from the current directory without the executable name like above example? Are there any other ways to do this than using std::env::current_exe()

推荐答案

PathBuf :: pop PathBuf :: push :

self 截断为 self.parent .

返回 false ,如果 self.file_name None ,则不执行任何操作.除此以外,返回 true .

Returns false and does nothing if self.file_name is None. Otherwise, returns true.

在您的情况下:

use std::env;
use std::io;
use std::path::PathBuf;

fn inner_main() -> io::Result<PathBuf> {
    let mut dir = env::current_exe()?;
    dir.pop();
    dir.push("Config");
    dir.push("test.txt");
    Ok(dir)
}

fn main() {
    let path = inner_main().expect("Couldn't");
    println!("{}", path.display());
}

还有可能使用 Path:: parent :

There's also the possibility of using Path::parent:

返回 Path ,不包含最终组成部分(如果有的话).

Returns the Path without its final component, if there is one.

如果路径以根或前缀结尾,则返回 None .

Returns None if the path terminates in a root or prefix.

在您的情况下:

fn inner_main() -> io::Result<PathBuf> {
    let exe = env::current_exe()?;
    let dir = exe.parent().expect("Executable must be in some directory");
    let mut dir = dir.join("Config");
    dir.push("test.txt");
    Ok(dir)
}

另请参阅:

这篇关于如何仅获取当前可执行文件路径的目录部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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