2013-07-18 82 views
2

我試圖在更改註冊表中的值後刷新系統(Windows 8),此值影響資源管理器設置,是顯示/隱藏複選框的值:如何在註冊表更改後刷新資源管理器

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Shown 
    RegEdit.Set_Value("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced", _ 
         "AutoCheckSelect", "1", Microsoft.Win32.RegistryValueKind.DWord) 
End Sub 

以及刷新系統我用這個:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    SHChangeNotify(HChangeNotifyEventID.SHCNE_ASSOCCHANGED, HChangeNotifyFlags.SHCNF_IDLIST, IntPtr.Zero, IntPtr.Zero) 
End Sub 

我已經嘗試了很多標誌組合。

問題是瀏覽器的窗口刷新,但複選框沒有出現(或沒有被刪除),我仍然需要在每個資源管理器窗口中按F5進行完整的更新刷新。

這是全碼:

Imports System.Runtime.InteropServices 

Public Class Form1 

<DllImport("shell32.dll")> _ 
Shared Sub SHChangeNotify(_ 
ByVal wEventID As HChangeNotifyEventID, _ 
ByVal uFlags As HChangeNotifyFlags, _ 
ByVal dwItem1 As IntPtr, _ 
ByVal dwItem2 As IntPtr) 
End Sub 

<Flags()> _ 
Public Enum HChangeNotifyFlags 
    ' <summary> 
    ' The <i>dwItem1</i> and <i>dwItem2</i> parameters are DWORD values. 
    ' </summary> 
    SHCNF_DWORD = &H3 
    ' <summary> 
    ' <i>dwItem1</i> and <i>dwItem2</i> are the addresses of ITEMIDLIST structures that 
    ' represent the item(s) affected by the change. 
    ' Each ITEMIDLIST must be relative to the desktop folder. 
    ' </summary> 
    SHCNF_IDLIST = &H0 
    ' <summary> 
    ' <i>dwItem1</i> and <i>dwItem2</i> are the addresses of null-terminated strings of 
    ' maximum length MAX_PATH that contain the full path names 
    ' of the items affected by the change. 
    ' </summary> 
    SHCNF_PATHA = &H1 
    ' <summary> 
    ' <i>dwItem1</i> and <i>dwItem2</i> are the addresses of null-terminated strings of 
    ' maximum length MAX_PATH that contain the full path names 
    ' of the items affected by the change. 
    ' </summary> 
    SHCNF_PATHW = &H5 
    ' <summary> 
    ' <i>dwItem1</i> and <i>dwItem2</i> are the addresses of null-terminated strings that 
    ' represent the friendly names of the printer(s) affected by the change. 
    ' </summary> 
    SHCNF_PRINTERA = &H2 
    ' <summary> 
    ' <i>dwItem1</i> and <i>dwItem2</i> are the addresses of null-terminated strings that 
    ' represent the friendly names of the printer(s) affected by the change. 
    ' </summary> 
    SHCNF_PRINTERW = &H6 
    ' <summary> 
    ' The function should not return until the notification 
    ' has been delivered to all affected components. 
    ' As this flag modifies other data-type flags it cannot by used by itself. 
    ' </summary> 
    SHCNF_FLUSH = &H1000 
    ' <summary> 
    ' The function should begin delivering notifications to all affected components 
    ' but should return as soon as the notification process has begun. 
    ' As this flag modifies other data-type flags it cannot by used by itself. 
    ' </summary> 
    SHCNF_FLUSHNOWAIT = &H2000 
End Enum 


