默认项目中下载并保存用户目录图片 执行一个命令行
进行简单的练习
注意环境搭建
编写了rust 代码后是自动进行更新的
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
#[tauri::command]
fn greet1(name: &str) -> String {
format!("Hello, {}! You've been greeted from Rust!", name)
}
use std::env;
use std::path::PathBuf;
use std::process::Command;
use std::fs::File;
use std::io::prelude::*;
use reqwest::Client;
use tokio::fs;
#[tauri::command]
async fn greet() -> String {
// 获取用户主目录
let mut save_dir = env::var("HOME").unwrap_or_else(|_| ".".to_string());
let mut save_path = PathBuf::from(&save_dir);
save_path.push("wzskyline-image.svg");
// 创建一个reqwest客户端
let client = Client::new();
// 下载图片
let response = client.get("https://visualstudio.microsoft.com/wp-content/uploads/2022/09/VisualStudioCode.svg")
.send()
.await
.unwrap()
.bytes()
.await
.unwrap();
// 将图片保存到文件
let mut file = File::create(&save_path).unwrap();
file.write_all(&response).unwrap();
// 执行ipconfig命令
let output = Command::new("ipconfig")
.output()
.expect("failed to execute ipconfig");
let output_str = String::from_utf8_lossy(&output.stdout).to_string();
// 返回结果
output_str
}
#[tauri::command]
fn greet2() -> String {
// 执行ipconfig命令
let output = Command::new("ipconfig")
.output()
.expect("failed to execute ipconfig");
let output_str = String::from_utf8_lossy(&output.stdout).to_string();
// 返回结果
output_str
}
fn main() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![greet])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}