c# - UWP get data usage -
i love programming uwp , start new program c# , uwp. want network usage each connection in target system. read document in msdn , write code app:
public async static task<list<connectionreport>> getnetworkusageasync(string connectionname, datetimeoffset starttime, datetimeoffset endtime, datausagegranularity granularity) { var granularitytimespan = granularitytotimespan(granularity); try { var connectionprofile = getconnectionprofile(connectionname); var networkusages = await connectionprofile.getnetworkusageasync(starttime, endtime, granularity, new networkusagestates()); list<connectionreport> listnetworkusage = new list<connectionreport>(); foreach (var networkusage in networkusages) { var connectionreport = new connectionreport() { date = starttime.date.tostring(dateformat), download = convertbytestomb(networkusage.bytesreceived), upload = convertbytestomb(networkusage.bytessent), }; listnetworkusage.add(connectionreport); starttime = starttime.add(granularitytimespan); } return listnetworkusage; } catch (exception ex) { // } } private static connectionprofile getconnectionprofile(string connectionname) { var connectionprofiles = networkinformation.getconnectionprofiles(); foreach (var connection in connectionprofiles) { if (connection.profilename == connectionname) return connection; } } private static double convertbytestomb(double bytes) => math.round(bytes / 1024 / 1024, 3); // returns amount of time between each period of network usage given granularity private static timespan granularitytotimespan(datausagegranularity granularity) { switch (granularity) { case datausagegranularity.perminute: return new timespan(0, 1, 0); case datausagegranularity.perhour: return new timespan(1, 0, 0); case datausagegranularity.perday: return new timespan(1, 0, 0, 0); default: return new timespan(0); } }
but don't know code have problems or windows 10 build 15063 have problems because run app on 2 different system , same value wifi , ethernet connections. network usage code not special connection, it's give me internet usage choose 2 different wifi.
possible me windows problem or code have problems?
thanks.
Comments
Post a Comment