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

标签 c# c++ winapi kernel32 vhd

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

https://learn.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();
            }
        }

最佳答案

最干净的方法似乎是通过前台窗口捕获 RegisterWindowMessage("QueryCancelAutoPlay") 消息并从窗口过程返回 TRUE。

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

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

除了其他答案中提到的 Windows 钩子(Hook)之外,我建议注册 IQueryCancelAutoPlay 的实现界面位于 running object table

关于c# - 停止 SetVolumeMountPoint 打开文件资源管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54030078/

相关文章:

c# - Graphics.DrawString 不绘制最后一个 SPACE 字符?

c# - 检查是否有任何 WPF DataGrid 单元格有错误

C++ 预处理器在类关键字之后和类名之前定义

c++ - 为什么 set_symmetry_difference 无法与比较器一起使用?

wpf - 用于开发文本编辑器的 VC (win32) 或 WPF

c# - 向 WinForms 添加事件?

c++ - 在 C++ 中添加到指针的位置

windows - 在 Win7 登录屏幕上运行应用程序

c++ - 哪种方式返回HHOOK消息给系统更好?

c# - .NET6最小API : Manipulating response after . MapGet