1+ using System ;
2+ using System . Diagnostics . CodeAnalysis ;
3+ using System . Runtime . InteropServices ;
4+
5+ namespace StlVault . ViewModels
6+ {
7+ [ SuppressMessage ( "ReSharper" , "FieldCanBeMadeReadOnly.Local" ) ]
8+ [ SuppressMessage ( "ReSharper" , "BuiltInTypeReferenceStyle" ) ]
9+ [ SuppressMessage ( "ReSharper" , "MemberCanBePrivate.Local" ) ]
10+ [ SuppressMessage ( "ReSharper" , "InconsistentNaming" ) ]
11+ [ SuppressMessage ( "ReSharper" , "IdentifierTypo" ) ]
12+ internal static class NativeMethods
13+ {
14+ public static void BrowseTo ( string filePath )
15+ {
16+ const uint normalPriorityClass = 0x0020 ;
17+
18+ var application = Environment . GetEnvironmentVariable ( "windir" ) + @"\explorer.exe" ;
19+ var commandLine = $ "/e, /select, \" { filePath } \" ";
20+ var sInfo = new STARTUPINFO ( ) ;
21+ var pSec = new SECURITY_ATTRIBUTES ( ) ;
22+ var tSec = new SECURITY_ATTRIBUTES ( ) ;
23+ pSec . nLength = Marshal . SizeOf ( pSec ) ;
24+ tSec . nLength = Marshal . SizeOf ( tSec ) ;
25+
26+ //Open Explorer at location
27+ CreateProcess ( application , commandLine ,
28+ ref pSec , ref tSec , false , normalPriorityClass ,
29+ IntPtr . Zero , null , ref sInfo , out _ ) ;
30+ }
31+
32+ [ DllImport ( "kernel32.dll" , SetLastError = true , CharSet = CharSet . Auto ) ]
33+ static extern bool CreateProcess (
34+ string lpApplicationName ,
35+ string lpCommandLine ,
36+ ref SECURITY_ATTRIBUTES lpProcessAttributes ,
37+ ref SECURITY_ATTRIBUTES lpThreadAttributes ,
38+ bool bInheritHandles ,
39+ uint dwCreationFlags ,
40+ IntPtr lpEnvironment ,
41+ string lpCurrentDirectory ,
42+ [ In ] ref STARTUPINFO lpStartupInfo ,
43+ out PROCESS_INFORMATION lpProcessInformation ) ;
44+
45+ // This also works with CharSet.Ansi as long as the calling function uses the same character set.
46+ [ StructLayout ( LayoutKind . Sequential , CharSet = CharSet . Unicode ) ]
47+ private struct STARTUPINFO
48+ {
49+ public Int32 cb ;
50+ public string lpReserved ;
51+ public string lpDesktop ;
52+ public string lpTitle ;
53+ public Int32 dwX ;
54+ public Int32 dwY ;
55+ public Int32 dwXSize ;
56+ public Int32 dwYSize ;
57+ public Int32 dwXCountChars ;
58+ public Int32 dwYCountChars ;
59+ public Int32 dwFillAttribute ;
60+ public Int32 dwFlags ;
61+ public Int16 wShowWindow ;
62+ public Int16 cbReserved2 ;
63+ public IntPtr lpReserved2 ;
64+ public IntPtr hStdInput ;
65+ public IntPtr hStdOutput ;
66+ public IntPtr hStdError ;
67+ }
68+
69+ [ StructLayout ( LayoutKind . Sequential ) ]
70+ private struct PROCESS_INFORMATION
71+ {
72+ public IntPtr hProcess ;
73+ public IntPtr hThread ;
74+ public int dwProcessId ;
75+ public int dwThreadId ;
76+ }
77+
78+ [ StructLayout ( LayoutKind . Sequential ) ]
79+ private struct SECURITY_ATTRIBUTES
80+ {
81+ public int nLength ;
82+ public unsafe byte * lpSecurityDescriptor ;
83+ public int bInheritHandle ;
84+ }
85+ }
86+ }
0 commit comments