using AForge.Video.DirectShow; using IOTContainer.Common; using IOTContainer.Communication; using IOTContainer.Model; using IOTContainer.View; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Management; using System.Net; using System.Windows.Forms; namespace IOTContainer.Common { public class SystemManage { public delegate void TouchMeDelegate(); public static event TouchMeDelegate TouchMeEvent; public static void removeDelegate() { if (TouchMeEvent != null) { System.Delegate[] dels = TouchMeEvent.GetInvocationList(); for (int i = 0; i < dels.Length; i++) { TouchMeEvent -= dels[i] as TouchMeDelegate; } } } /// /// 关闭程序 /// /// public static void CloseProgress(string name) { //关闭信息发布 Process[] pro = Process.GetProcesses(); for (int i = 0; i < pro.Length; i++) { if (pro[i].ProcessName.ToLower().Trim() == "name") { pro[i].Kill();//结束进程 } } } /// /// 重启程序 /// public static void RestartContainer() { System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location); System.Diagnostics.Process.GetCurrentProcess().Kill(); //System.Windows.Forms.Application.Restart(); //Application.Current.Shutdown(); } /// /// 获取本地Ip /// /// public static string GetLocalIp() { foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList) { if (_IPAddress.AddressFamily.ToString() == "InterNetwork") { return _IPAddress.ToString(); } } return string.Empty; } public static string GetMac() { ManagementClass mc; string mac = string.Empty; mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if (mo["IPEnabled"].ToString() == "True") mac = mo["MacAddress"].ToString(); } while (mac.IndexOf(":") >= 0) mac = mac.Remove(mac.IndexOf(":"), 1); return mac; } /// /// 是否存在音频捕捉设备 /// /// public static bool IsSound() { var devices = SharpDX.DirectSound.DirectSoundCapture.GetDevices(); // 枚举音频捕捉设备 if (devices.Count > 0) { return true; } else { return false; } } /// /// 是否存在音频播放 /// /// public static bool IsVideo() { var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); if (videoDevices.Count > 0) { return true; } else { return false; } } public static void ShutDown() { Process MyProcess = new Process(); //设定程序名 MyProcess.StartInfo.FileName = "cmd.exe"; //关闭Shell的使用 MyProcess.StartInfo.UseShellExecute = false; //重定向标准输入 MyProcess.StartInfo.RedirectStandardInput = true; //重定向标准输出 MyProcess.StartInfo.RedirectStandardOutput = true; //重定向错误输出 MyProcess.StartInfo.RedirectStandardError = true; //设置不显示窗口 MyProcess.StartInfo.CreateNoWindow = true; string cmdtext = string.Empty; cmdtext = @"Shutdown -f -s -t 0"; //关机 Log.MyLog.WriteLogFile(cmdtext, "cmdexelog"); //执行VER命令 MyProcess.Start(); MyProcess.StandardInput.WriteLine(cmdtext); Log.MyLog.WriteLogFile("关机", "cmdexelog"); string output = ""; char[] outl = new char[2048]; string error = ""; using (System.IO.StreamReader myOutput = MyProcess.StandardOutput) { myOutput.Read(outl, 0, 2048); output = new string(outl); Log.MyLog.WriteLogFile(output, "cmdexelog"); } using (System.IO.StreamReader myError = MyProcess.StandardError) { myError.Read(outl, 0, 1024); error = new string(outl); Log.MyLog.WriteLogFile(error, "cmdexelog"); } } public static void TurnOff() { bool is64Bit; is64Bit = Environment.Is64BitOperatingSystem; int i = 0; IntPtr p = new IntPtr(i); string tpath = System.AppDomain.CurrentDomain.BaseDirectory; string OKorNO = "disable"; string systemWordLength; //检测系统是64位还是32位 if (is64Bit == true) { systemWordLength = "x64"; } else { systemWordLength = "x86"; } WinApi.ShellExecute(p, "runas", tpath + "\\devcon\\" + systemWordLength + "\\devcon.exe", OKorNO + " *HID* /c net user administrator /active:yes", "", (int)WinApi.ShowWindowCommands.SW_HIDE); WinApi.ShellExecute(p, "runas", tpath + "\\devcon\\" + systemWordLength + "\\devcon.exe", "enable HID_DEVICE_SYSTEM_MOUSE user administrator /active:yes", "", (int)WinApi.ShowWindowCommands.SW_HIDE); } public static void TurnOn() { bool is64Bit; is64Bit = Environment.Is64BitOperatingSystem; int i = 0; IntPtr p = new IntPtr(i); string tpath = System.AppDomain.CurrentDomain.BaseDirectory; string OKorNO = "enable"; string systemWordLength; //检测系统是64位还是32位 if (is64Bit == true) { systemWordLength = "x64"; } else { systemWordLength = "x86"; } WinApi.ShellExecute(p, "runas", tpath + "\\devcon\\" + systemWordLength + "\\devcon.exe", OKorNO + " *HID* /c net user administrator /active:yes", "", (int)WinApi.ShowWindowCommands.SW_HIDE); } public static void ReStart() { try { Process MyProcess = new Process(); //设定程序名 MyProcess.StartInfo.FileName = "cmd.exe"; //关闭Shell的使用 MyProcess.StartInfo.UseShellExecute = false; //重定向标准输入 MyProcess.StartInfo.RedirectStandardInput = true; //重定向标准输出 MyProcess.StartInfo.RedirectStandardOutput = true; //重定向错误输出 MyProcess.StartInfo.RedirectStandardError = true; //设置不显示窗口 MyProcess.StartInfo.CreateNoWindow = true; string cmdtext = string.Empty; cmdtext = @"Shutdown -f -r -t 0"; //重启 Log.MyLog.WriteLogFile(cmdtext, "cmdexelog"); //执行VER命令 MyProcess.Start(); MyProcess.StandardInput.WriteLine(cmdtext); Log.MyLog.WriteLogFile("重启", "cmdexelog"); string output = ""; char[] outl = new char[2048]; string error = ""; using (System.IO.StreamReader myOutput = MyProcess.StandardOutput) { myOutput.Read(outl, 0, 2048); output = new string(outl); Log.MyLog.WriteLogFile(output, "cmdexelog"); } using (System.IO.StreamReader myError = MyProcess.StandardError) { myError.Read(outl, 0, 1024); error = new string(outl); Log.MyLog.WriteLogFile(error, "cmdexelog"); } } catch (Exception ex) { Log.MyLog.WriteLogFile("ReStart:" + ex.ToString(), "cmdexelog"); } } /// /// 截屏 /// public static string PrintScreen(string imgname) { try { if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\Screen")) { Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + @"\Screen"); } string path = AppDomain.CurrentDomain.BaseDirectory + @"\Screen\" + imgname; Bitmap bit = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); Graphics g = Graphics.FromImage(bit); g.CopyFromScreen(new System.Drawing.Point(0, 0), new System.Drawing.Point(0, 0), bit.Size); try { if (File.Exists(path)) File.Delete(path); bit.Save(path); Log.MyLog.WriteLogFile("设备截图:" + path, "cmdexelog"); } catch (Exception ex) { Log.MyLog.WriteLogFile(ex.Message, "cmdexelog"); } finally { g.Dispose(); bit.Dispose(); } return path; } catch (Exception ex) { Log.MyLog.WriteLogFile("设备截图失败:" + ex.Message, "cmdexelog"); return ""; } } /// /// 设备注册设备组 /// public static void AddNewGroup(string data) { try { var jo = JObject.Parse(data); var cont = jo.Value("data"); if (cont.HasValues) { var groups = cont.Value("group").ToObject>(); var nowGroupList = new List(); var nowGroups = ComParameters.Parameters.devGroupCodes; if (!string.IsNullOrEmpty(nowGroups)) { nowGroupList = nowGroups.Split(',').ToList(); } var newGroups = groups.Except(nowGroupList); foreach (var item in newGroups) { ComParameters.Parameters.MqttClient.SubscribeGroup(item); } #region 保存到数据库 var groupstring = string.Join(",", groups); LocalStorage.InsertPipTable("infos", ConfigKey.devGroupCodes, groupstring); ComParameters.Parameters.devGroupCodes = groupstring; #endregion Log.MyLog.WriteLogFile("设备分组:[" + string.Join(",", newGroups) + "]", "cmdexelog"); } } catch (Exception ex) { Log.MyLog.WriteLogFile("设备分组失败:" + ex.Message, "cmdexelog"); } } /// /// 编辑设备信息 /// public static void MachineConfig(string data) { try { var jo = JObject.Parse(data); var cont = jo.Value("data"); if (cont.HasValues) { var model = cont.Value("device").ToObject(); if (model != null) { if (ComParameters.Parameters.machineAttrDic.Keys.Contains(model.touch)) { LocalStorage.InsertPipTable("infos", ConfigKey.devAttr, ComParameters.Parameters.machineAttrDic[model.touch]); ComParameters.Parameters.devAttr = ComParameters.Parameters.machineAttrDic[model.touch]; } LocalStorage.InsertPipTable("infos", ConfigKey.buildingName, model.building); ComParameters.Parameters.buildingName = model.building; LocalStorage.InsertPipTable("infos", ConfigKey.floorName, model.floor); ComParameters.Parameters.floorName = model.floor; LocalStorage.InsertPipTable("infos", ConfigKey.devName, model.name); ComParameters.Parameters.devName = model.name; if (ComParameters.Parameters.devType != model.machineTypeName) { ////分屏不需要字幕 //ComParameters.rwl.EnterReadLock(); //File.Delete(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Programme", "ProgrammeList.txt")); //ComParameters.rwl.ExitReadLock(); //ComParameters.srwl.EnterReadLock(); //File.Delete(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Programme", "SubtitleList.txt")); //ComParameters.srwl.ExitReadLock(); switch (model.machineTypeName) { case "导视": case "会议室": { //停止定时任务 try { TimeJobs.Jobs.StopQuerySyncMachine(); WebSocketCom.Socket.StopListen(); WebSocketCom.Socket.StopWebSocketServer(); } catch (Exception) { } if (ComParameters.Parameters.EdgeWindow == null) { ComParameters.Parameters.MainWindow.Dispatcher.Invoke(() => { var web = new EdgeWindow(); web.Show(); }); } if (ComParameters.Parameters.ScreenWindowNew != null) { ComParameters.Parameters.MainWindow.Dispatcher.Invoke(() => { ComParameters.Parameters.ScreenWindowNew.Close(); }); } break; } case "信发": { if (ComParameters.Parameters.EdgeWindow != null) { ComParameters.Parameters.MainWindow.Dispatcher.Invoke(() => { ComParameters.Parameters.EdgeWindow.Close(); }); } if (ComParameters.Parameters.ScreenWindowNew == null) { ComParameters.Parameters.MainWindow.Dispatcher.Invoke(() => { ComParameters.Parameters.IsPlay = true; var view = new ScreenWindowNew(0); view.Show(); }); } else { //开始定时任务 if (TimeJobs.Jobs._timer == null) { TimeJobs.Jobs.QuerySyncMachine(); } } break; } } } LocalStorage.InsertPipTable("infos", ConfigKey.devType, model.machineTypeName); ComParameters.Parameters.devType = model.machineTypeName; if (ComParameters.Parameters.ScreenWindowNew != null) { TouchMeEvent?.Invoke(); } } } } catch (Exception ex) { Log.MyLog.WriteLogFile("编辑设备信息失败:" + ex.Message, "cmdexelog"); } } /// /// 将一个文件夹下的所有文件复制到另一个文件夹 (可备份文件夹) /// /// 源文件夹全名 /// 目标文件夹全名 /// 备份文件夹全名 public static void CopyDireToDire(string sourceDire, string destDire, string backupsDire = null) { try { if (Directory.Exists(sourceDire) && Directory.Exists(destDire)) { DirectoryInfo sourceDireInfo = new DirectoryInfo(sourceDire); FileInfo[] fileInfos = sourceDireInfo.GetFiles(); foreach (FileInfo fInfo in fileInfos) { string sourceFile = fInfo.FullName; string destFile = sourceFile.Replace(sourceDire, destDire); if (backupsDire != null && File.Exists(destFile)) { Directory.CreateDirectory(backupsDire); string backFile = destFile.Replace(destDire, backupsDire); File.Copy(destFile, backFile, true); } File.Copy(sourceFile, destFile, true); } DirectoryInfo[] direInfos = sourceDireInfo.GetDirectories(); foreach (DirectoryInfo dInfo in direInfos) { string sourceDire2 = dInfo.FullName; string destDire2 = sourceDire2.Replace(sourceDire, destDire); string backupsDire2 = null; if (backupsDire != null) { backupsDire2 = sourceDire2.Replace(sourceDire, backupsDire); } Directory.CreateDirectory(destDire2); CopyDireToDire(sourceDire2, destDire2, backupsDire2); } } } catch (Exception ex) { Log.MyLog.WriteLogFile("将一个文件夹下的所有文件复制到另一个文件夹失败:" + ex.Message, "cmdexelog"); throw; } } } }