using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.Text; using System.IO; using System.Threading; using System.Net; using System.Net.NetworkInformation; using System.Net.Sockets; using Microsoft.Win32; namespace SampleService { public partial class SampleWinService : ServiceBase { #region Variable and Constants private Timer service_timer; NetworkInterface[] fNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); NetworkInterface adapter; long start_received_bytes; long start_sent_bytes; long end_received_bytes; long end_sent_bytes; string start_time, end_time; private DateTime _startTime = DateTime.MinValue; private bool _isNetworkOnline; bool flag_c; string empty_line = "==============================================================="; string path_file = @"C:\\Documents and Settings\\users\\Desktop\\Test Services.txt"; #endregion // How do I input the file path, as it differs from system to system public SampleWinService() { InitializeComponent(); } protected void timerTicker(object sender) { if (IsConnectedToInternet() == true && flag_c == false) { mark_values("initial"); flag_c = true; } else if (IsConnectedToInternet() == true && flag_c == true) { mark_values("final"); } else if (IsConnectedToInternet() == false && flag_c == true) { writetoFile(); flag_c = false; } } protected void mark_values(string todo) { if (todo == "initial") { _startTime = DateTime.Now; start_received_bytes = adapter.GetIPv4Statistics().BytesReceived; start_sent_bytes = adapter.GetIPv4Statistics().BytesSent; } else { end_received_bytes = adapter.GetIPv4Statistics().BytesReceived; end_sent_bytes = adapter.GetIPv4Statistics().BytesSent; } } protected long cal_bytes(long value) { long temp_val; temp_val = (value / 1048576 * 100000) / 100000; return temp_val; } protected void writetoFile() { var timeSinceStartTime = DateTime.Now - _startTime; timeSinceStartTime = new TimeSpan(timeSinceStartTime.Hours, timeSinceStartTime.Minutes, timeSinceStartTime.Seconds); if (System.IO.File.Exists(path_file)) { System.IO.File.AppendAllText(path_file, empty_line + Environment.NewLine + "Connection established: " + start_time + " Ended: " + end_time + Environment.NewLine + "Total Time: " + timeSinceStartTime + Environment.NewLine + "Received Start: " + start_received_bytes.ToString() + " (in MB): " + cal_bytes(start_received_bytes).ToString() + Environment.NewLine + "Sent Start: " + start_sent_bytes.ToString() + " (in MB): " + cal_bytes(start_sent_bytes).ToString() + Environment.NewLine + "Received End: " + end_received_bytes.ToString() + " (in MB): " + cal_bytes(end_received_bytes).ToString() + Environment.NewLine + "Sent End: " + end_sent_bytes.ToString() + " (in MB): " + cal_bytes(end_sent_bytes).ToString() + Environment.NewLine); } } #region Internet Connection Status [System.Runtime.InteropServices.DllImport("wininet.dll")] private extern static bool InternetGetConnectedState(out int Description, int ReservedValue); public static bool IsConnectedToInternet() { int Desc; return InternetGetConnectedState(out Desc, 0); } protected void NetworkChange_NetworkAvailabilityChanged(object sender, System.Net.NetworkInformation.NetworkAvailabilityEventArgs e) { _isNetworkOnline = e.IsAvailable; } #endregion protected override void OnStart(string[] args) { try { // how do I assign the adpter dynamicaly or atleast during the installation of the services adapter = fNetworkInterfaces[0]; service_timer = new Timer(new TimerCallback(timerTicker)); service_timer.Change(29999, 30000); } catch (Exception ex) { Console.WriteLine(" Start Error: {0}", ex.Message.ToString()); } } protected override void OnStop() { } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)