public void loadClick(DateTime time) { // 储存到文件,比如20130203.xml string fileName = this.logFile; string today = time.ToString("yyyyMMdd"); SerializableDictionary fileDatas = new SerializableDictionary(); using (FileStream stream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Read)) { stream.Lock(0, stream.Length); XmlSerializer serializer = new XmlSerializer(typeof(SerializableDictionary)); if (stream.Length != 0) { fileDatas = (SerializableDictionary)serializer.Deserialize(stream); if (!fileDatas.ContainsKey(today)) { fileDatas[today] = new MouseState(); } } else { fileDatas = new SerializableDictionary(); fileDatas[today] = new MouseState(); } this.leftClickCount = fileDatas[today].leftClickCount; this.rightClickCount = fileDatas[today].rightClickCount; this.middleClickCount = fileDatas[today].middleClickCount; } }
public MainForm() { InitializeComponent(); // 创建文件夹 System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\mouseMonitor\\ string logFolder = System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\mouseMonitor"; if (!System.IO.Directory.Exists(logFolder)) { System.IO.Directory.CreateDirectory(logFolder); } // 启动监控服务 mouseMonitor = new MouseMonitor(); state = new MouseState(); this.formStartHook(); // 启动另外一个记录线程,每分钟记录一次 this.saveThread = new Thread(new ThreadStart(BeginSave)); this.saveThread.IsBackground = true; this.saveThread.Start(); // 需要读取今天的点击次数 this.state.loadClick(DateTime.Today); // 获取最新版本信息 string url = "http://mousemonitor.funaio.com/upgrade/info"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream receiveStream = response.GetResponseStream(); Encoding encode = System.Text.Encoding.GetEncoding("utf-8"); byte[] data = new byte[100]; receiveStream.Read(data, 0, 100); receiveStream.Close(); string newestVersion = Encoding.UTF8.GetString(data); newestVersion = newestVersion.Trim('\0'); if (newestVersion != CurrentVersion) { string message = "请到 http://mousemonitor.funaio.com/ 下载最新版本" + newestVersion; MessageBox.Show(message); } }
// 将action保存到文件中 public void saveAction(DateTime time) { int leftClickCount; int rightClickCount; int middleClickCount; lock (this) { leftClickCount = this.leftClickCount; rightClickCount = this.rightClickCount; middleClickCount = this.middleClickCount; } // 储存到文件,比如20130203.xml string fileName = this.logFile; string today = time.ToString("yyyyMMdd"); SerializableDictionary fileDatas = new SerializableDictionary(); MouseState fileData = new MouseState(); using (FileStream stream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { stream.Lock(0, stream.Length); XmlSerializer serializer = new XmlSerializer(typeof(SerializableDictionary)); if (stream.Length != 0) { fileDatas = (SerializableDictionary)serializer.Deserialize(stream); if (!fileDatas.ContainsKey(today)) { fileDatas[today] = new MouseState(); } } else { fileDatas = new SerializableDictionary(); fileDatas[today] = new MouseState(); } fileDatas[today].leftClickCount = leftClickCount; fileDatas[today].rightClickCount = rightClickCount; fileDatas[today].middleClickCount = middleClickCount; stream.Position = 0; XmlWriter writer = new XmlTextWriter(stream, Encoding.UTF8); serializer.Serialize(writer, fileDatas); } }