Skip to content

Commit 19d36f6

Browse files
committed
Remove Vanara from DriveHelpers
1 parent 4753ceb commit 19d36f6

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

src/Files.App.CsWin32/NativeMethods.txt

+1
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,4 @@ IFileOperation
133133
IShellItem2
134134
PSGetPropertyKeyFromName
135135
ShellExecuteEx
136+
QueryDosDevice

src/Files.App/Utils/Storage/Helpers/DriveHelpers.cs

+39-18
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
// Copyright (c) 2024 Files Community
22
// Licensed under the MIT License. See the LICENSE.
33

4+
using DiscUtils.Udf;
45
using Microsoft.Management.Infrastructure;
5-
using System.IO;
66
using Windows.Devices.Enumeration;
77
using Windows.Devices.Portable;
88
using Windows.Storage;
99
using Windows.Storage.FileProperties;
10-
using DiscUtils.Udf;
10+
using Windows.Win32;
11+
using Windows.Win32.Foundation;
1112

1213
namespace Files.App.Utils.Storage
1314
{
@@ -52,12 +53,12 @@ public static async Task<bool> CheckEmptyDrive(string? drivePath)
5253

5354
public static async Task<StorageFolderWithPath> GetRootFromPathAsync(string devicePath)
5455
{
55-
if (!Path.IsPathRooted(devicePath))
56+
if (!SystemIO.Path.IsPathRooted(devicePath))
5657
return null;
5758

5859
var drivesViewModel = Ioc.Default.GetRequiredService<DrivesViewModel>();
5960

60-
var rootPath = Path.GetPathRoot(devicePath);
61+
var rootPath = SystemIO.Path.GetPathRoot(devicePath);
6162
if (devicePath.StartsWith(@"\\?\", StringComparison.Ordinal)) // USB device
6263
{
6364
// Check among already discovered drives
@@ -102,41 +103,61 @@ public static async Task<StorageFolderWithPath> GetRootFromPathAsync(string devi
102103
return null;
103104
}
104105

105-
public static Data.Items.DriveType GetDriveType(System.IO.DriveInfo drive)
106+
public static DriveType GetDriveType(SystemIO.DriveInfo drive)
106107
{
107-
if (drive.DriveType is System.IO.DriveType.Unknown)
108+
if (drive.DriveType is SystemIO.DriveType.Unknown)
108109
{
109110
string path = PathNormalization.NormalizePath(drive.Name);
110111

111112
if (path is "A:" or "B:")
112-
return Data.Items.DriveType.FloppyDisk;
113+
return DriveType.FloppyDisk;
113114
}
114115

115116
return drive.DriveType switch
116117
{
117-
System.IO.DriveType.CDRom => Data.Items.DriveType.CDRom,
118-
System.IO.DriveType.Fixed => Data.Items.DriveType.Fixed,
119-
System.IO.DriveType.Network => Data.Items.DriveType.Network,
120-
System.IO.DriveType.NoRootDirectory => Data.Items.DriveType.NoRootDirectory,
121-
System.IO.DriveType.Ram => Data.Items.DriveType.Ram,
122-
System.IO.DriveType.Removable => Data.Items.DriveType.Removable,
123-
_ => Data.Items.DriveType.Unknown,
118+
SystemIO.DriveType.CDRom => DriveType.CDRom,
119+
SystemIO.DriveType.Fixed => DriveType.Fixed,
120+
SystemIO.DriveType.Network => DriveType.Network,
121+
SystemIO.DriveType.NoRootDirectory => DriveType.NoRootDirectory,
122+
SystemIO.DriveType.Ram => DriveType.Ram,
123+
SystemIO.DriveType.Removable => DriveType.Removable,
124+
_ => DriveType.Unknown,
124125
};
125126
}
126127

127-
public static string GetExtendedDriveLabel(DriveInfo drive)
128+
public static unsafe string GetExtendedDriveLabel(SystemIO.DriveInfo drive)
128129
{
129130
return SafetyExtensions.IgnoreExceptions(() =>
130131
{
131-
if (drive.DriveType is not System.IO.DriveType.CDRom || drive.DriveFormat is not "UDF")
132+
if (drive.DriveType is not SystemIO.DriveType.CDRom || drive.DriveFormat is not "UDF")
132133
return drive.VolumeLabel;
134+
133135
return SafetyExtensions.IgnoreExceptions(() =>
134136
{
135-
var dosDevicePath = Vanara.PInvoke.Kernel32.QueryDosDevice(drive.Name).FirstOrDefault();
137+
string dosDevicePath = "";
138+
139+
fixed (char* cDeviceName = drive.Name)
140+
{
141+
var cch = PInvoke.QueryDosDevice(cDeviceName, null, 0u);
142+
143+
fixed (char* cTargetPath = new char[cch])
144+
{
145+
PWSTR pszTargetPath = new(cTargetPath);
146+
PInvoke.QueryDosDevice(cDeviceName, pszTargetPath, 0u);
147+
dosDevicePath = pszTargetPath.ToString();
148+
}
149+
}
150+
136151
if (string.IsNullOrEmpty(dosDevicePath))
137152
return drive.VolumeLabel;
138-
using var driveStream = new FileStream(dosDevicePath.Replace(@"\Device\", @"\\.\"), FileMode.Open, FileAccess.Read);
153+
154+
using var driveStream = new SystemIO.FileStream(
155+
dosDevicePath.Replace(@"\Device\", @"\\.\"),
156+
SystemIO.FileMode.Open,
157+
SystemIO.FileAccess.Read);
158+
139159
using var udf = new UdfReader(driveStream);
160+
140161
return udf.VolumeLabel;
141162
}) ?? drive.VolumeLabel;
142163
}) ?? "";

0 commit comments

Comments
 (0)