I am changing the tag of a folder my either creating or modifying the desktop.ini file in Windows 10.
Without notifying the OS that it needs to refresh in some way, the tag takes an indeterminate amount of time to reflect as changed within Windows Explorer.
I am trying to use a Windows API function to notify the OS that an attribute on the folder has changed but it does not cause Windows Explorer to update. The following combinations have been tried:
[DllImport("shell32.dll")]
static extern void SHChangeNotify(HChangeNotifyEventID wEventId,
HChangeNotifyFlags uFlags,
IntPtr dwItem1,
IntPtr dwItem2);
public static void Refresh1()
{
var ptr = Marshal.StringToHGlobalUni(@"C:\tmp\test");
SHChangeNotify(HChangeNotifyEventID.SHCNE_ATTRIBUTES, HChangeNotifyFlags.SHCNF_PATHW, ptr, IntPtr.Zero);
Marshal.FreeHGlobal(ptr);
}
public static void Refresh2()
{
var ptr = Marshal.StringToHGlobalUni(@"C:\tmp\test\desktop.ini");
SHChangeNotify(HChangeNotifyEventID.SHCNE_DELETE, HChangeNotifyFlags.SHCNF_PATHW, ptr, IntPtr.Zero);
Marshal.FreeHGlobal(ptr);
}
public static void Refresh3()
{
var ptr = Marshal.StringToHGlobalUni(@"C:\tmp\test\desktop.ini");
SHChangeNotify(HChangeNotifyEventID.SHCNE_UPDATEITEM, HChangeNotifyFlags.SHCNF_PATHW, ptr, IntPtr.Zero);
Marshal.FreeHGlobal(ptr);
}
public static void Refresh4()
{
var ptr = Marshal.StringToHGlobalUni(@"C:\tmp\test");
SHChangeNotify(HChangeNotifyEventID.SHCNE_UPDATEITEM, HChangeNotifyFlags.SHCNF_PATHW, ptr, IntPtr.Zero);
Marshal.FreeHGlobal(ptr);
}
public static void Refresh5()
{
var ptr = Marshal.StringToHGlobalUni(@"C:\tmp\test");
SHChangeNotify(HChangeNotifyEventID.SHCNE_RENAMEFOLDER, HChangeNotifyFlags.SHCNF_PATHW, ptr, IntPtr.Zero);
Marshal.FreeHGlobal(ptr);
}
public static void Refresh6()
{
var ptr = Marshal.StringToHGlobalUni(@"C:\tmp\test");
SHChangeNotify(HChangeNotifyEventID.SHCNE_RENAMEFOLDER, HChangeNotifyFlags.SHCNF_PATHW, ptr, ptr);
Marshal.FreeHGlobal(ptr);
}
Based on the comments below I tried a different approach to Marshaling and the many permutations...
[DllImport("shell32.dll")]
static extern void SHChangeNotify(HChangeNotifyEventID wEventId, HChangeNotifyFlags uFlags, [MarshalAs(UnmanagedType.LPWStr)] string dwItem1, [MarshalAs(UnmanagedType.LPWStr)] string dwItem2);
public static void Refresh1()
{
var str = @"C:\tmp\test";
SHChangeNotify(HChangeNotifyEventID.SHCNE_ATTRIBUTES, HChangeNotifyFlags.SHCNF_PATHW, str, str);
}
public static void Refresh2()
{
var str = @"C:\tmp\test";
SHChangeNotify(HChangeNotifyEventID.SHCNE_UPDATEITEM, HChangeNotifyFlags.SHCNF_PATHW, str, "");
}
public static void Refresh3()
{
var str = @"C:\tmp\test";
SHChangeNotify(HChangeNotifyEventID.SHCNE_UPDATEITEM, HChangeNotifyFlags.SHCNF_PATHW, str, str);
}
public static void Refresh4()
{
var str = @"C:\tmp\test";
SHChangeNotify(HChangeNotifyEventID.SHCNE_ATTRIBUTES, HChangeNotifyFlags.SHCNF_PATHW, str, "");
}
I have also tried HChangeNotifyEventID.SHCNE_UPDATEDIR
as an option for the first parameter but that also has not succesfully updated Windows Explorer.
How can I change the tag of a folder in such a manner that it automatically refreshes in Windows Explorer?
- Is their a way to modify the code above to do that?
- Is their another windows API function that I should call to refresh Windows Explorer after a change to desktop.ini (even though the naming of this one implies that it should do the job...)?
- Should I be using a specific Windows API call just to change the tag directly that has a built in notification to the OS?