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

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