Skip to content

Commit d2e8980

Browse files
authored
Fix: Fixed issue where it didn't work to pick files when creating new shortcuts (#14422)
1 parent 95b1076 commit d2e8980

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

src/Files.App/Helpers/Interop/InteropHelpers.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.UI.Xaml;
66
using System.Reflection;
77
using System.Runtime.InteropServices;
8+
using System.Text;
89
using Vanara.PInvoke;
910
using static Vanara.PInvoke.User32;
1011

@@ -73,6 +74,25 @@ public static IntPtr SetWindowLong(HWND hWnd, WindowLongFlags nIndex, IntPtr dwN
7374

7475
[DllImport("User32.dll")]
7576
public extern static short GetKeyState(int n);
77+
78+
[DllImport("shell32.dll")]
79+
public static extern IntPtr SHBrowseForFolder(ref BROWSEINFO lpbi);
80+
81+
[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
82+
public static extern bool SHGetPathFromIDList(IntPtr pidl, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszPath);
83+
84+
[StructLayout(LayoutKind.Sequential)]
85+
public struct BROWSEINFO
86+
{
87+
public IntPtr hwndOwner;
88+
public IntPtr pidlRoot;
89+
public string pszDisplayName;
90+
public string lpszTitle;
91+
public uint ulFlags;
92+
public IntPtr lpfn;
93+
public int lParam;
94+
public IntPtr iImage;
95+
}
7696
}
7797

7898
[ComImport]

src/Files.App/ViewModels/Dialogs/CreateShortcutDialogViewModel.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// Copyright (c) 2023 Files Community
22
// Licensed under the MIT License. See the LICENSE.
33

4-
using Files.Core.Extensions;
54
using System.IO;
5+
using System.Runtime.InteropServices;
6+
using System.Text;
67
using System.Windows.Input;
78
using Windows.Storage.Pickers;
89

@@ -84,18 +85,27 @@ public CreateShortcutDialogViewModel(string workingDirectory)
8485
WorkingDirectory = workingDirectory;
8586
_destinationItemPath = string.Empty;
8687

87-
SelectDestinationCommand = new AsyncRelayCommand(SelectDestinationAsync);
88+
SelectDestinationCommand = new AsyncRelayCommand(SelectDestination);
8889
PrimaryButtonCommand = new AsyncRelayCommand(CreateShortcutAsync);
8990
}
9091

91-
private async Task SelectDestinationAsync()
92+
private Task SelectDestination()
9293
{
93-
var folderPicker = InitializeWithWindow(new FolderPicker());
94-
folderPicker.FileTypeFilter.Add("*");
94+
InteropHelpers.BROWSEINFO bi = new InteropHelpers.BROWSEINFO();
95+
bi.ulFlags = 0x00004000;
96+
bi.lpszTitle = "Select a folder";
97+
nint pidl = InteropHelpers.SHBrowseForFolder(ref bi);
98+
if (pidl != nint.Zero)
99+
{
100+
StringBuilder path = new StringBuilder(260);
101+
if (InteropHelpers.SHGetPathFromIDList(pidl, path))
102+
{
103+
DestinationItemPath = path.ToString();
104+
}
105+
Marshal.FreeCoTaskMem(pidl);
106+
}
95107

96-
var selectedFolder = await folderPicker.PickSingleFolderAsync();
97-
if (selectedFolder is not null)
98-
DestinationItemPath = selectedFolder.Path;
108+
return Task.CompletedTask;
99109
}
100110

101111
private FolderPicker InitializeWithWindow(FolderPicker obj)

0 commit comments

Comments
 (0)