|
| 1 | +#if WINDOWS |
| 2 | +using System; |
| 3 | +using System.Runtime.InteropServices; |
| 4 | +using Microsoft.Win32; |
| 5 | +using Microsoft.Win32.SafeHandles; |
| 6 | + |
| 7 | +namespace PSTree.Native; |
| 8 | + |
| 9 | +internal static partial class WinAPI |
| 10 | +{ |
| 11 | +#if NET8_0_OR_GREATER |
| 12 | + [LibraryImport("advapi32.dll", EntryPoint = "RegQueryInfoKeyW")] |
| 13 | + private static partial int RegQueryInfoKey( |
| 14 | +#else |
| 15 | + [DllImport("advapi32.dll")] |
| 16 | + private static extern int RegQueryInfoKey( |
| 17 | +#endif |
| 18 | + SafeRegistryHandle hkey, |
| 19 | + IntPtr lpClass, |
| 20 | + ref uint lpcbClass, |
| 21 | + IntPtr lpReserved, |
| 22 | + IntPtr lpcSubKeys, |
| 23 | + IntPtr lpcbMaxSubKeyLen, |
| 24 | + IntPtr lpcbMaxClassLen, |
| 25 | + IntPtr lpcValues, |
| 26 | + IntPtr lpcbMaxValueNameLen, |
| 27 | + IntPtr lpcbMaxValueLen, |
| 28 | + IntPtr lpcbSecurityDescriptor, |
| 29 | + out long lpftLastWriteTime); |
| 30 | + |
| 31 | + internal static DateTime? GetLastWriteTime(this RegistryKey key) |
| 32 | + { |
| 33 | + uint lpcbClass = 0; |
| 34 | + |
| 35 | + int result = RegQueryInfoKey( |
| 36 | + hkey: key.Handle, |
| 37 | + lpClass: IntPtr.Zero, |
| 38 | + lpcbClass: ref lpcbClass, |
| 39 | + lpReserved: IntPtr.Zero, |
| 40 | + lpcSubKeys: IntPtr.Zero, |
| 41 | + lpcbMaxSubKeyLen: IntPtr.Zero, |
| 42 | + lpcbMaxClassLen: IntPtr.Zero, |
| 43 | + lpcValues: IntPtr.Zero, |
| 44 | + lpcbMaxValueNameLen: IntPtr.Zero, |
| 45 | + lpcbMaxValueLen: IntPtr.Zero, |
| 46 | + lpcbSecurityDescriptor: IntPtr.Zero, |
| 47 | + lpftLastWriteTime: out long lpftLastWriteTime); |
| 48 | + |
| 49 | + return result == 0 |
| 50 | + ? DateTime.FromFileTime(lpftLastWriteTime) : null; |
| 51 | + } |
| 52 | +} |
| 53 | +#endif |
0 commit comments