性能计数器抛出SecurityException

编程入门 行业动态 更新时间:2024-10-28 02:28:47
本文介绍了性能计数器抛出SecurityException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是代码:

private static void CreateCounter() { if (PerformanceCounterCategory.Exists("DemoCategory")) PerformanceCounterCategory.Delete("DemoCategory"); CounterCreationDataCollection ccdArray = new CounterCreationDataCollection(); CounterCreationData ccd = new CounterCreationData(); ccd.CounterName = "RequestsPerSecond"; ccd.CounterType = PerformanceCounterType.NumberOfItems32; ccd.CounterHelp = "Requests per second"; ccdArray.Add(ccd); PerformanceCounterCategory.Create("DemoCategory", "Demo category", PerformanceCounterCategoryType.SingleInstance, ccdArray); Console.WriteLine("Press any key, to start use the counter"); }

很明显:

PerformanceCounterCategory.Create("DemoCategory", "Demo category", PerformanceCounterCategoryType.SingleInstance, ccdArray);

是引发异常的行.

我已经读过有关PerformanceCounterPermission的信息,我该怎么办?

I have read about PerformanceCounterPermission, what should I do exactly?

推荐答案

您的应用程序进程没有适当的特权级别.这就是安全异常告诉您的内容.

Your application's process does not have the appropriate privilege level. That's what the security exception is telling you.

简单的解决方法是在应用程序启动时请求该权限.您可以通过修改应用程序的清单以将requestedExecutionLevel设置为requireAdministrator来实现.

The simple fix is to request that permission when your application starts. You can do this by modifying your application's manifest such that the requestedExecutionLevel is set to requireAdministrator.

添加到应用程序清单中的完整部分将如下所示:

The complete section added to your application's manifest will look something like this:

<!-- Identify the application security requirements. --> <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2"> <security> <requestedPrivileges> <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> </requestedPrivileges> </security> </trustInfo>

如果您的应用程序否则不需要管理特权,则可能会有更好的选择,因为您应始终以绝对必要或必需的最低特权级别运行.您可以使用Google调查这些替代方案;它将涉及分离一个单独进程,该进程请求UAC提升并在用户明确请求时运行性能计数器.

There are potentially better alternatives if your application does not otherwise require administrative privileges, because you should always run at the lowest privilege level that is absolutely necessary or required. You can investigate these alternatives using Google; it's going to involve spinning off a separate process that requests UAC elevation and runs the performance counter when requested explicitly by the user.

更多推荐

性能计数器抛出SecurityException

本文发布于:2023-11-05 04:28:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1559901.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:抛出   计数器   性能   SecurityException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!