2015-08-26 57 views
4

我正在重新訪問我在VB.Net中爲我的幫助臺團隊編寫的一個工具,並希望添加一些複選框以複製Windows用於顯示的相同功能隱藏的文件和文件夾/重新隱藏,以及受保護的操作系統文件。複製Windows取消隱藏文件夾和文件功能

我知道我可以通過編輯註冊表項並重新啓動explorer.exe來完成此操作,但是會關閉所有打開的資源管理器窗口,我不想那樣做。

有誰知道Windows如何通過簡單點擊一個複選框以及我如何能夠在VB.net中對其進行編碼來完成此操作?

對此的任何輸入都非常感謝。


編輯:所以它看起來像我發現作品刷新其可應用於Drarig的回答下面,但我無法將其轉換爲VB.net Windows資源管理器/文件瀏覽器的刷新方法最初的例子是在C#中。

'Original at http://stackoverflow.com/questions/2488727/refresh-windows-explorer-in-win7 

Private Sub refreshExplorer(ByVal explorerType As String) 
    Dim CLSID_ShellApplication As Guid = Guid.Parse("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", Reflection.BindingFlags.InvokeMethod, Nothing, shellApplication, New Object() {}) 
    Dim windowsType As Type = windows.GetType() 
    Dim count As Object = windowsType.InvokeMember("Count", Reflection.BindingFlags.GetProperty, Nothing, windows, Nothing) 

    For i As Integer = 0 To CType(count, Integer) 
     Dim item As Object = windowsType.InvokeMember("Item", Reflection.BindingFlags.InvokeMethod, Nothing, windows, New Object() {i}) 
     Dim itemType As Type = item.GetType() 

     'Only fresh Windows explorer Windows 
     Dim itemName As String = CType(itemType.InvokeMember("Name", Reflection.BindingFlags.GetProperty, Nothing, item, Nothing), String) 
     If itemName = explorerType Then 
      itemType.InvokeMember("Refresh", Reflection.BindingFlags.InvokeMethod, Nothing, item, Nothing) 
     End If 
    Next 
End Sub 

我正在一個例外對象引用不設置爲一個對象的實例當我設置ITEMTYPE爲Type = item.GetType()上方。我無法弄清楚哪個對象沒有被創建。當我單步執行代碼時,它看起來像windowsType包含一個對象windows。有沒有人有這個想法?一旦解決問題,我可以將它應用於下面的Drarig解決方案。

+0

您可以使用此:http://www.askvg.com/create-simple-script-to-show隱藏文件和文件夾在Windows XP的Vista和7 /並運行它與vb.net,或在vb.net翻譯它。 – Drarig29

+0

這真棒Drarig29,我昨晚發現這篇文章完全一樣。謝謝你的驗證,雖然:)我會更新此線程與答案,一旦我把它翻譯成VB.net。 – ganjeii

+0

它也讓我感興趣,我可能會在代碼上工作併發布答案。 – Drarig29

回答

1

好吧我希望我能早日得到這個給你,但最近忙着工作。今天我花了一點時間弄清楚這一點,因爲我喜歡挖掘我以前從未做過的事情。這是一個新項目的全班同學;沒有時間把它包裝在一個單獨的課堂上。我相信這會讓你得到你所需要的。這比獲得正確的句柄然後發送命令要困難得多,但我明白了。希望對你有幫助。

P.S.你可以省略一些東西,特別是用於加載的布爾值,這樣我可以將當​​前值拉回加載並檢查/取消選中CheckBox

注:這是久經考驗的Windows 7,第8和10

Imports Microsoft.Win32 
Imports System.Reflection 
Imports System.Runtime.InteropServices 

