Initial import

This commit is contained in:
2022-09-16 12:32:25 +02:00
commit f10a1b7663
16 changed files with 1323 additions and 0 deletions

18
PdbFind.Gui/App.config Normal file
View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="PdbFind.Gui.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<PdbFind.Gui.Properties.Settings>
<setting name="UpgradeNeeded" serializeAs="String">
<value>True</value>
</setting>
<setting name="SymStore" serializeAs="String">
<value />
</setting>
</PdbFind.Gui.Properties.Settings>
</userSettings>
</configuration>

189
PdbFind.Gui/FormPdbFind.Designer.cs generated Normal file
View File

@@ -0,0 +1,189 @@
namespace PdbFind.Gui
{
partial class FormPdbFind
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.label1 = new System.Windows.Forms.Label();
this.textBoxSysStore = new System.Windows.Forms.TextBox();
this.buttonSymStoreBrowse = new System.Windows.Forms.Button();
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
this.textBoxPeFile = new System.Windows.Forms.TextBox();
this.buttonPeFileBrowse = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.textBoxPdbFile = new System.Windows.Forms.TextBox();
this.buttonPdbExplore = new System.Windows.Forms.Button();
this.label3 = new System.Windows.Forms.Label();
this.buttonLocatePdb = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 15);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(77, 15);
this.label1.TabIndex = 0;
this.label1.Text = "Symbol Store";
//
// textBoxSysStore
//
this.textBoxSysStore.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBoxSysStore.Location = new System.Drawing.Point(157, 12);
this.textBoxSysStore.Name = "textBoxSysStore";
this.textBoxSysStore.Size = new System.Drawing.Size(512, 23);
this.textBoxSysStore.TabIndex = 1;
//
// buttonSymStoreBrowse
//
this.buttonSymStoreBrowse.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonSymStoreBrowse.Location = new System.Drawing.Point(675, 12);
this.buttonSymStoreBrowse.Name = "buttonSymStoreBrowse";
this.buttonSymStoreBrowse.Size = new System.Drawing.Size(113, 23);
this.buttonSymStoreBrowse.TabIndex = 2;
this.buttonSymStoreBrowse.Text = "Browse";
this.buttonSymStoreBrowse.UseVisualStyleBackColor = true;
this.buttonSymStoreBrowse.Click += new System.EventHandler(this.buttonSymStoreBrowse_Click);
//
// notifyIcon1
//
this.notifyIcon1.Text = "notifyIcon1";
this.notifyIcon1.Visible = true;
//
// textBoxPeFile
//
this.textBoxPeFile.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBoxPeFile.Location = new System.Drawing.Point(157, 41);
this.textBoxPeFile.Name = "textBoxPeFile";
this.textBoxPeFile.Size = new System.Drawing.Size(512, 23);
this.textBoxPeFile.TabIndex = 3;
this.textBoxPeFile.TextChanged += new System.EventHandler(this.textBoxPeFile_TextChanged);
//
// buttonPeFileBrowse
//
this.buttonPeFileBrowse.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonPeFileBrowse.Location = new System.Drawing.Point(675, 41);
this.buttonPeFileBrowse.Name = "buttonPeFileBrowse";
this.buttonPeFileBrowse.Size = new System.Drawing.Size(113, 23);
this.buttonPeFileBrowse.TabIndex = 4;
this.buttonPeFileBrowse.Text = "Browse";
this.buttonPeFileBrowse.UseVisualStyleBackColor = true;
this.buttonPeFileBrowse.Click += new System.EventHandler(this.buttonPeFileBrowse_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 45);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(41, 15);
this.label2.TabIndex = 5;
this.label2.Text = "PE File";
//
// textBoxPdbFile
//
this.textBoxPdbFile.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBoxPdbFile.Location = new System.Drawing.Point(157, 99);
this.textBoxPdbFile.Name = "textBoxPdbFile";
this.textBoxPdbFile.ReadOnly = true;
this.textBoxPdbFile.Size = new System.Drawing.Size(512, 23);
this.textBoxPdbFile.TabIndex = 6;
//
// buttonPdbExplore
//
this.buttonPdbExplore.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonPdbExplore.Location = new System.Drawing.Point(675, 99);
this.buttonPdbExplore.Name = "buttonPdbExplore";
this.buttonPdbExplore.Size = new System.Drawing.Size(113, 23);
this.buttonPdbExplore.TabIndex = 7;
this.buttonPdbExplore.Text = "Show in Explorer";
this.buttonPdbExplore.UseVisualStyleBackColor = true;
this.buttonPdbExplore.Click += new System.EventHandler(this.buttonPdbExplore_Click);
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(12, 102);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(50, 15);
this.label3.TabIndex = 8;
this.label3.Text = "PDB File";
//
// buttonLocatePdb
//
this.buttonLocatePdb.Location = new System.Drawing.Point(157, 70);
this.buttonLocatePdb.Name = "buttonLocatePdb";
this.buttonLocatePdb.Size = new System.Drawing.Size(113, 23);
this.buttonLocatePdb.TabIndex = 9;
this.buttonLocatePdb.Text = "Locate PDB";
this.buttonLocatePdb.UseVisualStyleBackColor = true;
this.buttonLocatePdb.Click += new System.EventHandler(this.buttonLocatePdb_Click);
//
// FormPdbFind
//
this.AllowDrop = true;
this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
this.ClientSize = new System.Drawing.Size(800, 145);
this.Controls.Add(this.buttonLocatePdb);
this.Controls.Add(this.label3);
this.Controls.Add(this.buttonPdbExplore);
this.Controls.Add(this.textBoxPdbFile);
this.Controls.Add(this.label2);
this.Controls.Add(this.buttonPeFileBrowse);
this.Controls.Add(this.textBoxPeFile);
this.Controls.Add(this.buttonSymStoreBrowse);
this.Controls.Add(this.textBoxSysStore);
this.Controls.Add(this.label1);
this.Name = "FormPdbFind";
this.Text = "Pdb Find";
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.FormPdbFind_FormClosed);
this.DragDrop += new System.Windows.Forms.DragEventHandler(this.FormPdbFind_DragDrop);
this.DragEnter += new System.Windows.Forms.DragEventHandler(this.FormPdbFind_DragEnter);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private Label label1;
private TextBox textBoxSysStore;
private Button buttonSymStoreBrowse;
private NotifyIcon notifyIcon1;
private TextBox textBoxPeFile;
private Button buttonPeFileBrowse;
private Label label2;
private TextBox textBoxPdbFile;
private Button buttonPdbExplore;
private Label label3;
private Button buttonLocatePdb;
}
}

