mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 18:57:19 +02:00
* Only open FilStream in read-only mode; Release propertyStore handle after getting the file properties
(cherry picked from commit 3b1481da2c)
* Update calls to PropertyStoreHelper
* Add disposable property store
* Make GetPropertyStoreFromPath return Disposable property store
* correct typo
* correct typo
* Remove nullable in DisposablePropertyStore
* Add property getters
* Remove usued method
* Correct typo
* Correct typo again...
* Update description
121 lines
3.7 KiB
C#
121 lines
3.7 KiB
C#
// Copyright (c) Microsoft Corporation
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Runtime.InteropServices;
|
|
using Peek.Common.Models;
|
|
|
|
namespace Peek.Common.Extensions
|
|
{
|
|
public static class IPropertyStoreExtensions
|
|
{
|
|
/// <summary>
|
|
/// Helper method that retrieves a uint value from the given property store.
|
|
/// Returns 0 if the value is not a VT_UI4 (4-byte unsigned integer in little-endian order).
|
|
/// </summary>
|
|
/// <param name="propertyStore">The property store</param>
|
|
/// <param name="key">The pkey</param>
|
|
/// <returns>The uint value</returns>
|
|
public static uint? TryGetUInt(this DisposablePropertyStore propertyStore, PropertyKey key)
|
|
{
|
|
if (propertyStore == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
try
|
|
{
|
|
PropVariant propVar;
|
|
|
|
propertyStore.GetValue(ref key, out propVar);
|
|
|
|
// VT_UI4 Indicates a 4-byte unsigned integer formatted in little-endian byte order.
|
|
if ((VarEnum)propVar.Vt == VarEnum.VT_UI4)
|
|
{
|
|
return propVar.UlVal;
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Helper method that retrieves a ulong value from the given property store.
|
|
/// Returns 0 if the value is not a VT_UI8 (8-byte unsigned integer in little-endian order).
|
|
/// </summary>
|
|
/// <param name="propertyStore">The property store</param>
|
|
/// <param name="key">the pkey</param>
|
|
/// <returns>the ulong value</returns>
|
|
public static ulong? TryGetULong(this DisposablePropertyStore propertyStore, PropertyKey key)
|
|
{
|
|
if (propertyStore == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
try
|
|
{
|
|
PropVariant propVar;
|
|
|
|
propertyStore.GetValue(ref key, out propVar);
|
|
|
|
// VT_UI8 Indicates an 8-byte unsigned integer formatted in little-endian byte order.
|
|
if ((VarEnum)propVar.Vt == VarEnum.VT_UI8)
|
|
{
|
|
return propVar.UhVal;
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Helper method that retrieves a string value from the given property store.
|
|
/// </summary>
|
|
/// <param name="propertyStore">The property store</param>
|
|
/// <param name="key">The pkey</param>
|
|
/// <returns>The string value</returns>
|
|
public static string? TryGetString(this DisposablePropertyStore propertyStore, PropertyKey key)
|
|
{
|
|
if (propertyStore == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
try
|
|
{
|
|
PropVariant propVar;
|
|
|
|
propertyStore.GetValue(ref key, out propVar);
|
|
|
|
if ((VarEnum)propVar.Vt == VarEnum.VT_LPWSTR)
|
|
{
|
|
return Marshal.PtrToStringUni(propVar.P) ?? string.Empty;
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|