<Flags()> _ 
Public Enum HChangeNotifyEventID 
    ' <summary> 
    ' All events have occurred. 
    ' </summary> 
    SHCNE_ALLEVENTS = &H7FFFFFFF 

    ' <summary> 
    ' A file type association has changed. <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> 
    ' must be specified in the <i>uFlags</i> parameter. 
    ' <i>dwItem1</i> and <i>dwItem2</i> are not used and must be <see langword="null"/>. 
    ' </summary> 
    SHCNE_ASSOCCHANGED = &H8000000 

    ' <summary> 
    ' The attributes of an item or folder have changed. 
    ' <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> or 
    ' <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>. 
    ' <i>dwItem1</i> contains the item or folder that has changed. 
    ' <i>dwItem2</i> is not used and should be <see langword="null"/>. 
    ' </summary> 
    SHCNE_ATTRIBUTES = &H800 

    ' <summary> 
    ' A nonfolder item has been created. 
    ' <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> or 
    ' <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>. 
    ' <i>dwItem1</i> contains the item that was created. 
    ' <i>dwItem2</i> is not used and should be <see langword="null"/>. 
    ' </summary> 
    SHCNE_CREATE = &H2 

    ' <summary> 
    ' A nonfolder item has been deleted. 
    ' <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> or 
    ' <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>. 
    ' <i>dwItem1</i> contains the item that was deleted. 
    ' <i>dwItem2</i> is not used and should be <see langword="null"/>. 
    ' </summary> 
    SHCNE_DELETE = &H4 

    ' <summary> 
    ' A drive has been added. 
    ' <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> or 
    ' <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>. 
    ' <i>dwItem1</i> contains the root of the drive that was added. 
    ' <i>dwItem2</i> is not used and should be <see langword="null"/>. 
    ' </summary> 
    SHCNE_DRIVEADD = &H100 

    ' <summary> 
    ' A drive has been added and the Shell should create a new window for the drive. 
    ' <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> or 
    ' <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>. 
    ' <i>dwItem1</i> contains the root of the drive that was added. 
    ' <i>dwItem2</i> is not used and should be <see langword="null"/>. 
    ' </summary> 
    SHCNE_DRIVEADDGUI = &H10000 

    ' <summary> 
    ' A drive has been removed. <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> or 
    ' <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>. 
    ' <i>dwItem1</i> contains the root of the drive that was removed. 
    ' <i>dwItem2</i> is not used and should be <see langword="null"/>. 
    ' </summary> 
    SHCNE_DRIVEREMOVED = &H80 

    ' <summary> 
    ' Not currently used. 
    ' </summary> 
    ' SHCNE_EXTENDED_EVENT = &H4000000 

    ' <summary> 
    ' The amount of free space on a drive has changed. 
    ' <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> or 
    ' <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>. 
    ' <i>dwItem1</i> contains the root of the drive on which the free space changed. 
    ' <i>dwItem2</i> is not used and should be <see langword="null"/>. 
    ' </summary> 
    SHCNE_FREESPACE = &H40000 

    ' <summary> 
    ' Storage media has been inserted into a drive. 
    ' <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> or 
    ' <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>. 
    ' <i>dwItem1</i> contains the root of the drive that contains the new media. 
    ' <i>dwItem2</i> is not used and should be <see langword="null"/>. 
    ' </summary> 
    SHCNE_MEDIAINSERTED = &H20 

    ' <summary> 
    ' Storage media has been removed from a drive. 
    ' <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> or 
    ' <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>. 
    ' <i>dwItem1</i> contains the root of the drive from which the media was removed. 
    ' <i>dwItem2</i> is not used and should be <see langword="null"/>. 
    ' </summary> 
    SHCNE_MEDIAREMOVED = &H40 

    ' <summary> 
    ' A folder has been created. <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> 
    ' or <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>. 
    ' <i>dwItem1</i> contains the folder that was created. 
    ' <i>dwItem2</i> is not used and should be <see langword="null"/>. 
    ' </summary> 
    SHCNE_MKDIR = &H8 

    ' <summary> 
    ' A folder on the local computer is being shared via the network. 
    ' <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> or 
    ' <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>. 
    ' <i>dwItem1</i> contains the folder that is being shared. 
    ' <i>dwItem2</i> is not used and should be <see langword="null"/>. 
    ' </summary> 
    SHCNE_NETSHARE = &H200 

    ' <summary> 
    ' A folder on the local computer is no longer being shared via the network. 
    ' <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> or 
    ' <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>. 
    ' <i>dwItem1</i> contains the folder that is no longer being shared. 
    ' <i>dwItem2</i> is not used and should be <see langword="null"/>. 
    ' </summary> 
    SHCNE_NETUNSHARE = &H400 

    ' <summary> 
    ' The name of a folder has changed. 
    ' <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> or 
    ' <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>. 
    ' <i>dwItem1</i> contains the previous pointer to an item identifier list (PIDL) or name of the folder. 
    ' <i>dwItem2</i> contains the new PIDL or name of the folder. 
    ' </summary> 
    SHCNE_RENAMEFOLDER = &H20000 

    ' <summary> 
    ' The name of a nonfolder item has changed. 
    ' <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> or 
    ' <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>. 
    ' <i>dwItem1</i> contains the previous PIDL or name of the item. 
    ' <i>dwItem2</i> contains the new PIDL or name of the item. 
    ' </summary> 
    SHCNE_RENAMEITEM = &H1 

    ' <summary> 
    ' A folder has been removed. 
    ' <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> or 
    ' <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>. 
    ' <i>dwItem1</i> contains the folder that was removed. 
    ' <i>dwItem2</i> is not used and should be <see langword="null"/>. 
    ' </summary> 
    SHCNE_RMDIR = &H10 

    ' <summary> 
    ' The computer has disconnected from a server. 
    ' <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> or 
    ' <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>. 
    ' <i>dwItem1</i> contains the server from which the computer was disconnected. 
    ' <i>dwItem2</i> is not used and should be <see langword="null"/>. 
    ' </summary> 
    SHCNE_SERVERDISCONNECT = &H4000 

    ' <summary> 
    ' The contents of an existing folder have changed 
    ' but the folder still exists and has not been renamed. 
    ' <see cref="HChangeNotifyFlags.SHCNF_IDLIST"/> or 
    ' <see cref="HChangeNotifyFlags.SHCNF_PATH"/> must be specified in <i>uFlags</i>. 
    ' <i>dwItem1</i> contains the folder that has changed. 
    ' <i>dwItem2</i> is not used and should be <see langword="null"/>. 
    ' If a folder has been created deleted or renamed use SHCNE_MKDIR SHCNE_RMDIR or 
    ' SHCNE_RENAMEFOLDER respectively instead. 
    ' </summary> 
    SHCNE_UPDATEDIR = &H1000 

    ' <summary> 
    ' An image in the system image list has changed. 
    ' <see cref="HChangeNotifyFlags.SHCNF_DWORD"/> must be specified in <i>uFlags</i>. 
    ' </summary> 
    SHCNE_UPDATEIMAGE = &H8000 