119
PdbFind.Gui/FormPdbFind.cs Normal file
View File

@@ -0,0 +1,119 @@
using System.Diagnostics;
using PdbFind.Gui.Properties;
using PdbFind.Lib;
namespace PdbFind.Gui
{
public partial class FormPdbFind : Form
{
public FormPdbFind()
{
InitializeComponent();
textBoxSysStore.Text = Settings.Default.SymStore;
}
private void buttonSymStoreBrowse_Click(object sender, EventArgs e)
{
using var fbd = new FolderBrowserDialog();
fbd.InitialDirectory = textBoxSysStore.Text;
if (fbd.ShowDialog() == DialogResult.OK)
{
if (!string.IsNullOrWhiteSpace(fbd.SelectedPath) && Directory.Exists(fbd.SelectedPath))
{
textBoxSysStore.Text = fbd.SelectedPath;
}
}
}
private void FormPdbFind_FormClosed(object sender, FormClosedEventArgs e)
{
Settings.Default.SymStore = textBoxSysStore.Text;
Settings.Default.Save();
}
private void buttonPdbExplore_Click(object sender, EventArgs e)
{
var pdb = textBoxPdbFile.Text;
if (!string.IsNullOrWhiteSpace(pdb) && File.Exists(pdb))
{
ShowSelectedInExplorer.FileOrFolder(pdb);
}
}
private void buttonPeFileBrowse_Click(object sender, EventArgs e)
{
using var ofd = new OpenFileDialog();
ofd.CheckFileExists = true;
ofd.Multiselect = false;
var pe = textBoxPeFile.Text;
if (!string.IsNullOrWhiteSpace(pe) && File.Exists(pe))
{
ofd.InitialDirectory = Path.GetDirectoryName(pe);
ofd.FileName = Path.GetFileName(pe);
}
if (ofd.ShowDialog() == DialogResult.OK)
{
if (File.Exists(ofd.FileName))
{
textBoxPeFile.Text = ofd.FileName;
LocatePdb();
}
}
}
private void LocatePdb()
{
var store = textBoxSysStore.Text;
var pe = textBoxPeFile.Text;
if (string.IsNullOrWhiteSpace(store) || !Directory.Exists(store))
{
return;
}
if (string.IsNullOrWhiteSpace(pe) || !File.Exists(pe))
{
return;
}
try
{
textBoxPdbFile.Text = Locator.LocatePdbInStore(pe, store);
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void textBoxPeFile_TextChanged(object sender, EventArgs e)
{
textBoxPdbFile.Text = "";
}
private void buttonLocatePdb_Click(object sender, EventArgs e)
{
LocatePdb();
}
private void FormPdbFind_DragEnter(object sender, DragEventArgs e)
{
if (e.Data != null && e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
}
private void FormPdbFind_DragDrop(object sender, DragEventArgs e)
{
var files = (string[]?)e.Data?.GetData(DataFormats.FileDrop);
if (files is { Length: > 0 })
{
textBoxPeFile.Text = files.First();
LocatePdb();
}
}
}
}

View File

@@ -0,0 +1,63 @@
<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="notifyIcon1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root>

View File

@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
<Title>PdbFind.Gui</Title>
<AssemblyVersion></AssemblyVersion>
<FileVersion></FileVersion>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\PdbFind.Lib\PdbFind.Lib.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Update="Properties\Settings.Designer.cs">
<DesignTimeSharedInput>True</DesignTimeSharedInput>
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<None Update="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
</ItemGroup>
</Project>

28
PdbFind.Gui/Program.cs Normal file
View File

@@ -0,0 +1,28 @@
using PdbFind.Gui.Properties;
namespace PdbFind.Gui
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
if (Settings.Default.UpgradeNeeded)
{
Settings.Default.Upgrade();
Settings.Default.UpgradeNeeded = false;
Settings.Default.Save();
Settings.Default.Reload();
}
Application.Run(new FormPdbFind());
}
}
}

