gpt4 book ai didi

winapi - 获取硬盘所有逻辑驱动器号并收集根目录的有效方法

转载 作者:行者123 更新时间:2023-11-29 08:19:13 26 4
gpt4 key购买 nike

我知道有一个用于类似任务的箱子,但它不是特定于 Windows 的,可能不是最有效的方法。我还希望学习如何使用 Rust 在 C# 中实现我已经知道的事情。

这是我到目前为止尝试过的:read_dir source (更新)

fn main() {

unsafe{
for mut cur_drl in get_win32_ready_drives(){
let mut mcur_drl = cur_drl.clone();
let mut cur_path = PathBuf::from(mcur_drl);

//here i try to print root of current logical
let unwrpd_curfspath = fs::metadata(cur_path).unwrap();
if unwrpd_curfspath.is_dir(){
for entry in (fs::read_dir(cur_path)) {
let curfspath2 = fs::metadata(entry.path()).unwrap();
if curfspath2.is_dir(){

cur_drl.push_str(entry.path());
println!("drive {0}", cur_drl);
}
}
}
}
}
}
// this is the working part of the project so far
// getting all drive letters
//(hdd only actually (3 is hdd & 2 = removable..sd cards etc') into a list
unsafe fn get_win32_ready_drives() -> Vec<String>
{
let mut logical_drives = Vec::with_capacity(5);
let mut bitfield =kernel32::GetLogicalDrives();
let mut drive = 'A';
let mut rtstr = CString::new("");

while bitfield != 0 {
if bitfield & 1 == 1 {

let strfulldl = drive.to_string() + ":\\";
let cstrfulldl = CString::new(strfulldl.clone()).unwrap();
let drvT = kernel32::GetDriveTypeA(cstrfulldl.as_ptr());
if(drvT ==3 || drvT ==2)
{
logical_drives.push(strfulldl);
// println!("drive {0} is {1}", strfdl, drvT);
}
}
drive = std::char::from_u32((drive as u32) + 1).unwrap();
bitfield >>= 1;
}
logical_drives
}

此代码无法编译,因为我遇到了问题了解每种返回类型以及如何正确处理其结果的迭代。

最佳答案

几乎没有来自 this post 的帮助

extern crate kernel32;
extern crate libc;
use std::fs::{self, DirEntry};
use std::path::Path;
use std::path::PathBuf;.
fn main() {

unsafe{
for mut cur_drl in get_win32_ready_drives(){
let mut mcur_drl = cur_drl.clone();
let mut cur_path = PathBuf::from(mcur_drl.clone());

//print root of each logical drive letter
let unwrpd_curfspath = fs::metadata(cur_path).unwrap();
if unwrpd_curfspath.is_dir(){

for entry in fs::read_dir(mcur_drl).unwrap() {

println!("drive {0}", entry.unwrap().path().display());
}
}
}
}
}

//getting all drive letters
unsafe fn get_win32_ready_drives() -> Vec<String>
{
let mut logical_drives = Vec::with_capacity(5);
let mut bitfield =kernel32::GetLogicalDrives();
let mut drive = 'A';
let mut rtstr = CString::new("");

while bitfield != 0 {
if bitfield & 1 == 1 {

let strfulldl = drive.to_string() + ":\\";
let cstrfulldl = CString::new(strfulldl.clone()).unwrap();
let x = kernel32::GetDriveTypeA(cstrfulldl.as_ptr());
if(x ==3 || x ==2)
{
logical_drives.push(strfulldl);
// println!("drive {0} is {1}", strfdl, x);
}
}
drive = std::char::from_u32((drive as u32) + 1).unwrap();
bitfield >>= 1;
}
logical_drives
}

我希望其他一些新手可以利用这个

关于winapi - 获取硬盘所有逻辑驱动器号并收集根目录的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33654653/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com