///向日志文件中写入日志 public static void LogWrite(string logText) { CreateLogFile(); lock (lockTemp) { string filePath = @"D:\fengdong5\LogFile.log"; StreamWriter sw = new StreamWriter(filePath, true, System.Text.Encoding.UTF8); try { sw.WriteLine("/**************************************/"); sw.WriteLine("日期:" + System.DateTime.Now.ToString()); sw.WriteLine(logText); sw.WriteLine("/**************************************/"); sw.WriteLine(); sw.WriteLine(); sw.Flush(); } catch (Exception ex) { LogHelper.LogWrite(ex.Message); } finally { sw.Dispose(); sw.Close(); } } }
public static void LogWrite(Exception ex) { CreateLogFile(); lock (lockTemp1) { string filePath = @"D:\fengdong5\LogFile.log"; StreamWriter sw = new StreamWriter(filePath, true, System.Text.Encoding.UTF8); try { sw.WriteLine("/**************************************/"); sw.WriteLine("日期:" + System.DateTime.Now.ToString()); sw.WriteLine("错误源:" + ex.Source); sw.WriteLine("错误信息:" + ex.Message); sw.WriteLine("当前实例的运行时类型:" + ex.GetType()); sw.WriteLine("引发异常的方法名:" + ex.TargetSite); sw.WriteLine("/**************************************/"); sw.WriteLine(); sw.WriteLine(); sw.Flush(); } catch (Exception e) { LogHelper.LogWrite(e); } finally { sw.Close(); sw.Dispose(); } } }
/// <summary> /// 删除Upload文件夹中的带有small_的缩略图片 /// </summary> /// <param name="dirpath"></param> public void FindFileAndDelete(string dirpath) { DirectoryInfo Dir = new DirectoryInfo(dirpath); try { foreach (DirectoryInfo d in Dir.GetDirectories()) { FindFileAndDelete(Dir + @"\" + d.ToString()); } foreach (FileInfo f in Dir.GetFiles("*.png")) { if (f.Name.Contains("small_")) { f.Delete(); } } } catch (Exception e) { //MessageBox.Show(e.Message); //throw new Exception LogHelper.LogWrite(e); } }
///创建文件夹 public static void CreateLogFile() { string filePath = @"D:\fengdong5\"; if (!Directory.Exists(filePath)) { try { Directory.CreateDirectory(filePath); } catch (Exception ex) { LogHelper.LogWrite(ex.Message); } } if (!File.Exists(filePath + "LogFile.log")) { try { FileStream file = File.Create(filePath + "LogFile.log"); file.Dispose(); file.Close(); } catch (Exception ex) { LogHelper.LogWrite(ex.Message); } } }
/// <summary> /// 删除无效记录文件 /// </summary> /// <param name="pcTargetFolder">指定路径</param> private void DeleteInvalidFile(string pcTargetFolder) { if (!Directory.Exists(pcTargetFolder)) { return; } try { DirectoryInfo Dir = new DirectoryInfo(pcTargetFolder); if (Dir.GetDirectories().Length > 0 && Dir.GetFiles().Length == 0) { foreach (DirectoryInfo dI in Dir.GetDirectories()) { DeleteInvalidFile(Dir + dI.ToString() + @"\"); } } else { string[] strDir = Directory.GetFiles(Dir.FullName, "*.xml"); if (strDir.Length == 0) { Directory.Delete(Dir.FullName, true); } } } catch (Exception ex) { LogHelper.LogWrite(ex); } }
/// <summary> /// 判断是否下载设备台账信息成功 /// </summary> /// <param name="iar"></param> private void IsDownEquipInfoComplete(IAsyncResult iar) { //string wcfdownloadpath = @"D:\download\"; byte[] bytes; try { AsyncResult ar = (AsyncResult)iar; myEquipInfo = (AsycEquipInfo)ar.AsyncDelegate; bytes = myEquipInfo.EndInvoke(iar); if (!Directory.Exists(wcfdownloadpath)) { Directory.CreateDirectory(wcfdownloadpath); FileStream wcfFile = new FileStream(wcfdownloadpath + "EquipInfo.xml", FileMode.Create); wcfFile.Write(bytes, 0, bytes.Length); wcfFile.Flush(); wcfFile.Close(); wcfFile.Dispose(); } else { if (File.Exists(wcfdownloadpath + "EquipInfo.xml")) { File.Delete(wcfdownloadpath + "EquipInfo.xml"); } FileStream wcfFile = null; try { wcfFile = new FileStream(wcfdownloadpath + "EquipInfo.xml", FileMode.Create); wcfFile.Write(bytes, 0, bytes.Length); wcfFile.Flush(); } catch (Exception ex) { LogHelper.LogWrite(ex); } finally { wcfFile.Close(); wcfFile.Dispose(); } } try { portableDevices[0].CopyFolderToPad(lastObjDown, wcfdownloadpath + "EquipInfo.xml"); } catch (Exception ex) { LogHelper.LogWrite(ex); } n = n + 1; Thread.Sleep(100); //MessageBox.Show("下载设备台账信息XML成功!"); } catch (Exception ex) { LogHelper.LogWrite(ex); } }
protected override void OnStart(string[] args) { try { MtpThread = new Thread(LoopDetection); MtpThread.Start(); } catch (Exception ex) { LogHelper.LogWrite(ex.Message); } }
/// <summary> /// 下载应急计划回调函数 /// </summary> /// <param name="iar"></param> private void IsEmergencyPlanCom(IAsyncResult iar) { byte[] bytes; //string wcfdownloadpath = @"D:\download\"; try { AsyncResult ar = (AsyncResult)iar; myEmergencyPlan = (AsycEmergencyPlan)ar.AsyncDelegate; bytes = myEmergencyPlan.EndInvoke(iar); if (!Directory.Exists(wcfdownloadpath)) { Directory.CreateDirectory(wcfdownloadpath); } if (File.Exists(wcfdownloadpath + "EmergencyPlan.xml")) { File.Delete(wcfdownloadpath + "EmergencyPlan.xml"); } FileStream wcfFile = null; try { wcfFile = new FileStream(wcfdownloadpath + "EmergencyPlan.xml", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); wcfFile.Write(bytes, 0, bytes.Length); wcfFile.Flush(); } catch (Exception ex) { LogHelper.LogWrite(ex); } finally { wcfFile.Close(); wcfFile.Dispose(); } Thread.Sleep(100); } catch (Exception ex) { LogHelper.LogWrite(ex); } try { portableDevices[0].CopyFolderToPad(lastObjDown, wcfdownloadpath + "EmergencyPlan.xml"); } catch (Exception ex) { LogHelper.LogWrite(ex); } n = n + 1; Thread.Sleep(100); iar.AsyncWaitHandle.Close(); }
/// <summary> /// 异步方式下载备份备件信息回调函数 /// </summary> /// <param name="iar"></param> private void IsDownBackupEquipCom(IAsyncResult iar) { byte[] bytes; //string wcfdownloadpath = @"D:\download\"; try { AsyncResult ar = (AsyncResult)iar; myBackupEquip = (AsycBackupEquip)ar.AsyncDelegate; bytes = myBackupEquip.EndInvoke(iar); if (!Directory.Exists(wcfdownloadpath)) { Directory.CreateDirectory(wcfdownloadpath); using (FileStream wcfFile = new FileStream(wcfdownloadpath + "BackupEquip.xml", FileMode.Create)) { wcfFile.Write(bytes, 0, bytes.Length); wcfFile.Flush(); } } else { if (File.Exists(wcfdownloadpath + "BackupEquip.xml")) { File.Delete(wcfdownloadpath + "BackupEquip.xml"); } using (FileStream wcfFile = new FileStream(wcfdownloadpath + "BackupEquip.xml", FileMode.Create)) { wcfFile.Write(bytes, 0, bytes.Length); wcfFile.Flush(); } } Thread.Sleep(100); } catch (Exception ex) { LogHelper.LogWrite(ex); } try { portableDevices[0].CopyFolderToPad(lastObjDown, wcfdownloadpath + "BackupEquip.xml"); } catch (Exception ex) { LogHelper.LogWrite(ex); //continue; } n = n + 1; Thread.Sleep(100); iar.AsyncWaitHandle.Close(); }
protected override void OnStop() { try { if (MtpThread != null || MtpThread.IsAlive) { MtpThread.Abort(); } } catch (Exception ex) { LogHelper.LogWrite("结束循环线程失败:" + ex.Message); } LogHelper.LogWrite("MTP服务结束!" + DateTime.Now.ToString()); }
/// <summary> /// 添加节点属性方法 /// </summary> /// <param name="node">要添加的节点</param> /// <param name="attributeName">属性名称</param> /// <param name="value">属性值</param> /// <returns></returns> private XmlAttribute CreateAttribute(XmlNode node, string attributeName, string value) { try { XmlDocument doc = node.OwnerDocument; XmlAttribute attr = null; attr = doc.CreateAttribute(attributeName); attr.Value = value; node.Attributes.SetNamedItem(attr); return(attr); } catch (Exception ex) { LogHelper.LogWrite(ex); return(null); } }
/// <summary> /// 异步回调方法判断是否上传Upload压缩文件成功 /// </summary> /// <param name="iar"></param> private void isUploadComplete(IAsyncResult iar) { try { AsyncResult ar = (AsyncResult)iar; myUploadFile = (AsycUploadFile)ar.AsyncDelegate; if (myUploadFile.EndInvoke(iar)) { fsUpload.Close(); fsUpload.Dispose(); ///上传成功后记录在XML中 UpdateHisRecord(); //portableDevices[0].CopyFolderToPad(lastObjDown, wcfdownloadpath + @"\EquipInfo.xml"); n = n + 1; //删除已上传文件夹uploadRefesh DeleteDirectory(DirToZip); } } catch (Exception ex) { LogHelper.LogWrite(ex); } iar.AsyncWaitHandle.Close(); }
/// <summary> /// 压缩单个文件 /// </summary> /// <param name="FileToZip">被压缩的文件名称(包含文件路径)</param> /// <param name="ZipedFile">压缩后的文件名称(包含文件路径)</param> /// <param name="CompressionLevel">压缩率0(无压缩)-9(压缩率最高)</param> /// <param name="BlockSize">缓存大小</param> public void ZipFile(string FileToZip, string ZipedFile, int CompressionLevel) { //如果文件没有找到,则报错 if (!System.IO.File.Exists(FileToZip)) { throw new System.IO.FileNotFoundException("文件:" + FileToZip + "没有找到!"); } if (ZipedFile == string.Empty) { ZipedFile = Path.GetFileNameWithoutExtension(FileToZip) + ".zip"; } if (Path.GetExtension(ZipedFile) != ".zip") { ZipedFile = ZipedFile + ".zip"; } ////如果指定位置目录不存在,创建该目录 //string zipedDir = ZipedFile.Substring(0, ZipedFile.LastIndexOf("/")); //if (!Directory.Exists(zipedDir)) // Directory.CreateDirectory(zipedDir); //被压缩文件名称 string filename = FileToZip.Substring(FileToZip.LastIndexOf("//") + 1); System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read); System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile); ZipOutputStream ZipStream = new ZipOutputStream(ZipFile); ZipEntry ZipEntry = new ZipEntry(filename); ZipStream.PutNextEntry(ZipEntry); ZipStream.SetLevel(CompressionLevel); byte[] buffer = new byte[2048]; System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length); ZipStream.Write(buffer, 0, size); try { while (size < StreamToZip.Length) { int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length); ZipStream.Write(buffer, 0, sizeRead); size += sizeRead; } } catch (System.Exception ex) { LogHelper.LogWrite(ex); throw ex; } finally { ZipStream.Finish(); ZipStream.Close(); StreamToZip.Close(); } }
/// <summary> /// 异步方式进行数据的更新 /// </summary> private void StartUpdateDate() { ///创建同步历史记录 createUpdateRecordXml(); /// <summary> /// 新建WCF服务实例 /// </summary> try { wcfclient = new DeskTopServiceClient(); } catch (Exception ex) { LogHelper.LogWrite(ex.Message); } ///下载维护保养计划 try { byte[] bytes = wcfclient.GetPlanList(); if (!Directory.Exists(wcfdownloadpath)) { Directory.CreateDirectory(wcfdownloadpath); using (FileStream wcfFile = new FileStream(wcfdownloadpath + "planList.xml", FileMode.Create)) { wcfFile.Write(bytes, 0, bytes.Length); wcfFile.Flush(); } } else { //File.Delete(wcfdownloadpath + "planList.xml"); using (FileStream wcfFile = new FileStream(wcfdownloadpath + "planList.xml", FileMode.Create)) { wcfFile.Write(bytes, 0, bytes.Length); wcfFile.Flush(); } } Thread.Sleep(100); try { portableDevices[0].CopyFolderToPad(lastObjDown, wcfdownloadpath + "planList.xml"); } catch (Exception ex) { LogHelper.LogWrite(ex); } n = n + 1; Thread.Sleep(100); //MessageBox.Show("下载维护保养计划成功!"); } catch (Exception ex) { LogHelper.LogWrite(ex); } ///下载应急预案 //int a=int.Parse(DateTime.now) try { byte[] bytes = wcfclient.EmergencyDetail(DateTime.Now.ToString("yyyy"), 505); if (!Directory.Exists(wcfdownloadpath)) { Directory.CreateDirectory(wcfdownloadpath); using (FileStream wcfFile = new FileStream(wcfdownloadpath + "应急预案.pdf", FileMode.Create)) { wcfFile.Write(bytes, 0, bytes.Length); wcfFile.Flush(); } } else { //File.Delete(wcfdownloadpath + "planList.xml"); using (FileStream wcfFile = new FileStream(wcfdownloadpath + "应急预案.pdf", FileMode.Create)) { wcfFile.Write(bytes, 0, bytes.Length); wcfFile.Flush(); } } Thread.Sleep(100); try { portableDevices[0].CopyFolderToPad(lastObjDown, wcfdownloadpath + "应急预案.pdf"); } catch (Exception ex) { LogHelper.LogWrite(ex); } n = n + 1; Thread.Sleep(100); } catch (Exception ex) { LogHelper.LogWrite(ex); } ///异步方式下载备份备件信息 try { myBackupEquip = new AsycBackupEquip(wcfclient.GetProduct); myBackupEquip.BeginInvoke(new AsyncCallback(IsDownBackupEquipCom), null); } catch (Exception ex) { LogHelper.LogWrite(ex); } //异步方式下载应急计划 try { string currentYear = DateTime.Now.ToString("yyyy"); int departMentName = 505; myEmergencyPlan = new AsycEmergencyPlan(wcfclient.EmergencyDetail); myEmergencyPlan.BeginInvoke(currentYear, departMentName, new AsyncCallback(IsEmergencyPlanCom), null); } catch (Exception ex) { LogHelper.LogWrite(ex); } int CompressionLevel = 9; try { myZip = new AsycMyZipZipDir(new ICharpZip().ZipDir); myZip.BeginInvoke(DirToZip, ZipedFile, CompressionLevel, new AsyncCallback(IsZipComplete), null); } catch (Exception ex) { LogHelper.LogWrite(ex); } ///异步方法下载设备台账信息 try { myEquipInfo = new AsycEquipInfo(wcfclient.GetTZList); myEquipInfo.BeginInvoke(new AsyncCallback(IsDownEquipInfoComplete), null); } catch (Exception ex) { LogHelper.LogWrite(ex); } }
///循环监测是否有Pad插入 private void LoopDetection() { //LogHelper.LogWrite("MTP服务开启!"); //创建同步历史记录XML while (true) { ///获取接入的Pad信息 portableDevices = new ObservableCollection <PortableDevice>(); if (PortableDeviceCollection.Instance == null) { try { PortableDeviceCollection.CreateInstance(AppName, AppMajorVersionNumber, AppMinorVersionNumber); PortableDeviceCollection.Instance.AutoConnectToPortableDevice = false; } catch (Exception ex) { LogHelper.LogWrite(ex.Message); } } ///添加Pad设备,一次只能接入一个Pad设备 foreach (var device in PortableDeviceCollection.Instance.Devices) { if (device.DeviceId == padOne || device.DeviceId == padTwo) { portableDevices.Add(device); } } if (portableDevices.Count >= 1)//需增加判断,是否远程WCF服务已开启?? { if ((portableDevices[0].DeviceId == padOne || portableDevices[0].DeviceId == padTwo) && isUpdateComplete == false) { foreach (var portableDevice in portableDevices) { string[] paths = padSourcePath.Split('\\'); try { portableDevice.ConnectToDevice(AppName, AppMajorVersionNumber, AppMinorVersionNumber); } catch//(Exception ex) { //LogHelper.LogWrite(ex.Message); continue; } portableDevice.ScanContent(paths); PortableDeviceContainerObject lastObj = portableDevice.GetLast(portableDevice.Content); if (lastObj.Name == "uploadRefresh") { portableDevice.ScanFolderEnumerate(lastObj.ID, lastObj); //从Pad向PC复制文件 try { portableDevice.CopyFolderToPC(lastObj, pcTargetFolder); } catch (Exception ex) { LogHelper.LogWrite(ex); } ///查找Pad端指定位置 string[] paths1 = padTargetFolder.Split('\\'); portableDevice.ScanContent(paths1); lastObjDown = portableDevice.GetLast(portableDevice.Content); //find the all files in the last folder portableDevice.ScanFolderEnumerate(lastObjDown.ID, lastObjDown); //copy folder from pc to pad //portableDevice.CopyFolderToPad(lastObjDown, pcSourceFolder); if (Directory.Exists(pcTargetFolder + "\\uploadRefresh")) { ///执行数据更新工作 StartUpdateDate(); } bool complete = true; ///判断是否更新成功 do { if (n == 6) { ///上传同步记录 if (portableDevices[0].DeviceId == padOne) { portableDevices[0].CopyFolderToPad(lastObjDown, historyRecordXmlOne); } if (portableDevices[0].DeviceId == padTwo) { portableDevices[0].CopyFolderToPad(lastObjDown, historyRecordXmlTwo); } ///上传结束标志文件 using (FileStream wcfFile = new FileStream(wcfdownloadpath + "time.xml", FileMode.Create)) { byte[] bytes = new byte[0]; wcfFile.Write(bytes, 0, bytes.Length); wcfFile.Flush(); } try { portableDevices[0].CopyFolderToPad(lastObjDown, wcfdownloadpath + "time.xml"); } catch (Exception ex) { LogHelper.LogWrite(ex.Message); } complete = false; n = 0; if (wcfclient != null) { try { wcfclient.Close(); } catch (Exception ex) { wcfclient.Abort(); LogHelper.LogWrite(ex); } } } Thread.Sleep(100); }while (complete); isUpdateComplete = true; } } } } if (portableDevices.Count == 0) { isUpdateComplete = false; } if (portableDevices != null) { portableDevices = null; } GC.Collect(); Thread.Sleep(100); } }