我正在使用此方法来计算我的app发送字节数:
string currId = Process.GetCurrentProcess().Id.ToString();
PerformanceCounter dataSentCounter = new PerformanceCounter();
dataSentCounter.CategoryName=".NET CLR Networking";
dataSentCounter.CounterName="Bytes Sent";
dataSentCounter.InstanceName = "curr"+"["+currId+"]";
dataSentCounter.ReadOnly = true;
float sumSent = 0;
sumSent = dataSentCounter.NextValue();
uploadSize_Label.Content = sumSent.ToString();
它适用于app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<settings>
<performanceCounters enabled="true"/>
</settings>
</system.net>
</configuration>
是否可以在不使用app.config的情况下设置performanceCounters enabled="true"
(没有任何配置文件 - 只能通过应用程序代码)?
答案 0 :(得分:3)
您应该可以使用PerformanceCountersElement.Enabled属性。
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);