Skip to content

Commit 3c7c941

Browse files
authored
Update StorageNetworkService.cs
1 parent c7ac2d3 commit 3c7c941

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

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

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,27 +88,21 @@ unsafe IEnumerable<ILocatableFolder> GetComputers()
8888
(attribute & SFGAO_FLAGS.SFGAO_FOLDER) is not SFGAO_FLAGS.SFGAO_FOLDER)
8989
continue;
9090

91-
// Get the target path
92-
using ComPtr<IShellLinkW> pShellLink = default;
93-
var shellLinkIid = typeof(IShellLinkW).GUID;
94-
pShellItem.Get()->QueryInterface(&shellLinkIid, (void**)pShellLink.GetAddressOf());
95-
string targetPath = string.Empty;
96-
fixed (char* pszTargetPath = new char[1024])
97-
{
98-
hr = pShellLink.Get()->GetPath(pszTargetPath, 1024, null, (uint)SLGP_FLAGS.SLGP_RAWPATH);
99-
targetPath = Environment.ExpandEnvironmentVariables(new PWSTR(pszTargetPath).ToString());
100-
}
101-
10291
// Get the display name
10392
pShellItem.Get()->GetDisplayName(SIGDN.SIGDN_NORMALDISPLAY, out var szDisplayName);
10493
var fileName = szDisplayName.ToString();
10594
PInvoke.CoTaskMemFree(szDisplayName.Value);
10695

96+
// Get the file system path on disk
97+
pShellItem.Get()->GetDisplayName(SIGDN.SIGDN_FILESYSPATH, out szDisplayName);
98+
var filePath = szDisplayName.ToString();
99+
PInvoke.CoTaskMemFree(szDisplayName.Value);
100+
107101
var item = new DriveItem()
108102
{
109103
Text = fileName,
110-
Path = targetPath,
111-
DeviceID = targetPath,
104+
Path = filePath,
105+
DeviceID = filePath,
112106
Type = DriveType.Network,
113107
ItemType = NavigationControlItemType.Drive,
114108
MenuOptions = new()
@@ -156,10 +150,32 @@ unsafe IEnumerable<ILocatableFolder> GetShortcuts()
156150
var shellLinkIid = typeof(IShellLinkW).GUID;
157151
pShellItem.Get()->QueryInterface(&shellLinkIid, (void**)pShellLink.GetAddressOf());
158152
string targetPath = string.Empty;
159-
fixed (char* pszTargetPath = new char[1024])
153+
if (pShellLink.IsNull)
160154
{
161-
hr = pShellLink.Get()->GetPath(pszTargetPath, 1024, null, (uint)SLGP_FLAGS.SLGP_RAWPATH);
162-
targetPath = Environment.ExpandEnvironmentVariables(new PWSTR(pszTargetPath).ToString());
155+
using ComPtr<IShellItem2> pShellItem2 = default;
156+
var shellItem2Iid = typeof(IShellItem2).GUID;
157+
pShellItem.Get()->QueryInterface(&shellItem2Iid, (void**)pShellItem2.GetAddressOf());
158+
PInvoke.PSGetPropertyKeyFromName("System.Link.TargetParsingPath", out var propertyKey);
159+
pShellItem2.Get()->GetString(propertyKey, out var pszTargetPath);
160+
targetPath = Environment.ExpandEnvironmentVariables(pszTargetPath.ToString());
161+
162+
// Test 1
163+
pShellItem.Get()->GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING, out var szDisplayNameTest);
164+
var filePathTest = szDisplayNameTest.ToString();
165+
PInvoke.CoTaskMemFree(szDisplayNameTest.Value); // break here
166+
167+
// Test 2
168+
pShellItem.Get()->GetDisplayName(SIGDN_PARENTRELATIVEPARSING, out szDisplayNameTest);
169+
filePathTest = szDisplayNameTest.ToString();
170+
PInvoke.CoTaskMemFree(szDisplayNameTest.Value); // break here
171+
}
172+
else
173+
{
174+
fixed (char* pszTargetPath = new char[1024])
175+
{
176+
hr = pShellLink.Get()->GetPath(pszTargetPath, 1024, null, (uint)SLGP_FLAGS.SLGP_RAWPATH);
177+
targetPath = Environment.ExpandEnvironmentVariables(new PWSTR(pszTargetPath).ToString());
178+
}
163179
}
164180

165181
// Get the display name

0 commit comments

Comments
 (0)