File size: 1,693 Bytes
a0a138d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
022ab12
a0a138d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9961292
a0a138d
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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);
    }
}