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

发布于 2024-09-09 01:00:00 字数 44 浏览 8 评论 0原文

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

Does anybody know a way to deactivate the autoplay function of windows using c#/.NET?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

伪心 2024-09-16 01:00:00

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

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

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

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

实施示例

1。 QueryCancelAutoPlay

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

2。注册表

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

3. IQueryCancelAutoPlay

其他一些链接:

A little summary, for all the others looking for a good way to disable/supress autoplay.
So far I've found 3 methods to disable autoplay programatically:

  1. Intercepting the QueryCancelAutoPlay message
  2. Using the Registry
  3. Implementing the COM Interface IQueryCancelAutoPlay

In the end I chose the 3rd method and used the IQueryCancelAutoPlay interface because the others had some signifcant disadvantages:

  • The first method
    (QueryCancelAutoPlay) was only able
    to suppress autoplay if the
    application window was in the foreground, cause only the foreground window receives the message
  • Configuring autoplay in the registry worked even if the application window was in the background. The downside: It required a restart of the currently running explorer.exe to take effect...so this was no solution to temporarily disable autoplay.

Examples for the implementation

1. QueryCancelAutoPlay

Note: If your application is using a dialog box you need to call SetWindowLong (signature) instead of just returning false. See here for more details)

2. Registry

Using the registry you can disables AutoRun for specified drive letters (NoDriveAutoRun) or for a class of drives (NoDriveTypeAutoRun)

3. IQueryCancelAutoPlay

Some other links:

触ぅ动初心 2024-09-16 01:00:00

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");

从此处(顶部的专家交流链接)。该网站上还有其他帮助,其中包含更多示例,这些示例可能比上面的更全面一些。不过上面确实解决了问题。

RegisterWindowMessage is a Win32 API call. So you will need to use PInvoke to make it work..

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");

From here (The Experts-Exchange link at the top). There is additional help on that site with some more examples that may be a little more comprehensive than the above. The above does however solve the problem.

终止放荡 2024-09-16 01:00:00

一些可能有用的其他链接:

Some additional links that might be helpful:

掌心的温暖 2024-09-16 01:00:00

试试这个代码对我有用:)有关更多信息,请查看此参考链接: 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

Try this code work for me :) For more info check out this reference link : 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文