View File

@@ -0,0 +1,50 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace PdbFind.Gui.Properties {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.4.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
public static Settings Default {
get {
return defaultInstance;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("True")]
public bool UpgradeNeeded {
get {
return ((bool)(this["UpgradeNeeded"]));
}
set {
this["UpgradeNeeded"] = value;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("")]
public string SymStore {
get {
return ((string)(this["SymStore"]));
}
set {
this["SymStore"] = value;
}
}
}
}

View File

@@ -0,0 +1,12 @@
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="PdbFind.Gui.Properties" GeneratedClassName="Settings">
<Profiles />
<Settings>
<Setting Name="UpgradeNeeded" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="SymStore" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
</Settings>
</SettingsFile>

View File

@@ -0,0 +1,260 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
namespace PdbFind.Gui;
static class ShowSelectedInExplorer
{
[Flags]
enum SHCONT : ushort
{
SHCONTF_CHECKING_FOR_CHILDREN = 0x0010,
SHCONTF_FOLDERS = 0x0020,
SHCONTF_NONFOLDERS = 0x0040,
SHCONTF_INCLUDEHIDDEN = 0x0080,
SHCONTF_INIT_ON_FIRST_NEXT = 0x0100,
SHCONTF_NETPRINTERSRCH = 0x0200,
SHCONTF_SHAREABLE = 0x0400,
SHCONTF_STORAGE = 0x0800,
SHCONTF_NAVIGATION_ENUM = 0x1000,
SHCONTF_FASTITEMS = 0x2000,
SHCONTF_FLATLIST = 0x4000,
SHCONTF_ENABLE_ASYNC = 0x8000
}
[ComImport,
Guid("000214E6-0000-0000-C000-000000000046"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
ComConversionLoss]
interface IShellFolder
{
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void ParseDisplayName(IntPtr hwnd, [In, MarshalAs(UnmanagedType.Interface)] IBindCtx pbc,
[In, MarshalAs(UnmanagedType.LPWStr)] string pszDisplayName, [Out] out uint pchEaten,
[Out] out IntPtr ppidl, [In, Out] ref uint pdwAttributes);
[PreserveSig]
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
int EnumObjects([In] IntPtr hwnd, [In] SHCONT grfFlags,
[MarshalAs(UnmanagedType.Interface)] out IEnumIDList ppenumIDList);
[PreserveSig]
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
int BindToObject([In] IntPtr pidl, [In, MarshalAs(UnmanagedType.Interface)] IBindCtx pbc, [In] ref Guid riid,
[Out, MarshalAs(UnmanagedType.Interface)] out IShellFolder ppv);
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void BindToStorage([In] ref IntPtr pidl, [In, MarshalAs(UnmanagedType.Interface)] IBindCtx pbc,
[In] ref Guid riid, out IntPtr ppv);
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void CompareIDs([In] IntPtr lParam, [In] ref IntPtr pidl1, [In] ref IntPtr pidl2);
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void CreateViewObject([In] IntPtr hwndOwner, [In] ref Guid riid, out IntPtr ppv);
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void GetAttributesOf([In] uint cidl, [In] IntPtr apidl, [In, Out] ref uint rgfInOut);
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void GetUIObjectOf([In] IntPtr hwndOwner, [In] uint cidl, [In] IntPtr apidl, [In] ref Guid riid,
[In, Out] ref uint rgfReserved, out IntPtr ppv);
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void GetDisplayNameOf([In] ref IntPtr pidl, [In] uint uFlags, out IntPtr pName);
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void SetNameOf([In] IntPtr hwnd, [In] ref IntPtr pidl, [In, MarshalAs(UnmanagedType.LPWStr)] string pszName,
[In] uint uFlags, [Out] IntPtr ppidlOut);
}
[ComImport,
Guid("000214F2-0000-0000-C000-000000000046"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IEnumIDList
{
[PreserveSig]
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
int Next(uint celt, IntPtr rgelt, out uint pceltFetched);
[PreserveSig]
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
int Skip([In] uint celt);
[PreserveSig]
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
int Reset();
[PreserveSig]
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
int Clone([MarshalAs(UnmanagedType.Interface)] out IEnumIDList ppenum);
}
static class NativeMethods
{
[DllImport("shell32.dll", EntryPoint = "SHGetDesktopFolder", CharSet = CharSet.Unicode,
SetLastError = true)]
static extern int SHGetDesktopFolder_([MarshalAs(UnmanagedType.Interface)] out IShellFolder ppshf);
public static IShellFolder SHGetDesktopFolder()
{
IShellFolder result;
Marshal.ThrowExceptionForHR(SHGetDesktopFolder_(out result));
return result;
}
[DllImport("shell32.dll", EntryPoint = "SHOpenFolderAndSelectItems")]
static extern int SHOpenFolderAndSelectItems_(
[In] IntPtr pidlFolder, uint cidl, [In, Optional, MarshalAs(UnmanagedType.LPArray)] IntPtr[] apidl,
int dwFlags);
public static void SHOpenFolderAndSelectItems(IntPtr pidlFolder, IntPtr[] apidl, int dwFlags)
{
var cidl = (apidl != null) ? (uint)apidl.Length : 0U;
var result = SHOpenFolderAndSelectItems_(pidlFolder, cidl, apidl, dwFlags);
Marshal.ThrowExceptionForHR(result);
}
[DllImport("shell32.dll")]
public static extern void ILFree([In] IntPtr pidl);
}
static IntPtr GetShellFolderChildrenRelativePIDL(IShellFolder parentFolder, string displayName)
{
uint pchEaten;
uint pdwAttributes = 0;
IntPtr ppidl;
parentFolder.ParseDisplayName(IntPtr.Zero, null, displayName, out pchEaten, out ppidl, ref pdwAttributes);
return ppidl;
}
static IntPtr PathToAbsolutePIDL(string path)
{
var desktopFolder = NativeMethods.SHGetDesktopFolder();
return GetShellFolderChildrenRelativePIDL(desktopFolder, path);
}
static Guid IID_IShellFolder = typeof(IShellFolder).GUID;
static IShellFolder PIDLToShellFolder(IShellFolder parent, IntPtr pidl)
{
IShellFolder folder;
var result = parent.BindToObject(pidl, null, ref IID_IShellFolder, out folder);
Marshal.ThrowExceptionForHR((int)result);
return folder;
}
static IShellFolder PIDLToShellFolder(IntPtr pidl)
{
return PIDLToShellFolder(NativeMethods.SHGetDesktopFolder(), pidl);
}
static void SHOpenFolderAndSelectItems(IntPtr pidlFolder, IntPtr[] apidl, bool edit)
{
NativeMethods.SHOpenFolderAndSelectItems(pidlFolder, apidl, edit ? 1 : 0);
}
public static void FileOrFolder(string path, bool edit = false)
{
if (path == null) throw new ArgumentNullException("path");
var pidl = PathToAbsolutePIDL(path);
try
{
SHOpenFolderAndSelectItems(pidl, null, edit);
}
finally
{
NativeMethods.ILFree(pidl);
}
}
static IEnumerable<FileSystemInfo> PathToFileSystemInfo(IEnumerable<string> paths)
{
foreach (var path in paths)
{
var fixedPath = path;
if (fixedPath.EndsWith(Path.DirectorySeparatorChar.ToString())
|| fixedPath.EndsWith(Path.AltDirectorySeparatorChar.ToString()))
{
fixedPath = fixedPath.Remove(fixedPath.Length - 1);
}
if (Directory.Exists(fixedPath))
{
yield return new DirectoryInfo(fixedPath);
}
else if (File.Exists(fixedPath))
{
yield return new FileInfo(fixedPath);
}
else
{
throw new FileNotFoundException
(string.Format("The specified file or folder doesn't exists : {0}", fixedPath),
fixedPath);
}
}
}
public static void FilesOrFolders(string parentDirectory, ICollection<string> filenames)
{
if (filenames == null) throw new ArgumentNullException("filenames");
if (filenames.Count == 0) return;
var parentPidl = PathToAbsolutePIDL(parentDirectory);
try
{
var parent = PIDLToShellFolder(parentPidl);
var filesPidl = filenames
.Select(filename => GetShellFolderChildrenRelativePIDL(parent, filename))
.ToArray();
try
{
SHOpenFolderAndSelectItems(parentPidl, filesPidl, false);
}
finally
{
foreach (var pidl in filesPidl)
{
NativeMethods.ILFree(pidl);
}
}
}
finally
{
NativeMethods.ILFree(parentPidl);
}
}
public static void FilesOrFolders(params string[] paths)
{
FilesOrFolders((IEnumerable<string>)paths);
}
public static void FilesOrFolders(IEnumerable<string> paths)
{
if (paths == null) throw new ArgumentNullException("paths");
FilesOrFolders(PathToFileSystemInfo(paths));
}
public static void FilesOrFolders(IEnumerable<FileSystemInfo> paths)
{
if (paths == null) throw new ArgumentNullException("paths");
var pathsArray = paths.ToArray();
if (pathsArray.Length == 0) return;
var explorerWindows = pathsArray.GroupBy(p => Path.GetDirectoryName(p.FullName));
foreach (var explorerWindowPaths in explorerWindows)
{
var parentDirectory = Path.GetDirectoryName(explorerWindowPaths.First().FullName);
FilesOrFolders(parentDirectory, explorerWindowPaths.Select(fsi => fsi.Name).ToList());
}
}
}