Skip to content

Commit 0c28d27

Browse files
authored
Fix: Fixed performance issue with SMB shares (#16812)
Signed-off-by: KFlab <[email protected]>
1 parent ee6674e commit 0c28d27

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

src/Files.App/Services/Storage/StorageNetworkService.cs

+19
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,25 @@ public async Task<bool> AuthenticateNetworkShare(string path)
208208

209209
unsafe
210210
{
211+
212+
if (!path.StartsWith(@"\\", StringComparison.Ordinal))
213+
{
214+
// Special handling for network drives
215+
// This part will change path from "y:\Download" to "\\192.168.0.1\nfs\Download"
216+
[DllImport("mpr.dll", CharSet = CharSet.Auto)]
217+
static extern int WNetGetConnection(string lpLocalName, StringBuilder lpRemoteName, ref int lpnLength);
218+
219+
StringBuilder remoteName = new StringBuilder(300);
220+
int length = remoteName.Capacity;
221+
string lpLocalName = path.Substring(0, 2);
222+
223+
int ret = WNetGetConnection(lpLocalName, remoteName, ref length);
224+
225+
if ( ret == 0 )
226+
path = path.Replace(lpLocalName, remoteName.ToString());
227+
228+
}
229+
211230
fixed (char* lpcPath = path)
212231
netRes.lpRemoteName = new PWSTR(lpcPath);
213232
}

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,11 @@ public static async Task<StorageFolderWithPath> GetRootFromPathAsync(string devi
9292
}
9393
}
9494
// Network share
95-
else if (devicePath.StartsWith(@"\\", StringComparison.Ordinal) &&
96-
!devicePath.StartsWith(@"\\SHELL\", StringComparison.Ordinal))
95+
else if (
96+
( devicePath.StartsWith(@"\\", StringComparison.Ordinal) ||
97+
GetDriveType(new SystemIO.DriveInfo(devicePath)) is DriveType.Network ) &&
98+
!devicePath.StartsWith(@"\\SHELL\", StringComparison.Ordinal)
99+
)
97100
{
98101
int lastSepIndex = rootPath.LastIndexOf(@"\", StringComparison.Ordinal);
99102
rootPath = lastSepIndex > 1 ? rootPath.Substring(0, lastSepIndex) : rootPath; // Remove share name
@@ -169,4 +172,4 @@ public static async Task<StorageItemThumbnail> GetThumbnailAsync(StorageFolder f
169172
=> folder.GetThumbnailAsync(ThumbnailMode.SingleItem, 40, ThumbnailOptions.UseCurrentScale).AsTask()
170173
);
171174
}
172-
}
175+
}

src/Files.App/ViewModels/ShellViewModel.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1634,12 +1634,18 @@ private async Task<int> EnumerateItemsFromStandardFolderAsync(string path, Cance
16341634
!isMtp &&
16351635
!isShellFolder &&
16361636
!isWslDistro;
1637+
1638+
// Special handling for network drives
1639+
bool isNetdisk = false;
1640+
if (!isNetwork)
1641+
isNetdisk = (new DriveInfo(path).DriveType == System.IO.DriveType.Network);
1642+
16371643
bool isFtp = FtpHelpers.IsFtpPath(path);
16381644
bool enumFromStorageFolder = isBoxFolder || isFtp;
16391645

16401646
BaseStorageFolder? rootFolder = null;
16411647

1642-
if (isNetwork)
1648+
if (isNetwork || isNetdisk)
16431649
{
16441650
var auth = await NetworkService.AuthenticateNetworkShare(path);
16451651
if (!auth)

src/Files.Shared/Helpers/PathHelpers.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ public static string FormatName(string path)
2222
string fileName;
2323
string rootPath = Path.GetPathRoot(path) ?? string.Empty;
2424

25-
if (rootPath == path && path.StartsWith(@"\\"))
25+
if (rootPath == path &&
26+
( path.StartsWith(@"\\") ||
27+
(new DriveInfo(path).DriveType == System.IO.DriveType.Network)
28+
)
29+
)
2630
{
2731
// Network Share path
2832
fileName = path.Substring(path.LastIndexOf(@"\", StringComparison.Ordinal) + 1);

0 commit comments

Comments
 (0)