finder-wpf / FastSeekWpf /Core /DriveDiscovery.cs
anshdadhich's picture
DriveDiscovery: Add missing using System (for StringComparison)
022ab12 verified
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using FastSeekWpf.NativeInterop;
namespace FastSeekWpf.Core;
public static class DriveDiscovery
{
public static List<NtfsDrive> GetNtfsDrives()
{
var drives = new List<NtfsDrive>();
char[] buffer = new char[256];
uint len = Win32Api.GetLogicalDriveStringsW((uint)buffer.Length, buffer);
if (len == 0) return drives;
// Parse null-separated drive strings (same as Rust buf[..len].split(|&c| c == 0))
var driveStrings = new List<string>();
var sb = new StringBuilder();
for (int i = 0; i < len; i++)
{
if (buffer[i] == '\0')
{
if (sb.Length > 0)
{
driveStrings.Add(sb.ToString());
sb.Clear();
}
}
else
{
sb.Append(buffer[i]);
}
}
foreach (var root in driveStrings)
{
if (IsNtfs(root))
{
drives.Add(new NtfsDrive
{
Letter = root[0],
Root = root
});
}
}
return drives;
}
private static bool IsNtfs(string root)
{
var fsName = new StringBuilder(32);
bool ok = Win32Api.GetVolumeInformationW(
root,
null!, 0,
out _, out _, out _,
fsName, (uint)fsName.Capacity);
if (!ok) return false;
return fsName.ToString().StartsWith("NTFS", StringComparison.OrdinalIgnoreCase);
}
}