public void startListen() { try { this.listener.Start(); } catch (Exception ex) { CJMonitor.writeLog("Error101-TCP-SERVER", ex.Message); } }
private void fileSystemWatcher_EventHandle_Create(object sender, FileSystemEventArgs e) //文件增删改时被调用的处理方法 { bool isMonitor = false; string relaFilePath = ""; string fileTypeTmp = e.Name.Substring(e.Name.LastIndexOf(".")).ToLower(); foreach (string str in Filter) { if (str == "*.*") { isMonitor = true; break; } if (str.Substring(str.LastIndexOf(".")) == fileTypeTmp) { isMonitor = true; break; } } if (isMonitor) { try { this.fileWatcher.EnableRaisingEvents = false; relaFilePath = e.FullPath.ToString().Remove(0, DirPath.Length); int dirIndex = relaFilePath.LastIndexOf("\\"); if (dirIndex > 0) { string dirStrTmp = relaFilePath.Remove(dirIndex); Directory.CreateDirectory(BackupDir + dirStrTmp); } File.Move(e.FullPath, BackupDir + relaFilePath + "-" + DateTime.Now.ToFileTime().ToString()); string sql = "insert into filelog(filename,fullpath,operate) VALUES('" + e.Name + "','" + e.FullPath + "','添加文件')"; cmd = new OleDbCommand(sql, connection); connection.Open(); cmd.ExecuteNonQuery(); cmd.Dispose(); connection.Close(); } catch (Exception ex) { this.Status = new KiVs(-2, ex.Message); CJMonitor.writeLog("Error010-Create", ex.Message); } if (this.FileChangeEvent != null) { this.FileCreateEvent(this, e); } this.fileWatcher.EnableRaisingEvents = true; CJMonitor.writeLog("Create", e.FullPath); } }
private void fileSystemWatcher_Renamed(object sender, RenamedEventArgs e) //文件重命名时被调用的处理方法 { bool isMonitor = false; string relaFilePath = ""; string fileTypeTmp = e.OldName.Substring(e.OldName.LastIndexOf(".")).ToLower(); foreach (string str in Filter) { if (str == "*.*") { isMonitor = true; break; } if (str.Substring(str.LastIndexOf(".")) == fileTypeTmp) { isMonitor = true; break; } } if (isMonitor) { try { this.fileWatcher.EnableRaisingEvents = false; relaFilePath = e.OldFullPath.ToString().Remove(0, DirPath.Length); File.Delete(e.FullPath); File.Copy(StandDir + relaFilePath, e.OldFullPath); string sql = "insert into filelog(filename,fullpath,operate,remark) VALUES('" + e.OldName + "','" + e.OldFullPath + "','更改文件名','更改为:" + e.Name + "')"; cmd = new OleDbCommand(sql, connection); connection.Open(); cmd.ExecuteNonQuery(); cmd.Dispose(); connection.Close(); } catch (Exception ex) { this.Status = new KiVs(-2, ex.Message); CJMonitor.writeLog("Error031-Renamed", ex.Message); } if (this.FileChangeEvent != null) { this.FileRenameEvent(this, e); } this.fileWatcher.EnableRaisingEvents = true; CJMonitor.writeLog("Renamed", e.OldFullPath); } }
private void getConf() { xmlDoc = new XmlDocument(); try { xmlDoc.Load(confFile); rootNode = xmlDoc.DocumentElement; } catch (Exception) { this.Status = new KiVs(-1, "配置文件加载失败"); CJMonitor.writeLog("error", "配置文件加载失败"); } if (Status.Key < 0) { return; } try { pipeName = rootNode.SelectSingleNode("Server").SelectSingleNode("PipeName").Value; } catch (Exception) { pipeName = "cjmonitorPipe"; } XmlNodeList folderNodes; folderNodes = rootNode.SelectNodes("Folder"); foreach (XmlNode node in folderNodes) { string path = node.Attributes["path"].Value; string filter = node.Attributes["filter"].Value; string[] fs = filter.Split('|'); string standDir = node.Attributes["standDir"].Value; string backupDir = node.Attributes["backDir"].Value; this.AddFolder(path, fs.ToList <string>(), standDir, backupDir); } }
private void RevMsg() { while (true) { try { TcpClient client = listener.AcceptTcpClient(); NetworkStream ns = client.GetStream(); int readLen = client.Available; if (readLen > 0) { Thread netsThread = new Thread(() => { operateStream(ns); }); netsThread.Start(); } } catch (Exception ex) { CJMonitor.writeLog("监听信息", ex.Message); } } }
protected override void OnStop() { CJMonitor.writeLog("Stop", "ServiceStop"); mainMonitor.stopAll(); }
protected override void OnStart(string[] args) { CJMonitor.writeLog("Start", "ServiceStart"); mainMonitor = new CJMonitor(); mainMonitor.startAll(); }
private void fileSystemWatcher_EventHandle_Change(object sender, FileSystemEventArgs e) //文件增删改时被调用的处理方法 { bool isMonitor = false; string relaFilePath = ""; string fileTypeTmp; if (e.Name.LastIndexOf(".") < 0) { fileTypeTmp = ".*"; } else { fileTypeTmp = e.Name.Substring(e.Name.LastIndexOf(".")).ToLower(); } foreach (string str in Filter) { if (str == "*.*") { isMonitor = true; break; } if (str.Substring(str.LastIndexOf(".")) == fileTypeTmp) { isMonitor = true; break; } } if (isMonitor) { try { this.fileWatcher.EnableRaisingEvents = false; relaFilePath = e.FullPath.ToString().Remove(0, DirPath.Length); int dirIndex = relaFilePath.LastIndexOf("\\"); if (dirIndex > 0) { string dirStrTmp = relaFilePath.Remove(dirIndex); Directory.CreateDirectory(BackupDir + dirStrTmp); } } catch (Exception ex) { this.Status = new KiVs(-2, ex.Message); CJMonitor.writeLog("Error001-Change", ex.Message); } if (this.FileChangeEvent != null) { this.FileChangeEvent(this, e); } //if (File.Exists(e.FullPath)) //{ // File.Delete(e.FullPath); //} try { File.Move(e.FullPath, BackupDir + relaFilePath + "-" + DateTime.Now.ToFileTime().ToString()); File.Copy(StandDir + relaFilePath, e.FullPath); string sql = "insert into filelog(filename,fullpath,operate) VALUES('" + e.Name + "','" + e.FullPath + "','内容更改')"; cmd = new OleDbCommand(sql, connection); connection.Open(); cmd.ExecuteNonQuery(); cmd.Dispose(); connection.Close(); } catch (Exception ex) { CJMonitor.writeLog("Error002-Change", ex.Message + " " + StandDir + relaFilePath + "Move to" + e.FullPath); bool isNeedOver = true; while (isNeedOver) { try { this.fileWatcher.EnableRaisingEvents = false; File.Move(e.FullPath, BackupDir + relaFilePath + "-" + DateTime.Now.ToFileTime().ToString()); File.Copy(StandDir + relaFilePath, e.FullPath); string sql = "insert into filelog(filename,fullpath,operate) VALUES('" + e.Name + "','" + e.FullPath + "','内容更改')"; cmd = new OleDbCommand(sql, connection); connection.Open(); cmd.ExecuteNonQuery(); cmd.Dispose(); connection.Close(); isNeedOver = false; } catch (Exception ex2) { Thread.Sleep(1000); CJMonitor.writeLog("Error003-Change", ex2.Message); } this.fileWatcher.EnableRaisingEvents = true; } } this.fileWatcher.EnableRaisingEvents = true; CJMonitor.writeLog("Change", e.FullPath); } }