【问题标题】:How to correctly initialize the Windows desktop (explorer.exe) of Windows 8如何正确初始化Windows 8的Windows桌面(explorer.exe)
【发布时间】:2014-01-31 22:36:05
【问题描述】:

我需要为 Windows 8.1 开发一个很小的应用程序,它应该包含很少的代码行,因为唯一的目的是在第 3 方程序杀死 explorer.exe 后初始化 Windows 8 操作系统桌面(不是启动屏幕) .

我会更好地解释这一点:

我已经进行了 Windows 8 的无人值守安装,需要静默安装特定程序(程序为 StartIsBackPlus),要静默安装程序作者说使用参数/silent 执行安装程序:

StartIsBackPlus_Setup.exe /Silent

好吧,如果您没有注意到该程序是 Windows 8.1 的 Windows 开始按钮替代品,而我遇到的问题是,在该程序的静默安装完成后,该程序会杀死/关闭“桌面"(explorer.exe进程),任务栏和墙消失,只显示纯色背景。

要解决这个问题,我的意思是手动重新初始化桌面,打开任务管理器并运行一个新进程(explorer.exe)就这么简单,但正如我所说,这需要完全无人看管,目前我尝试重现此步骤(例如从 CMD 或从开发的 .NET 应用程序静默打开 explorer.exe)它只是打开一个新的资源管理器窗口,我不明白为什么 CMD 只打开一个资源管理器窗口但如果我启动TaskManager 中的 explorer.exe 进程会正确地重新初始化整个桌面。

然后我需要编写一个方法来重新初始化 Windows 桌面的元素(资源管理器和任务栏),就像 TaskManager 在手动运行 explorer.exe 时所做的那样,在安装了这个有问题的第 3 方程序之后。

...但是不像使用Process.Start方法启动explorer.exe进程那么容易,因为我已经尝试过了,它只打开一个新的资源管理器窗口,桌子仍然消失,然后是一个简单的进程.Start("Explorer.exe") 未正确初始化此场景中的 Windows 桌面。

那么...在这样的第 3 方程序杀死 explorer.exe 后,初始化 Windows 桌面的正确方法是什么?

【问题讨论】:

  • 正如@LB 试图说的那样——听起来你有一个不同的问题——你不需要初始化 Windows 桌面,而是找到一种方法来阻止它首先杀死它也许是地方(或其他东西)?
  • @Chris 我会解释详细信息,该应用程序是一个 StartButton 安装程序,它取代了 windows 8 的启动按钮,安装后它关闭 explorer.exe(屏幕变为纯色,它删除了任务栏太),那么如果我只是重新启动 explorer.exe 进程,桌面元素不会初始化它只会打开一个新的资源管理器窗口。
  • 这是因为我为一个应用程序做了一个自定义的静默安装程序,安装后会自动关闭 explorer.exe, IIRC ClassicShell 安装时不会杀死 explorer,所以也许可以做它的工作...而且不要杀死探险家...
  • @ta.speot.is 感谢您的评论,但使用 ClassicShell 替换对我来说不是一个解决方案,我需要使用我评论过的软件我已经付费了

标签: c# .net vb.net windows windows-8


【解决方案1】:

这是凭记忆,但试试这个:

 myProcess = New Process()
 myProcess.StartInfo.FileName = "C:\Windows\explorer.exe"
 myProcess.StartInfo.UseShellExecute = True
 myProcess.StartInfo.WorkingDirectory = Application.StartupPath
 myProcess.StartInfo.CreateNoWindow = True

 myProcess.Start()

我不得不说,我认为这可能是作者应该知道/处理的事情。获得价值 3 美元的支持;)

