【发布时间】:2018-03-26 17:42:24
【问题描述】:
我想从可执行文件所在目录的配置文件夹中读取文件。我使用以下函数来做到这一点:
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。
使用get_file_path("Config\test.txt"),我想将Config\test.txt附加到上述路径。然后我得到以下文件路径:C:\Users\User\Documents\Rust\Hangman\target\debug\Hangman.exe\Config\test.txt
问题是std::env::current_exe() 也会得到可执行文件的文件名,我不需要那个。我只需要它所在的目录。
问题
以下函数调用应返回C:\Users\User\Documents\Rust\Hangman\target\debug\Config\test.txt:
let path = get_file_path("Config\\test.txt");
我如何从当前目录获取路径,而不像上面的示例那样包含可执行文件名称?除了使用std::env::current_exe()
【问题讨论】: