Closed zhuxb711 closed 4 months ago
First of all, you should use using
on the ShellItemPropertyStore
-- it is disposed with ShellItem
. Try the following and let me know if it works (I haven't tried it, but it should work):
// D:\\ is locked by bitlocker
using (ShellItem Item = new ShellItem("D:\\"))
{
ShellItemPropertyStore PropertyStore = Item.Properties;
PropertyStore.ReadOnly = false;
PropertyStore.Temporary = false;
if (PropertyStore["System.Volume.BitLockerProtection"] != 6)
throw new Exception();
PropertyStore["System.Volume.BitLockerProtection"] = 1;
if (PropertyStore["System.Volume.BitLockerProtection"] != 1)
throw new Exception();
PropertyStore["System.Volume.BitLockerProtection"] = 6;
if (PropertyStore["System.Volume.BitLockerProtection"] != 6)
throw new Exception();
}
There are something misunderstanding. I lock the drive through Bitlocker manager panel in Windows rather than assign the value directly to the property. At that situation, this property would not change itself unless the process restarted.
I haven't tested your solution but I don't think this issue related to the disposal of ShellItemPropertyStore. I ran the same code again after the drive unlocked/locked.
Please try my code then without setting the BitLocker property, especially setting the ReadOnly
and Temporary
properties as shown, to see if your scenario works.
Any success?
I could not test the code as it threw InvalidCastException
using (ShellItem Item = new ShellItem("D:\\"))
{
Item.Properties.ReadOnly = false;
Item.Properties.Temporary = false;
//InvalidCastException threw here
if (Item.Properties.TryGetValue(ReadOnlyPropertyStore.GetPropertyKeyFromName("System.Volume.BitLockerProtection"), out object RawObject))
{
if (int.TryParse(Convert.ToString(RawObject), out int BitlockerState))
{
return BitlockerState;
}
}
}
StackTrace
at Vanara.PInvoke.Shell32.IShellItem2.GetPropertyStore(GETPROPERTYSTOREFLAGS flags, Guid& riid)
at Vanara.Windows.Shell.ShellItemPropertyStore.GetIPropertyStore()
at Vanara.Windows.Shell.ReadOnlyPropertyStore.Run[T](Func`2 action)
at Vanara.Windows.Shell.ReadOnlyPropertyStore.TryGetValue[TVal](PROPERTYKEY key, TVal& value)
at Vanara.Windows.Shell.ReadOnlyPropertyStore.TryGetValue(PROPERTYKEY key, Object& value)
The exception was thew here:
Sorry for the long wait, but I found the problem. This code works:
using ShellItem Item = new("C:\\");
//Item.Properties.ReadOnly = false; // This is the problem!!
Item.Properties.Temporary = false;
if (Item.Properties.TryGetValue<int>(ReadOnlyPropertyStore.GetPropertyKeyFromName("System.Volume.BitLockerProtection"), out int BitlockerState))
{
// Do something with value
}
Describe the bug and how to reproduce
Make sure the application is running before you unlock the Bitlocker on D:\
It will not change until you restart the application
What code is involved
Expected behavior ShellItemPropertyStore should change itself once Windows modify the property of the file/folder
Screenshots