Public Class Form1 

    <Flags()> _ 
    Public Enum KeyboardFlag As UInteger 
     KEYBOARDF_5 = &H74 
    End Enum 

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _ 
    Private Shared Function GetWindow(ByVal hl As Long, ByVal vm As Long) As IntPtr 
    End Function 

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _ 
    Private Shared Function PostMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Boolean 
    End Function 

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _ 
    Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr 
    End Function 

    Private blnLoading As Boolean = False 

    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged 
     Form1.HideFilesExtension(Me.CheckBox1.Checked) 
     If Not blnLoading Then NotifyFileAssociationChanged() 
     RefreshExplorer() 
    End Sub 

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     Dim name As String = "Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" 
     Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey(name, False) 

     blnLoading = True 
     Me.CheckBox1.Checked = CBool(key.GetValue("Hidden")) 
     key.Close() 

     blnLoading = False 
    End Sub 

    Private Shared Sub HideFilesExtension(ByVal Hide As Boolean) 
     Dim name As String = "Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" 
     Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey(name, True) 
     key.SetValue("Hidden", If(Hide, 1, 0)) 
     key.Close() 
    End Sub 

    Public Shared Sub RefreshExplorer() 
     Dim clsid As New Guid("13709620-C279-11CE-A49E-444553540000") 
     Dim typeFromCLSID As Type = Type.GetTypeFromCLSID(clsid, True) 
     Dim objectValue As Object = Activator.CreateInstance(typeFromCLSID) 
     Dim obj4 As Object = typeFromCLSID.InvokeMember("Windows", BindingFlags.InvokeMethod, Nothing, objectValue, New Object(0 - 1) {}) 
     Dim type1 As Type = obj4.GetType 
     Dim obj2 As Object = type1.InvokeMember("Count", BindingFlags.GetProperty, Nothing, obj4, Nothing) 
     If (CInt(obj2) <> 0) Then 
      Dim num2 As Integer = (CInt(obj2) - 1) 
      Dim i As Integer = 0 
      Do While (i <= num2) 
       Dim obj5 As Object = type1.InvokeMember("Item", BindingFlags.InvokeMethod, Nothing, obj4, New Object() {i}) 
       Dim type3 As Type = obj5.GetType 
       Dim str As String = CStr(type3.InvokeMember("Name", BindingFlags.GetProperty, Nothing, obj5, Nothing)) 
       If (str = "File Explorer") Then 
        type3.InvokeMember("Refresh", BindingFlags.InvokeMethod, Nothing, obj5, Nothing) 
       End If 
       i += 1 
      Loop 
     End If 

    End Sub 

    Public Shared Sub NotifyFileAssociationChanged() 
     'Find the actual window... 
     Dim hwnd As IntPtr = FindWindow("Progman", "Program Manager") 

     'Get the window handle and refresh option... 
     Dim j = GetWindow(hwnd, 3) 

     'Finally post the message... 
     PostMessage(j, 256, KeyboardFlag.KEYBOARDF_5, 3) 
    End Sub 


End Class 
+1

非常感謝你在這方面的努力!這看起來正是我正在尋找的東西,我現在正在將它很好地綁定到一個類中,並且只要我有工作,就會將其標記爲答案。你爲我節省了很多時間!我無法強調,你在這裏做了很棒的工作。我正在從C#翻譯[**這篇文章**](http://stackoverflow.com/questions/17503289/how-to-refresh-reload-desktop?rq=1)中的答案,它真的導致了可怕的頭痛 – ganjeii

+1

它的ALIVEEE! **適用於Win7 -10 **。再次感謝你的幫助!我還很小,所以學習這樣的新東西很令人興奮! – ganjeii

+0

歡迎有點頭疼,但我明白了。 – Codexer

-1

下面是除了資源管理器的刷新之外的一切的解決方案。 我翻譯了代碼,但我無法找到如何刷新資源管理器/桌面而無需重新啓動它。

Const keyName As String = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" 
Const Hidden As String = "Hidden" 
Const SHidden As String = "ShowSuperHidden" 

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Dim St As Integer = GetRegValue(Hidden) 

    If St = 2 Then 
     SetRegValue(Hidden, 1) 
     SetRegValue(SHidden, 1) 
    Else 
     SetRegValue(Hidden, 2) 
     SetRegValue(SHidden, 0) 
    End If 
End Sub 

Private Function GetRegValue(valueName As String) As Integer 
    Return CInt(My.Computer.Registry.GetValue(keyName, valueName, 0)) 
End Function 

Private Sub SetRegValue(valueName As String, value As Integer) 
    My.Computer.Registry.SetValue(keyName, valueName, value, Microsoft.Win32.RegistryValueKind.DWord) 
End Sub 

我有一些想法,以刷新桌面:

  • 發送鍵,正在運行的進程。我想這(source):

    Dim pp As Process() = Process.GetProcessesByName("explorer") 
    
    If pp.Length > 0 Then 
        For Each p In pp 
         AppActivate(p.Id) 
         SendKeys.SendWait("{F5}") 
        Next 
    End If 
    
    • 刷新使用SHChangeNotifysource),
    • 刷新廣播WM_SETTINGCHANGE消息(source),

我認爲你將被迫手動刷新或重新啓動瀏覽器。