Die*_*eco 0 securityexception performancecounter
这是代码:
    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,我该怎么办?
您的应用程序的进程没有适当的权限级别.这就是安全例外告诉你的.
简单的解决方法是在应用程序启动时请求该权限.您可以通过修改应用程序的清单来实现此目的,以便将requestedExecutionLevel其设置为requireAdministrator.
添加到应用程序清单中的完整部分将如下所示:
<!-- 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提升的单独进程,并在用户明确请求时运行性能计数器.
| 归档时间: | 
 | 
| 查看次数: | 1596 次 | 
| 最近记录: |