/// <summary> /// 启动 windows服务 /// </summary> /// <param name="serviceName"></param> /// <param name="timeoutMilliseconds"></param> public bool StartService(string serviceName, int timeoutMilliseconds) { var bRet = true; ServiceController service = new ServiceController(serviceName); try { if (IsServiceControllerStart(service)) { return(true); } TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); service.Start(); service.WaitForStatus(ServiceControllerStatus.Running, timeout); } catch (Exception ex) { bRet = false; Log4Helper.Error(this.GetType(), String.Format("启动{0}服务失败,异常:{1}", serviceName, ex.Message), ex); } return(bRet); }
/// <summary> /// 文件目录整体对比 /// </summary> /// <param name="dir1">目录1</param> /// <param name="dir2">目录2</param> /// <returns></returns> public bool CompareDirs(string fileHolder1, string fileHolder2) { bool isEqual = false; try { DirectoryInfo dir1 = new DirectoryInfo(fileHolder1); DirectoryInfo dir2 = new DirectoryInfo(fileHolder2); // Take a snapshot of the file system. IEnumerable <FileInfo> fileInfolist1 = dir1.GetFiles("*.*", SearchOption.AllDirectories); IEnumerable <FileInfo> fileInfolist2 = dir2.GetFiles("*.*", SearchOption.AllDirectories); //1.对比文件名和数量是否一致 List <string> filelist1 = new List <string>(); List <string> filelist2 = new List <string>(); //To List with Relative Path. 把文件相对路径添加到list foreach (System.IO.FileInfo file in fileInfolist1) { string filepathone = file.DirectoryName.Replace(fileHolder1, "") + "\\" + file.Name; filelist1.Add(filepathone); } //To List with Relative Path. 把文件相对路径添加到list foreach (System.IO.FileInfo file in fileInfolist2) { string filepathtwo = file.DirectoryName.Replace(fileHolder2, "") + "\\" + file.Name; filelist2.Add(filepathtwo); } //check Is Same 两个文件夹下文件数量、名字完全相同,把文件列表放到listbox 控件里 bool areIdentical = filelist1.SequenceEqual(filelist2); if (!areIdentical) { string msg = string.Format("1.目录{0}和目录{1}文件名或者数量不一致!2.请检查App路径和发布机上路径是否一致(如多了一个下划线)!", fileHolder1, fileHolder2); Log4Helper.Error(this.GetType(), msg); return(false); } //2.对比hash是否一致 foreach (string file in filelist1) { string file1path = fileHolder1 + file; //目录1 string file2path = fileHolder2 + file; //目录2 string file1Hash = FileHash.GetFileSha1(file1path); string file2Hash = FileHash.GetFileSha1(file2path); if (!file1Hash.Equals(file2Hash)) { string msg = string.Format("文件{0}和文件{1}Hash值不一致!", file1path, file2path); Log4Helper.Error(this.GetType(), msg); return(false); } } isEqual = true; } catch (Exception ex) { isEqual = false; string msg = string.Format("目录{0}和目录{1}对比失败!异常:{2}", fileHolder1, fileHolder2, ex.Message); Log4Helper.Error(this.GetType(), msg); } return(isEqual); }