【讨论】:

  • 它工作得很好,我不明白为什么设置 startinfo 工作(所有桌子都重新初始化)和不设置 startinfo 不起作用(只有一个新的资源管理器窗口是打开),也许是因为 ShellExecute 属性?无论如何,谢谢!
  • 我认为这可能是由于CreateNoWindow: explorer 假设正在请求桌面内容,或者这就是它所知道的与 NoWindow 的关系
  • CreateNoWindow 与此工作无关:msdn.microsoft.com/en-us/library/… "如果 UseShellExecute 属性为 true 或 UserName 和 Password 属性不为 null,则忽略 CreateNoWindow 属性值并创建一个新窗口。 "
  • explorer.exe 显然不是这种情况:所有这些都是正确的,但没有创建资源管理器窗口。
【解决方案2】:

这是我正确重新初始化桌面的小工具的代码,也许它可以改进更多(使用比任务栏更好的元素检查)......我知道,但至少在我的情况下这很好用:

#Region " Imports "

Imports System.IO
Imports System.Runtime.InteropServices
Imports RefreshExplorer.NativeMethods

#End Region

''' <summary>
''' Initializes a new instance of Explorer process.
''' </summary>
Module RefreshExplorer

#Region " P/Invoke "

    ''' <summary>
    ''' Class NativeMethods.
    ''' </summary>
    Friend Class NativeMethods

        ''' <summary>
        ''' Retrieves a handle to the top-level window whose class name and window name match the specified strings. 
        ''' This function does not search child windows. 
        ''' This function does not perform a case-sensitive search.
        ''' </summary>
        ''' <param name="lpClassName">
        ''' The class name or a class atom created by a previous call to the 
        ''' RegisterClass or RegisterClassEx function.
        ''' The atom must be in the low-order word of lpClassName; 
        ''' the high-order word must be zero.
        ''' If lpClassName points to a string, it specifies the window class name. 
        ''' The class name can be any name registered with RegisterClass or RegisterClassEx, 
        ''' or any of the predefined control-class names.
        ''' If lpClassName is NULL, it finds any window whose title matches the lpWindowName parameter.
        ''' </param>
        ''' <param name="zero">
        ''' The window name (the window's title). 
        ''' If this parameter is NULL, all window names match.</param>
        ''' <returns>IntPtr.</returns>
        <DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function FindWindowByClass(
            ByVal lpClassName As String,
            ByVal zero As IntPtr
        ) As IntPtr
        End Function

    End Class

#End Region

#Region " ReadOnly Strings "

    ''' <summary>
    ''' Indicates the directory where the Explorer process is located.
    ''' </summary>
    Private ReadOnly ExplorerDirectory As String =
        Environment.GetFolderPath(Environment.SpecialFolder.Windows)

    ''' <summary>
    ''' Indicates the filename of the process.
    ''' </summary>
    Private ReadOnly ExplorerFilename As String = "Explorer.exe"

    ''' <summary>
    ''' Indicates the desk Taskbar Class-name.
    ''' </summary>
    Private ReadOnly TaskBarClassName As String = "Shell_TrayWnd"

#End Region

#Region " Process "

    ''' <summary>
    ''' The explorer process.
    ''' </summary>
    Dim Explorer As New Process With
    {
     .StartInfo = New ProcessStartInfo With
                  {
                   .FileName = Path.Combine(ExplorerDirectory, ExplorerFilename),
                   .WorkingDirectory = My.Application.Info.DirectoryPath,
                   .UseShellExecute = True
                  }
    }

#End Region

#Region " Main "

    ''' <summary>
    ''' Defines the entry point of the application.
    ''' </summary>
    Sub Main()

        Select Case Convert.ToBoolean(CInt(FindWindowByClass(TaskBarClassName, Nothing)))

            Case False ' The Taskbar is not found.

                Try
                    Explorer.Start()
                    Console.WriteLine("Windows Explorer has been re-initialized successfully")
                    Environment.Exit(0)

                Catch ex As Exception
                    Console.WriteLine(ex.Message)
                    Debug.WriteLine(ex.Message)
                    Environment.Exit(1)

                End Try

            Case True ' The Taskbar is found.
                Console.WriteLine("Can't proceed, Windows Explorer is currently running. Exiting...")
                Environment.Exit(2)

        End Select

    End Sub

#End Region

End Module

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2014-04-10
    相关资源
    最近更新 更多