星火直播PC
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

App.cs 16KB

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