【问题标题】:Performance counter throws SecurityException性能计数器抛出 SecurityException
【发布时间】:2012-01-24 05:15:56
【问题描述】:

这是代码:

    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
  • 不客气。添加了一个完整的答案(现在我不再打电话了!)所以你可以接受它并关闭问题。

标签: performancecounter securityexception


【解决方案1】:

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

简单的解决方法是在您的应用程序启动时请求该权限。为此,您可以修改应用程序的清单,将 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 提升并在用户明确请求时运行性能计数器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-07
    • 2019-07-25
    • 1970-01-01
    • 2016-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多