使用Rust,在文件上打开资源管理器

5

如果想在文件资源管理器中显示一个文件,或者使用类似于OSX上的“Reveal in Finder”功能,那么在Rust中该如何实现呢?有没有可以帮助的crate呢?

fn main(){
   reveal_file("tmp/my_file.jpg")
   //would bring up the file in a File Explorer Window
}

我正在寻找类似于这个Python解决方案。


(翻译:我在寻找类似于这个Python解决方案的东西。)
2个回答

6
你可以使用Command来打开finder进程。

macOS

use std::process::Command;

fn main( ) {
    println!( "Opening" );
    Command::new( "open" )
        .arg( "." ) // <- Specify the directory you'd like to open.
        .spawn( )
        .unwrap( );
}

Windows

use std::process::Command;

fn main( ) {
    println!( "Opening" );
    Command::new( "explorer" )
        .arg( "." ) // <- Specify the directory you'd like to open.
        .spawn( )
        .unwrap( );
}

补充说明:

根据@hellow的评论。

Linux

use std::process::Command;

fn main( ) {
    println!( "Opening" );
    Command::new( "xdg-open" )
        .arg( "." ) // <- Specify the directory you'd like to open.
        .spawn( )
        .unwrap( );
}

我相信你提供的链接已经失效了。 - ANimator120
@ANimator120 已修复。我还添加了如何在 Windows 上执行此操作的说明(在重新阅读您的问题后,我认为这就是您想要的)。 - WBuck
2
在Linux上,您可以使用xdg-open - hellow
1
我猜这个没有跨平台的方法可以做到吧? - ANimator120

2
将以下内容添加到您的Cargo.toml文件中。
[dependencies]
open = "3"

...并使用...打开某物...

open::that(".");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接