星火直播PC
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.

App.cs 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. using Common.system;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Reflection;
  7. using System.Security.Principal;
  8. using System.Threading;
  9. using System.Windows;
  10. using System.Windows.Threading;
  11. namespace XHZB.Desktop
  12. {
  13. public partial class APP : Application
  14. {
  15. #region 全局变量
  16. /// <summary>
  17. /// 是否为测试版
  18. /// </summary>
  19. public static bool isDebug = FileToolsCommon.GetConfigValue("IsDebug") != "0";
  20. /// <summary>
  21. /// 接口地址
  22. /// </summary>
  23. public static string apiUrl = isDebug ? "http://schoolapitest.xhkjedu.com" : FileToolsCommon.GetConfigValue("APIRequestAddress");
  24. /// <summary>
  25. /// 图片地址
  26. /// </summary>
  27. public static string uploadUrl = isDebug ? "http://schoolfiletest.xhkjedu.com/" : FileToolsCommon.GetConfigValue("FileRequestAddress");
  28. /// <summary>
  29. /// 展示文件
  30. /// </summary>
  31. public static string showImageUrl = isDebug ? "http://schoolfiletest.xhkjedu.com/static/" : FileToolsCommon.GetConfigValue("SchoolfileRequestAddress") + "/static/";
  32. /// <summary>
  33. /// 认证接口地址
  34. /// </summary>
  35. public static string certapiUrl = isDebug ? "http://certapitest.xhkjedu.com" : FileToolsCommon.GetConfigValue("CertapiRequestAddress");
  36. /// <summary>
  37. /// 数据存放目录
  38. /// </summary>
  39. public static string dataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\XHZB\\";
  40. /// <summary>
  41. /// 签名
  42. /// </summary>
  43. public static string Signature = "";
  44. public static string secretKey = "nanhuakaizhangjianwangni";
  45. /// <summary>
  46. /// 录屏工具
  47. /// </summary>
  48. public static FFMpeg FFmpeg = new FFMpeg();
  49. /// <summary>
  50. /// 登陆状态 false 未登录 true已登陆
  51. /// </summary>
  52. public static bool IsLoginType = false;
  53. /// <summary>
  54. /// 防止图片地址重复
  55. /// </summary>
  56. public static long num = 9999;
  57. /// <summary>
  58. /// 导入图片路径
  59. /// </summary>
  60. public static string[] Paths = new string[] { };
  61. /// <summary>
  62. /// 截图图片路径
  63. /// </summary>
  64. public static string[] JPaths = new string[999];
  65. public static string ImgPath = string.Empty;
  66. /// <summary>
  67. /// 页码IP
  68. /// </summary>
  69. public static string[] OutPut = new string[] { };
  70. public static string CameraPosition = string.Empty;
  71. /// <summary>
  72. /// 数据存放目录
  73. /// </summary>
  74. public static string DataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\XHZB\\";
  75. /// <summary>
  76. /// 是否正在
  77. /// </summary>
  78. public static bool IsUpLoad = false;
  79. #region 页面
  80. /// <summary>
  81. /// 主页面
  82. /// </summary>
  83. public static MainWindow W_MainWindow = null;
  84. #endregion
  85. #endregion
  86. [STAThread]
  87. private static void Main()
  88. {
  89. //myloading = new LoadDialog();
  90. StopSameProcess();
  91. Killffmpeg();
  92. try
  93. {
  94. WindowsIdentity identity = WindowsIdentity.GetCurrent();
  95. WindowsPrincipal principal = new WindowsPrincipal(identity);
  96. try
  97. {
  98. if (Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "temp"))//清除临时文件
  99. {
  100. Directory.Delete(AppDomain.CurrentDomain.BaseDirectory + "temp", true);
  101. }
  102. }
  103. catch (Exception)
  104. {
  105. }
  106. //如果本地文件为空,弹出弹窗 输密匙
  107. //本地文件不为空, 解析
  108. if (!File.Exists(APP.dataPath + "signature.txt"))//上次登陆时间为空 或者签名为空 或者上次登陆时间不是今天 都需要输入密匙验证 ,否则只验证本地的签名
  109. {
  110. if (principal.IsInRole(WindowsBuiltInRole.Administrator))
  111. {
  112. ////如果是管理员,则直接运行
  113. //KeyVerification wins = new KeyVerification();
  114. // 定义Application对象作为整个应用程序入口
  115. //Application app = new Application();
  116. //app.Run(wins);
  117. W_MainWindow = new MainWindow();
  118. Application app = new Application();
  119. app.Run(W_MainWindow);
  120. }
  121. else
  122. {
  123. //创建启动对象
  124. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo
  125. {
  126. UseShellExecute = true,
  127. WorkingDirectory = Environment.CurrentDirectory,
  128. FileName = Assembly.GetExecutingAssembly().Location,
  129. //设置启动动作,确保以管理员身份运行
  130. Verb = "runas"
  131. };
  132. try
  133. {
  134. System.Diagnostics.Process.Start(startInfo);
  135. }
  136. catch
  137. {
  138. return;
  139. }
  140. //退出
  141. //Current.Shutdown();
  142. Environment.Exit(0);
  143. }
  144. }
  145. else
  146. {
  147. if (principal.IsInRole(WindowsBuiltInRole.Administrator))
  148. {
  149. //ProductVerification wins = new ProductVerification();
  150. //Application app = new Application();
  151. //app.Run(wins);
  152. W_MainWindow = new MainWindow();
  153. Application app = new Application();
  154. app.Run(W_MainWindow);
  155. }
  156. else
  157. {
  158. //创建启动对象
  159. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo
  160. {
  161. UseShellExecute = true,
  162. WorkingDirectory = Environment.CurrentDirectory,
  163. FileName = Assembly.GetExecutingAssembly().Location,
  164. //设置启动动作,确保以管理员身份运行
  165. Verb = "runas"
  166. };
  167. try
  168. {
  169. System.Diagnostics.Process.Start(startInfo);
  170. }
  171. catch
  172. {
  173. return;
  174. }
  175. //退出
  176. //Current.Shutdown();
  177. Environment.Exit(0);
  178. }
  179. }
  180. }
  181. catch (Exception ex)
  182. {
  183. string ErrMessage = "【进程】(Main):进程意外关闭。 " + ex.Message;
  184. LogHelper.WriteErrLog(ErrMessage, ex);
  185. }
  186. }
  187. #region 杀死已存在的进程
  188. /// <summary>
  189. /// 结束已运行的程序
  190. /// </summary>
  191. private static void StopSameProcess()
  192. {
  193. Process current = Process.GetCurrentProcess();
  194. Process[] processList = Process.GetProcesses();
  195. foreach (Process process in processList)
  196. {
  197. if (process.ProcessName.ToUpper() == "星火直播")
  198. {
  199. if (process.Id != current.Id) //忽略当前进程
  200. {
  201. try
  202. {
  203. process.Kill(); //结束进程
  204. }
  205. catch (Exception)
  206. {
  207. }
  208. }
  209. }
  210. }
  211. }
  212. /// <summary>
  213. /// 杀死正在运行的ffmpeg
  214. /// </summary>
  215. public static void Killffmpeg()
  216. {
  217. Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
  218. foreach (Process KillProcess in KillProcessArray)
  219. {
  220. KillProcess.Kill();
  221. }
  222. }
  223. #endregion
  224. /// <summary>
  225. /// 错误处理
  226. /// </summary>
  227. /// <param name="sender"></param>
  228. /// <param name="e"></param>
  229. private void MyApp_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
  230. {
  231. LogHelper.WriteErrLog("未标示错误:" + e.Exception.Message, e.Exception);
  232. MessageWindow.Show("程序异常." + Environment.NewLine + e.Exception.Message);
  233. e.Handled = true;
  234. }
  235. #region 检测解压ffmpeg
  236. public void UnZipFFmpeg()
  237. {
  238. new Thread(o =>
  239. {
  240. if (!FileToolsCommon.IsExistDirectory(FileToolsCommon.GetFileAbsolutePath("/ffmpeg/")))
  241. {
  242. try
  243. {
  244. //根据系统解压ffmpeg
  245. if (Environment.Is64BitOperatingSystem)
  246. {
  247. //64
  248. ZipHelper.UnZip(FileToolsCommon.GetFileAbsolutePath("/ffmpegx64.zip"), FileToolsCommon.GetFileAbsolutePath());
  249. }
  250. else
  251. {
  252. //32
  253. ZipHelper.UnZip(FileToolsCommon.GetFileAbsolutePath("/ffmpegx32.zip"), FileToolsCommon.GetFileAbsolutePath());
  254. }
  255. }
  256. catch (Exception)
  257. {
  258. ZipHelper.UnZip(FileToolsCommon.GetFileAbsolutePath("/ffmpegx64.zip"), FileToolsCommon.GetFileAbsolutePath());
  259. }
  260. }
  261. }).Start();
  262. }
  263. #endregion
  264. #region 检测安装Screen Capturer Recorder
  265. /// <summary>
  266. /// 检测是否安装了Screen Capturer Recorder
  267. /// </summary>
  268. /// <returns></returns>
  269. public static bool CheckScreenCapturerRecorder()
  270. {
  271. if (FileToolsCommon.CheckSoftWartInstallState("Screen Capturer Recorder"))
  272. {
  273. //已经安装
  274. return true;
  275. }
  276. else
  277. {
  278. return false;
  279. }
  280. }
  281. /// <summary>
  282. /// 安装Screen Capturer Recorder
  283. /// </summary>
  284. /// <returns></returns>
  285. public static bool InstallScreenCapturerRecorder()
  286. {
  287. Process InstallProcess = new Process();
  288. string StandardError = "";
  289. string StandardOutput = "";
  290. try
  291. {
  292. InstallProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/SSCR/Setup Screen Capturer Recorder v0.12.10.exe"); //绝对路径
  293. InstallProcess.StartInfo.UseShellExecute = false; //不使用操作系统外壳程序启动
  294. InstallProcess.StartInfo.RedirectStandardError = true; //重定向标准错误输出
  295. InstallProcess.StartInfo.RedirectStandardOutput = true;
  296. InstallProcess.StartInfo.CreateNoWindow = true; //不显示程序窗口
  297. InstallProcess.StartInfo.RedirectStandardInput = true; //用于模拟该进程控制台的输入
  298. //外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
  299. InstallProcess.ErrorDataReceived += new DataReceivedEventHandler(InstallProcess_ErrorDataReceived);
  300. InstallProcess.Start(); //启动线程
  301. InstallProcess.WaitForExit(); //阻塞等待进程结束
  302. StandardError = InstallProcess.StandardError.ReadToEnd();
  303. StandardOutput = InstallProcess.StandardOutput.ReadToEnd();
  304. int ExitCode = InstallProcess.ExitCode;
  305. InstallProcess.Close(); //关闭进程
  306. InstallProcess.Dispose(); //释放资源
  307. if (ExitCode == 0) //非0则为安装失败 1为取消安装
  308. {
  309. return true;
  310. }
  311. else
  312. {
  313. MessageBoxResult br = MessageWindow.Show("不安装环境将无法使用微课录制系统,是否现在安装?", "安装", MessageBoxButton.OKCancel);
  314. if (br == MessageBoxResult.OK)
  315. {
  316. return InstallScreenCapturerRecorder();
  317. }
  318. else
  319. {
  320. return false;
  321. }
  322. }
  323. }
  324. catch (Exception ex)
  325. {
  326. LogHelper.WriteErrLog("【ScreenCapturerRecorder安装】(installScreenCapturerRecorder)安装失败:" + ex.Message, ex);
  327. if (string.IsNullOrWhiteSpace(StandardError))
  328. {
  329. LogHelper.WriteErrLog("【ScreenCapturerRecorder安装】(installScreenCapturerRecorder)安装失败:" + StandardError, null);
  330. }
  331. if (string.IsNullOrWhiteSpace(StandardOutput))
  332. {
  333. LogHelper.WriteErrLog("【ScreenCapturerRecorder安装】(installScreenCapturerRecorder)安装返回信息:" + StandardOutput, null);
  334. }
  335. return false;
  336. }
  337. }
  338. /// <summary>
  339. /// 安装失败信息
  340. /// </summary>
  341. /// <param name="sender"></param>
  342. /// <param name="e"></param>
  343. private static void InstallProcess_ErrorDataReceived(object sender, DataReceivedEventArgs e)
  344. {
  345. LogHelper.WriteErrLog("【ScreenCapturerRecorder安装】(installScreenCapturerRecorder)安装失败:" + e.Data, null);
  346. //MessageWindow.Show(e.Data);
  347. }
  348. #endregion
  349. #region 内存处理 -创建人:赵耀 -创建时间:2020年8月12日
  350. /// <summary>
  351. /// 内存释放压缩
  352. /// </summary>
  353. /// <param name="e"></param>
  354. protected override void OnStartup(StartupEventArgs e)
  355. {
  356. //private LierdaCracker cracker = new LierdaCracker();
  357. //cracker.Cracker(5000);//间隔时间
  358. //base.OnStartup(e);
  359. new Thread(o =>
  360. {
  361. while (true)
  362. {
  363. try
  364. {
  365. //FreeMemoryHelper.SetDate();
  366. FreeMemoryHelper.ReallocateMemory();
  367. Thread.Sleep(20000);//暂定每20秒释放一次
  368. }
  369. catch (Exception ex)
  370. {
  371. LogHelper.WriteErrLog("【释放内存(MyApp)" + ex.Message, ex);
  372. }
  373. }
  374. }).Start();
  375. }
  376. #endregion 内存处理 -创建人:赵耀 -创建时间:2020年8月12日
  377. }
  378. }