2015-08-26 130 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),

我认为你将被迫手动刷新或重新启动浏览器。