Skip to content

Commit 8ed068e

Browse files
authored
Merge pull request #34 from walterlv/master
Enhance file and folder selection
2 parents effabb0 + f668395 commit 8ed068e

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

src/SourceGit/Native/Windows.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ internal struct MARGINS
4444
[DllImport("dwmapi.dll")]
4545
private static extern int DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS margins);
4646

47+
[DllImport("shell32.dll", CharSet = CharSet.Unicode, SetLastError = false)]
48+
private static extern IntPtr ILCreateFromPathW(string pszPath);
49+
50+
[DllImport("shell32.dll", SetLastError = false)]
51+
private static extern void ILFree(IntPtr pidl);
52+
53+
[DllImport("shell32.dll", CharSet = CharSet.Unicode, SetLastError = false)]
54+
private static extern int SHOpenFolderAndSelectItems(IntPtr pidlFolder, int cild, IntPtr apidl, int dwFlags);
55+
4756
public void SetupApp(AppBuilder builder)
4857
{
4958
builder.With(new FontManagerOptions()
@@ -163,6 +172,10 @@ public void OpenInFileManager(string path, bool select)
163172
if (File.Exists(path))
164173
{
165174
fullpath = new FileInfo(path).FullName;
175+
176+
// For security reason, we never execute a file.
177+
// Instead, we open the folder and select it.
178+
select = true;
166179
}
167180
else
168181
{
@@ -171,11 +184,31 @@ public void OpenInFileManager(string path, bool select)
171184

172185
if (select)
173186
{
174-
Process.Start("explorer", $"/select,\"{fullpath}\"");
187+
// The fullpath here may be a file or a folder.
188+
OpenFolderAndSelectFile(fullpath);
175189
}
176190
else
177191
{
178-
Process.Start("explorer", fullpath);
192+
// The fullpath here is always a folder.
193+
Process.Start(new ProcessStartInfo(fullpath)
194+
{
195+
UseShellExecute = true,
196+
CreateNoWindow = true,
197+
});
198+
}
199+
}
200+
201+
private static void OpenFolderAndSelectFile(string folderPath)
202+
{
203+
var pidl = ILCreateFromPathW(folderPath);
204+
205+
try
206+
{
207+
SHOpenFolderAndSelectItems(pidl, 0, 0, 0);
208+
}
209+
finally
210+
{
211+
ILFree(pidl);
179212
}
180213
}
181214

0 commit comments

Comments
 (0)