有人知道使用 c#/.NET 停用 Windows 自动播放功能的方法吗?
问问题
7029 次
4 回答
11
一个小总结,对于所有其他寻找禁用/抑制自动播放的好方法的人。到目前为止,我已经找到了 3 种以编程方式禁用自动播放的方法:
- 拦截QueryCancelAutoPlay 消息
- 使用注册表
- 实现 COM 接口IQueryCancelAutoPlay
最后我选择了第三种方法并使用了 IQueryCancelAutoPlay 接口,因为其他方法有一些明显的缺点:
- 第一种方法(QueryCancelAutoPlay)只有在应用程序窗口在前台时才能禁止自动播放,因为只有前台窗口接收到消息
- 即使应用程序窗口在后台,在注册表中配置自动播放也有效。缺点:它需要重新启动当前正在运行的explorer.exe才能生效……所以这不是暂时禁用自动播放的解决方案。
实施示例
1.查询取消自动播放
- 以编程方式抑制自动运行(MSDN 文章)
- CodeProject:防止 CD 自动播放
- 从 C# 取消自动播放
注意:如果您的应用程序正在使用对话框,您需要调用SetWindowLong ( signature ) 而不是仅仅返回 false。请参阅此处了解更多详细信息)
2.注册表
使用注册表,您可以为指定的驱动器号 (NoDriveAutoRun) 或一类驱动器 ( NoDriveTypeAutoRun )禁用自动运行
3. IQueryCancelAutoPlay
- MSDN 上IQueryCancelAutoPlay接口的参考
- IQueryCancelAutoPlay 只调用一次?(示例实现,另请阅读评论)
- AutoPlayController(另一种实现,未测试)
其他一些链接:
- 启用和禁用自动运行(MSDN 文章)
- Windows XP 中的自动播放:自动检测系统上的新设备并对其做出反应(关于自动播放的旧但内容广泛的文章)
于 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
一些可能有用的附加链接:
- 防止 CD 自动播放显示了一些示例 vb.net 代码,显示了 CodeProject 上“QueryCancelAutoPlay”的用法。
- 在 MSDN 上启用和禁用自动运行。
于 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 回答