停止 SetVolumeMountPoint 打开文件资源管理器

Stop SetVolumeMountPoint from opening file explorer

我正在使用 SetVolumeMountPoint 将 vhd 挂载到我选择的驱动器盘符。问题是当安装 vhd 时,文件资源管理器会自动在新的驱动器目录中打开。这对我来说是个问题,因为我需要我的程序保留在前台,有时生成的文件资源管理器会成为前台的一部分。

https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-setvolumemountpointa

想法?

更新:

我在安装我的 vhd 之前使用这两种方法以编程方式设置了 noautorun 注册表项:

        /// <summary>
        /// Removing file explorer auto run for the given DriveLetter so that when a vhd is mounted file explorer doesn't open
        /// </summary>
        /// <param name="DriveLetter"></param>
        private void RemoveFileExplorerAutoRun(char DriveLetter)
        {
            var KeyPath = "Software\Microsoft\Windows\CurrentVersion\Policies\Explorer";
            RegistryKey AutoRunKey = Registry.CurrentUser.OpenSubKey(KeyPath, true);
            var DriveLetterValue = DriveLetter - 'A';

            if (AutoRunKey != null)
            {
                RemoveFileExplorerAutoRun(AutoRunKey, DriveLetterValue);
            }
            else // create key as it does not exist
            {
                AutoRunKey = Registry.CurrentUser.CreateSubKey(KeyPath);
                RemoveFileExplorerAutoRun(AutoRunKey, DriveLetterValue);
            }
        }

        private void RemoveFileExplorerAutoRun(RegistryKey AutoRunKey, int DriveLetterValue)
        {
            if (AutoRunKey != null)
            {
                AutoRunKey.SetValue("NoDriveTypeAutoRun", DriveLetterValue);
                AutoRunKey.Close();
            }
        }

最干净的方法似乎是通过您的前台 window 和 return 从您的 window 过程中捕获 RegisterWindowMessage("QueryCancelAutoPlay") 消息。

https://docs.microsoft.com/en-us/windows/desktop/shell/autoplay-reg

编辑: 如果前景 window 不是您的应用程序 window,那么我建议不要编辑注册表,因为它是全局状态,而您只需要临时绕过自动运行。

除了 windows 钩子,在其他答案中提到,我建议注册你对 IQueryCancelAutoPlay interface in running object table

的实现

另一种方法是使用注册表。

请参考“Using the Registry to Disable AutoRun" "How to disable the Autorun functionality in Windows

备注

The NoDriveAutoRun and NoDriveTypeAutoRun values should only be modified by system administrators to change the value for the entire system for testing or administrative purposes. Applications should not modify these values, as there is no way to reliably restore them to their original values.

第三种方式是基于@Alexander Gutenev 指出注册一个 "QueryCancelAutoPlay" window 消息然后从你的应用程序安装一个全局 hook 来监控这个消息。

备注

You should use global hooks only for debugging purposes; otherwise, you should avoid them. Global hooks hurt system performance and cause conflicts with other applications that implement the same type of global hook.

Hooks tend to slow down the system because they increase the amount of processing the system must perform for each message. You should install a hook only when necessary, and remove it as soon as possible.