Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Change Registry Settings and Broadcast Changes (Refresh Desktop and Windows Explorer)

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
2 Jun 2011CPOL 20.5K   3   1
[solved]

Hello all. I found a solution that can change Registry settings and broadcast the changes to the Desktop and Windows Explorer. Here is the code for changing Show/Hide File Extension:


VB
Private Shared Sub HideFilesExtension(ByVal Hide As Boolean)
    Dim root As String = "Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced"
    Dim HideFileExt As RegistryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(root, True)
    HideFileExt.SetValue("HideFileExt", IIf(Hide, 1, 0))
    HideFileExt.Close()
    mhkwins.commons.RefreshExplorer()
End Sub

Here is the code to refresh the Desktop and Window Explorer:


VB
Private Declare Function SHChangeNotify Lib "Shell32.dll" (ByVal wEventID As Int32, _
        ByVal uFlags As Int32, ByVal dwItem1 As Int32, ByVal dwItem2 As Int32) As Int32
  Public Shared Sub RefreshExplorer()
    Call Shell("explorer /select," & "C:\DATA\", AppWinStyle.NormalFocus)
    Threading.Thread.Sleep(3000)
    SHChangeNotify(&H8000000, &H0, 0, 0)
    Dim CLSID_ShellApplication As New Guid("13709620-C279-11CE-A49E-444553540000")
    Dim shellApplicationType As Type = Type.GetTypeFromCLSID(CLSID_ShellApplication, True)
    Dim shellApplication As Object = Activator.CreateInstance(shellApplicationType)
    Dim windows As Object = shellApplicationType.InvokeMember("Windows", _
        System.Reflection.BindingFlags.InvokeMethod, Nothing, shellApplication, New Object() {})
    Dim windowsType As Type = windows.[GetType]()
    Dim count As Object = windowsType.InvokeMember("Count", _
        System.Reflection.BindingFlags.GetProperty, Nothing, windows, Nothing)
    If CInt(count) = 0 Then Exit Sub
    For i As Integer = 0 To CInt(count) - 1
      Dim item As Object = windowsType.InvokeMember("Item", System.Reflection.BindingFlags.InvokeMethod, _
                           Nothing, windows, New Object() {i})
      Dim itemType As Type = item.[GetType]()
      ' only refresh windows explorers       
      Dim itemName As String = DirectCast(itemType.InvokeMember("Name", _
          System.Reflection.BindingFlags.GetProperty, Nothing, item, Nothing), String)
      'MsgBox(itemName)
      If itemName = "Windows Explorer" Then
        itemType.InvokeMember("Refresh", System.Reflection.BindingFlags.InvokeMethod, Nothing, item, Nothing)
      End If
    Next
End Sub

That's all.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
ytfrdfiw3-Sep-12 19:38
ytfrdfiw3-Sep-12 19:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.