You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
390 lines
15 KiB
390 lines
15 KiB
using IOTContainer.Model;
|
|
using Microsoft.Win32;
|
|
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
|
|
namespace IOTContainer.Common
|
|
{
|
|
public class ExternalCall
|
|
{
|
|
/// <summary>
|
|
/// 运行CMD指令
|
|
/// </summary>
|
|
/// <param name="cmdtext">Cmd指令</param>
|
|
public static void RunCmdComm(string cmdtext)
|
|
{
|
|
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;
|
|
|
|
Log.MyLog.WriteLogFile(cmdtext, "CMDString");
|
|
//执行VER命令
|
|
MyProcess.Start();
|
|
MyProcess.StandardInput.WriteLine(cmdtext);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.MyLog.WriteLogFile(ex.ToString() + Environment.NewLine + "CMDString," + cmdtext, "ExternalCallError");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建开机启动项
|
|
/// </summary>
|
|
/// <param name="path"></param>
|
|
/// <returns></returns>
|
|
public static bool SetStartApp(string path)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(path))
|
|
{
|
|
MessageBox.Show("创建启动文件失败");
|
|
return false;
|
|
}
|
|
RegistryKey rKey = Registry.LocalMachine;
|
|
//开机自动运行
|
|
// RegistryKeyPermissionCheck.ReadWriteSubTree
|
|
RegistryKey autoRun = rKey.CreateSubKey(@"Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Run", RegistryKeyPermissionCheck.ReadWriteSubTree);
|
|
Log.MyLog.WriteLogFile(@"打开注册表Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Run成功", "RegistryKey");
|
|
try
|
|
{
|
|
autoRun.SetValue("Container", "\"" + path + "\"");
|
|
rKey.Close();
|
|
Log.MyLog.WriteLogFile(@"创建注册表启动项成功", "RegistryKey");
|
|
}
|
|
catch (Exception exp)
|
|
{
|
|
MessageBox.Show("创建开机自启动失败:" + exp.Message.ToString() + "请手动设置开机自启动");
|
|
Log.MyLog.WriteLogFile(exp.Message.ToString(), "RegistryKey");
|
|
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show("创建开机自启动失败:" + ex.Message.ToString() + "请手动设置开机自启动");
|
|
Log.MyLog.WriteLogFile(ex.Message.ToString(), "RegistryKey");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 打开外部exe应用程序
|
|
/// <param name="path">应用程序地址</param>
|
|
/// </summary>
|
|
public static bool StartExE(string path, bool isVisibility = false, string param = "")
|
|
{
|
|
try
|
|
{
|
|
Log.MyLog.WriteLogFile("打开的程序路径" + path, "StartPath");
|
|
if (!File.Exists(path) && !path.StartsWith("http:"))
|
|
{
|
|
path = System.Windows.Forms.Application.ExecutablePath.Replace("\\", "/").Substring(0, System.Windows.Forms.Application.ExecutablePath.Replace("\\", "/").LastIndexOf("/")) + path;
|
|
if (!File.Exists(path))
|
|
{
|
|
Log.MyLog.WriteLogFile("不存在" + path, "StartPath");
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
path = System.Windows.Forms.Application.ExecutablePath.Replace("\\", "/").Substring(0, System.Windows.Forms.Application.ExecutablePath.Replace("\\", "/").LastIndexOf("/")) + path;
|
|
}
|
|
}
|
|
|
|
Process myprocess = new Process();
|
|
ProcessStartInfo startInfo = new ProcessStartInfo(path, param);
|
|
startInfo.Verb = "runas";
|
|
startInfo.CreateNoWindow = isVisibility;
|
|
path = path.Replace("\\", "/");
|
|
startInfo.WorkingDirectory = path.Substring(0, path.LastIndexOf("/"));//设置应用程序在其所在目录
|
|
myprocess.StartInfo = startInfo;
|
|
myprocess.StartInfo.UseShellExecute = false;
|
|
myprocess.Start();
|
|
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.MyLog.WriteLogFile("打开外部程序错误" + Environment.NewLine + path + Environment.NewLine + ex.ToString(), "ExternalCallError");
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 关闭已打开外部应用程序
|
|
/// </summary>
|
|
public static bool KillExE(string Path)
|
|
{
|
|
try
|
|
{
|
|
Process[] pro = Process.GetProcesses();
|
|
for (int i = 0; i < pro.Length; i++)
|
|
{
|
|
if (pro[i].ProcessName.ToLower().Equals(Path))
|
|
{
|
|
pro[i].Kill();//结束进程
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.MyLog.WriteLogFile("杀外部程序出错" + ex.ToString() + Path, "ExternalCallError");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 开启桌面
|
|
/// </summary>
|
|
public static void StartExplorer()
|
|
{
|
|
System.Diagnostics.Process process = new System.Diagnostics.Process();
|
|
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
|
|
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
|
|
startInfo.FileName = "cmd.exe";
|
|
process.StartInfo = startInfo;
|
|
process.StartInfo.RedirectStandardInput = true;
|
|
process.StartInfo.RedirectStandardOutput = true;
|
|
process.StartInfo.UseShellExecute = false;
|
|
process.Start();
|
|
process.StandardInput.WriteLine(Environment.GetEnvironmentVariable("windir") + "\\explorer.exe");
|
|
process.StandardInput.Flush();
|
|
process.StandardInput.Close();
|
|
process.WaitForExit();
|
|
WinApi.ShellExecute(IntPtr.Zero, null, "explorer.exe", null, null, (int)WinApi.ShowWindowCommands.SW_HIDE);
|
|
}
|
|
public static void CheckStartExplorer()
|
|
{
|
|
Process[] pro = Process.GetProcesses();
|
|
var name = pro.Select(p => p.ProcessName);
|
|
if (!pro.Any(p => p.ProcessName == "explorer"))
|
|
{
|
|
StartExplorer();
|
|
}
|
|
}
|
|
public static void SetContainerStart()
|
|
{
|
|
SetStartApp(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "IOTContainer.exe"));
|
|
}
|
|
public static void DelayContainerStart(string path)
|
|
{
|
|
try
|
|
{
|
|
|
|
RegistryKey rKey = Registry.LocalMachine;
|
|
// RegistryKeyPermissionCheck.ReadWriteSubTree
|
|
RegistryKey autoRun = rKey.CreateSubKey(@"Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Run", RegistryKeyPermissionCheck.ReadWriteSubTree);
|
|
autoRun.SetValue("ContainerStart", "\"" + path + "\"");
|
|
object obj = Registry.GetValue(@"HKEY_LOCAL_MACHINE\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Run", "Container", null);
|
|
try
|
|
{
|
|
|
|
if (obj != null)
|
|
{
|
|
autoRun.DeleteValue("Container");
|
|
}
|
|
rKey.Close();
|
|
Log.MyLog.WriteLogFile(@"创建延时启动成功", "RegistryKey");
|
|
}
|
|
catch (Exception exp)
|
|
{
|
|
Log.MyLog.WriteLogFile(exp.Message.ToString(), "RegistryKey");
|
|
|
|
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.MyLog.WriteLogFile(ex.Message.ToString(), "RegistryKey");
|
|
|
|
}
|
|
}
|
|
|
|
#region cmd方法
|
|
public static void StartChrome()
|
|
{
|
|
var cmd = ConfigurationManager.AppSettings["cmdChrome"];
|
|
RunCmdComm(cmd);
|
|
}
|
|
/// <summary>
|
|
/// 关闭桌面
|
|
/// </summary>
|
|
public static void CloseExplorer()
|
|
{
|
|
string cmd = "taskkill /im explorer.exe /f";
|
|
RunCmdComm(cmd);
|
|
}
|
|
/// <summary>
|
|
/// 计划任务-进程守护
|
|
/// </summary>
|
|
/// <param name="exePath"></param>
|
|
public static void ContainerServiceLoader(string exePath)
|
|
{
|
|
string cmd = "schtasks /create /tn IOTContainerService /sc minute /RL HIGHEST /mo 2 /tr \"" + exePath + "\" -f"; //进程守护
|
|
RunCmdComm(cmd);
|
|
}
|
|
public static void ContainerServiceLoaderDelay(string exePath)
|
|
{
|
|
string cmd = "schtasks /create /tn IOTContainerService /sc minute /RL HIGHEST /mo 10 /tr \"" + exePath + "\" -f"; //进程守护
|
|
RunCmdComm(cmd);
|
|
}
|
|
/// <summary>
|
|
/// 关闭计划任务
|
|
/// </summary>
|
|
public static void CloseContainerServiceLoader()
|
|
{
|
|
string cmd = "schtasks /delete /tn IOTContainerService -f";
|
|
RunCmdComm(cmd);
|
|
}
|
|
/// <summary>
|
|
/// 计划任务-设置自动关机计划
|
|
/// </summary>
|
|
/// <param name="data"></param>
|
|
public static void SetShutDownTime(string data)
|
|
{
|
|
try
|
|
{
|
|
var jo = JObject.Parse(data);
|
|
var cont = jo.Value<JObject>("data");
|
|
var timeModel = cont.Value<JObject>("time");
|
|
var time = timeModel.Value<string>("off");
|
|
if (string.IsNullOrEmpty(time))
|
|
{
|
|
List<MachineOnOffTimeModel> itemList = timeModel.Value<JArray>("weekList").ToObject<List<MachineOnOffTimeModel>>();
|
|
if (itemList.Count == 7)
|
|
{
|
|
ExternalCall.ClearShutDownTime();
|
|
int day = 0;
|
|
foreach (var item in itemList)
|
|
{
|
|
day++;
|
|
switch (day)
|
|
{
|
|
case 1:
|
|
ExternalCall.SetWeekShutDownTime("Mon", item.off);
|
|
break;
|
|
case 2:
|
|
ExternalCall.SetWeekShutDownTime("Tue", item.off);
|
|
break;
|
|
case 3:
|
|
ExternalCall.SetWeekShutDownTime("Wed", item.off);
|
|
break;
|
|
case 4:
|
|
ExternalCall.SetWeekShutDownTime("Thu", item.off);
|
|
break;
|
|
case 5:
|
|
ExternalCall.SetWeekShutDownTime("Fri", item.off);
|
|
break;
|
|
case 6:
|
|
ExternalCall.SetWeekShutDownTime("Sat", item.off);
|
|
break;
|
|
case 7:
|
|
ExternalCall.SetWeekShutDownTime("Sun", item.off);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
else if (itemList.Count == 0)
|
|
{
|
|
ExternalCall.DeleteWeekShutDownTime();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ExternalCall.DeleteWeekShutDownTime();
|
|
ExternalCall.SetDayShutDownTime(time);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
Log.MyLog.WriteLogFile("SetShutDownTime:"+ex.ToString(), "ExternalCallError");
|
|
}
|
|
|
|
}
|
|
public static void SetDayShutDownTime(string time)
|
|
{
|
|
string cmd = "schtasks /create /tn ShutdownWindows /sc DAILY /st " + time + " /tr \"shutdown -s -f -t 0\" -f"; //关机
|
|
RunCmdComm(cmd);
|
|
}
|
|
public static void SetWeekShutDownTime(string SchtasksName, string shutDowntime)
|
|
{
|
|
string cmd = "schtasks /create /tn " + SchtasksName + "shutdownWindows /sc weekly /mo 1 /d " + SchtasksName + " /st " + shutDowntime + " /tr \"shutdown -s -f -t 0\" -f";
|
|
RunCmdComm(cmd);
|
|
}
|
|
/// <summary>
|
|
/// 计划任务-清除自动关机计划
|
|
/// </summary>
|
|
public static void ClearShutDownTime()
|
|
{
|
|
string cmd = "schtasks /delete /tn ShutdownWindows -f";
|
|
RunCmdComm(cmd);
|
|
}
|
|
public static void ClearWeekShutDownTime(string SchtasksName)
|
|
{
|
|
string cmd = "schtasks /delete /tn " + SchtasksName + "shutdownWindows -f";
|
|
RunCmdComm(cmd);
|
|
}
|
|
public static void DeleteWeekShutDownTime()
|
|
{
|
|
for (int i = 1; i < 8; i++)
|
|
{
|
|
switch (i)
|
|
{
|
|
case 1:
|
|
ExternalCall.ClearWeekShutDownTime("Mon");
|
|
break;
|
|
case 2:
|
|
ExternalCall.ClearWeekShutDownTime("Tue");
|
|
break;
|
|
case 3:
|
|
ExternalCall.ClearWeekShutDownTime("Wed");
|
|
break;
|
|
case 4:
|
|
ExternalCall.ClearWeekShutDownTime("Thu");
|
|
break;
|
|
case 5:
|
|
ExternalCall.ClearWeekShutDownTime("Fri");
|
|
break;
|
|
case 6:
|
|
ExternalCall.ClearWeekShutDownTime("Sat");
|
|
break;
|
|
case 7:
|
|
ExternalCall.ClearWeekShutDownTime("Sun");
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
}
|
|
|