End Enum 


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Shown 
    RegEdit.Set_Value("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced", _ 
         "AutoCheckSelect", "0", Microsoft.Win32.RegistryValueKind.DWord) 
End Sub 


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    SHChangeNotify(HChangeNotifyEventID.SHCNE_ASSOCCHANGED, HChangeNotifyFlags.SHCNF_IDLIST, IntPtr.Zero, IntPtr.Zero) 
End Sub 

End Class 
+0

它只是我的suggestion.why你不使用計時器? –

+0

@sentil kumar謝謝,但你能更具體請,一個計時器的意圖?關鍵是修改後,我嘗試刷新系統,那麼爲什麼你認爲我需要一個計時器,你認爲計時器滴答會做什麼? – ElektroStudios

回答

1

你可以嘗試一些便宜的把戲。

- Send a "F5" keystroke to the explorer window so you don't have to do it manually. 
- kill explorer.exe, and relaunch it. 

看看這個。 How to refresh Windows Explorer

+0

感謝您的回答,您的第二個建議將會生效,但我的目的是刷新該屬性更改的資源管理器窗口,而不是通過重新啓動資源管理器進程來終止所有打開的窗口。發送F5的建議在這種情況下,我需要列出所有打開的資源管理器窗口,激活 - 逐一聚焦每個窗口,然後發送F5鍵...我認爲,如果用戶擁有50個打開的窗口,tat並不是很有效。我的意圖是不與用戶交互的窗口,這就是爲什麼我試圖使用API​​以沉默和改進的方式來做,或尋找一種類似的方式來做到這一點。 – ElektroStudios

+0

http://stackoverflow.com/questions/4940603/how-to-refresh-windows-explorer –

2

你可以嘗試使用SHChangeNotify()和SHCNE_ALLEVENTS事件,它可能會工作。之後它可能刷新探險家。否則,你不得不想一些其他的技巧

+0

它不工作... – ElektroStudios

1

我正在嘗試做類似的事情 - 更新開始菜單的註冊表設置,然後立即讓開始菜單反映更改。

The solution from this MSDN question完美地爲我工作。

您可以嘗試廣播WM_SETTINGCHANGE消息。例如:

class Program 
{ 
    [DllImport("user32.dll", SetLastError = true)] 
    private static extern IntPtr SendMessageTimeout(IntPtr hWnd, int Msg, IntPtr wParam, string lParam, uint fuFlags, uint uTimeout, IntPtr lpdwResult); 

    private static readonly IntPtr HWND_BROADCAST = new IntPtr(0xffff); 
    private const int WM_SETTINGCHANGE = 0x1a; 
    private const int SMTO_ABORTIFHUNG = 0x0002; 

    static void Main(string[] args) 
    { 
     SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, IntPtr.Zero, null, SMTO_ABORTIFHUNG, 100, IntPtr.Zero); 
    } 
} 
+0

nope,它不適合我,我仍然需要按「F5」看到打開資源管理器中的更改。 – ElektroStudios

+0

反正這是非常有趣的,也許它適用於其他特定的更改,請指出您嘗試過什麼樣的系統更改? – ElektroStudios

+0

這是專門更新開始菜單的使用大圖標設置。 '[HKEY_CURRENT_USER \ Software \ Microsoft \ Windows \ CurrentVersion \ Explorer \ Advanced]''「Start_LargeMFUIcons」= dword:00000000' – mcw0933

相關問題