我可以用C#/.NET编程禁用窗口自动播放功能吗?

我可以用C#/.NET编程禁用窗口自动播放功能吗?,c#,.net,windows,autoplay,C#,.net,Windows,Autoplay,有人知道用c#/.NET停用windows自动播放功能的方法吗 RegisterWindowMessage是一个Win32 API调用。因此,您需要使用PInvoke使其工作 (顶部的专家交换链接)。该站点上还有更多的帮助,其中包括一些可能比上述内容更全面的示例。但是,以上确实解决了问题。一些可能有用的附加链接: 举个例子 vb.net代码,显示了 CodeProject上的“QueryCancelAutoPlay” 在MSDN上 为所有其他正在寻找禁用/抑制自动播放的好方法的人提供一个小结。

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

RegisterWindowMessage是一个Win32 API调用。因此,您需要使用PInvoke使其工作


(顶部的专家交换链接)。该站点上还有更多的帮助,其中包括一些可能比上述内容更全面的示例。但是,以上确实解决了问题。

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

  • 举个例子 vb.net代码,显示了 CodeProject上的“QueryCancelAutoPlay”
  • 在MSDN上

为所有其他正在寻找禁用/抑制自动播放的好方法的人提供一个小结。 到目前为止,我发现了3种通过编程禁用自动播放的方法:

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

    • 第一种方法 (QueryCancelAutoPlay)只能 在以下情况下禁止自动播放: 应用程序窗口位于前台,原因是
    • 即使应用程序窗口位于后台,在注册表中配置autoplay也能正常工作。缺点:它需要重新启动当前运行的explorer.exe才能生效…因此这不是临时禁用自动播放的解决方案

    实施的例子 1。QueryCancelAutoPlay

    • (MSDN文章)

    注意:如果应用程序使用的是对话框,则需要调用()而不是返回false。有关更多详细信息,请参阅)

    2。注册表

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

    • (MSDN文章)
    3。IQueryCancelAutoPlay

    • MSDN上的接口
    • (示例实施,另请阅读注释)
    • (另一个实现,未测试)
    其他一些链接:
    • (MSDN文章)
    • (一篇关于自动播放的古老但内容广泛的文章)

    请尝试此代码为我工作:)有关更多信息,请查看此参考链接:


    通过Windows API,以及可能的P/Invoke。此外,查看此注册表项--请参阅-@Nate如果您想禁用整个计算机的自动播放功能,而不仅仅是一个用户,请在该链接中使用相同的注册表项,HKLM下除外,而不是HKCU@Kyle-除非您加入,否则您无法通过直接链接查看e-e中的内容。只有当你从谷歌搜索链接时,内容才会显示。@ChrisF-我不知道,谢谢,我已经更新了该站点的谷歌链接。你不能再滚动到页面底部了吗?E-E过去在你没有登录的时候是这样工作的。但至少有谷歌缓存…非常重要:这个事件只发送到当前的前台窗口!
    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");
    
    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