开发者

c# set performanceCounters enabled=true in code

I'm using this method to count my app sent bytes:

string currId = Process.GetCurrentProcess().Id.ToString();

            PerformanceCounter dataSentCounter = new PerformanceCounter();
            dataSentCounter.CategoryName=".NET CLR Networking";
            dataSe开发者_如何学CntCounter.CounterName="Bytes Sent";
            dataSentCounter.InstanceName =  "curr"+"["+currId+"]";
            dataSentCounter.ReadOnly = true;

            float sumSent = 0;
            sumSent = dataSentCounter.NextValue();

            uploadSize_Label.Content = sumSent.ToString();

It works fine with app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
    <settings>
        <performanceCounters enabled="true"/>
    </settings>
</system.net>
</configuration>

Is it possible to set performanceCounters enabled="true" without using app.config (without any configuraton file - only by aplication code)?


You should be able to use the PerformanceCountersElement.Enabled property.

        System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        NetSectionGroup netGroup = (NetSectionGroup)config.SectionGroups.Get("system.net");
        netGroup.Settings.PerformanceCounters.Enabled = true;

        config.Save(ConfigurationSaveMode.Modified);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