5

有人知道使用 c#/.NET 停用 Windows 自动播放功能的方法吗?

4

4 回答 4

11

一个小总结,对于所有其他寻找禁用/抑制自动播放的好方法的人。到目前为止,我已经找到了 3 种以编程方式禁用自动播放的方法:

  1. 拦截QueryCancelAutoPlay 消息
  2. 使用注册表
  3. 实现 COM 接口IQueryCancelAutoPlay

最后我选择了第三种方法并使用了 IQueryCancelAutoPlay 接口,因为其他方法有一些明显的缺点:

  • 第一种方法(QueryCancelAutoPlay)只有在应用程序窗口在前台时才能禁止自动播放,因为只有前台窗口接收到消息
  • 即使应用程序窗口在后台,在注册表中配置自动播放也有效。缺点:它需要重新启动当前正在运行的explorer.exe才能生效……所以这不是暂时禁用自动播放的解决方案。

实施示例

1.查询取消自动播放

注意:如果您的应用程序正在使用对话框,您需要调用SetWindowLong ( signature ) 而不是仅仅返回 false。请参阅此处了解更多详细信息)

2.注册表

使用注册表,您可以为指定的驱动器号 (NoDriveAutoRun) 或一类驱动器 ( NoDriveTypeAutoRun )禁用自动运行

3. IQueryCancelAutoPlay

其他一些链接:

于 2010-09-21T14:18:37.503 回答
1

RegisterWindowMessage 是一个 Win32 API 调用。所以你需要使用 PInvoke 来使它工作..

using System.Runtime.InteropServices;

class Win32Call
{
[DllImport("user32.dll")]
   public static extern int RegisterWindowMessage(String strMessage);
}

// In your application you will call

Win32Call.RegisterWindowMessage("QueryCancelAutoPlay");

从这里(顶部的专家交流链接)。该站点上还有其他帮助,其中包含更多示例,这些示例可能比上述内容更全面。然而,上面确实解决了这个问题。

于 2010-04-28T20:13:11.767 回答
0

一些可能有用的附加链接:

于 2010-05-01T14:21:04.060 回答
0

试试这个代码为我工作:) 有关更多信息,请查看此参考链接:http ://www.pinvoke.net/default.aspx/user32.registerwindowmessage

using System.Runtime.InteropServices;

//provide a private internal message id
private UInt32 queryCancelAutoPlay = 0;

[DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
static extern uint RegisterWindowMessage(string lpString);

/* only needed if your application is using a dialog box and needs to 
* respond to a "QueryCancelAutoPlay" message, it cannot simply return TRUE or FALSE.
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
*/

protected override void WndProc(ref Message m)
{
    //calling the base first is important, otherwise the values you set later will be lost
    base.WndProc (ref m);

    //if the QueryCancelAutoPlay message id has not been registered...
    if (queryCancelAutoPlay == 0)
        queryCancelAutoPlay = RegisterWindowMessage("QueryCancelAutoPlay");

    //if the window message id equals the QueryCancelAutoPlay message id
    if ((UInt32)m.Msg == queryCancelAutoPlay)
    {
        /* only needed if your application is using a dialog box and needs to
        * respond to a "QueryCancelAutoPlay" message, it cannot simply return TRUE or FALSE.
        SetWindowLong(this.Handle, 0, 1);
        */
        m.Result = (IntPtr)1;
    }
} //WndProc
于 2016-12-28T16:16:34.187 回答