MainForm.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Windows.Forms;
  5. using System.Data;
  6. using System.Text;
  7. using System.ComponentModel;
  8. using Microsoft.Win32;
  9. using Newtonsoft.Json;
  10. using System.Net;
  11. using System.Runtime.Serialization;
  12. using System.Runtime.Serialization.Formatters.Binary;
  13. namespace felixyin
  14. {
  15. public partial class MainForm : Form
  16. {
  17. public MainForm()
  18. {
  19. // FIXME 生产环境,需要修改为4404
  20. string url = "http://localhost:4404";
  21. int bv = GetBrowserVersion();
  22. if (bv >= 8)// 浏览器版本不能小于8
  23. {
  24. bool isOk = ResponseOk(url);
  25. if (isOk)
  26. {
  27. SetWebBrowserFeatures(11);
  28. InitializeComponent();
  29. this.webBrowser1.ObjectForScripting = this;
  30. this.webBrowser1.Url = new Uri(url);
  31. }
  32. else
  33. {
  34. DialogResult result = MessageBox.Show("程序发生未知错误,请联系管理员:15965585803", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  35. if (result == DialogResult.OK)
  36. {
  37. System.Environment.Exit(0);
  38. }
  39. }
  40. }
  41. else
  42. {
  43. DialogResult result = MessageBox.Show("程序使用了IE组件,要求IE版本不能小于8,请先升级您的IE浏览器", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  44. if (result == DialogResult.OK)
  45. {
  46. System.Environment.Exit(0);
  47. }
  48. }
  49. }
  50. /// <summary>
  51. /// Get方式获取url地址输出内容
  52. /// </summary> /// <param name="url">url</param>
  53. public static bool ResponseOk(String url)
  54. {
  55. try
  56. {
  57. HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
  58. webRequest.Method = "GET";
  59. HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
  60. Console.WriteLine(webResponse.StatusCode);
  61. return true;
  62. }
  63. catch
  64. {
  65. return false;
  66. }
  67. }
  68. /**
  69. * 遍历文件夹,查找返回所有avi视频文件
  70. */
  71. public void FindFile4(List<Dictionary<string, string>> list, string sSourcePath)
  72. {
  73. //遍历文件夹
  74. DirectoryInfo theFolder = new DirectoryInfo(sSourcePath);
  75. FileInfo[] thefileInfo = theFolder.GetFiles("*.*", SearchOption.TopDirectoryOnly);
  76. string lmi1 = "";
  77. string avi1 = "";
  78. foreach (FileInfo NextFile in thefileInfo)
  79. { //遍历文件
  80. string fullName1 = NextFile.FullName;
  81. if (fullName1.Contains(".lmi"))
  82. {
  83. lmi1 = fullName1;
  84. }
  85. if (fullName1.Contains(".avi"))
  86. {
  87. avi1 = fullName1;
  88. }
  89. }
  90. if (lmi1 != "" && avi1 != "")
  91. {
  92. Dictionary<string, string> map = new Dictionary<string, string>();
  93. map.Add("avi_path", avi1);
  94. map.Add("lmi_path", lmi1);
  95. list.Add(map);
  96. }
  97. //遍历子文件夹
  98. DirectoryInfo[] dirInfo = theFolder.GetDirectories();
  99. foreach (DirectoryInfo NextFolder in dirInfo)
  100. {
  101. FindFile4(list, NextFolder.FullName);
  102. }
  103. }
  104. /**
  105. * 将从视频文件中获取的视频信息转化为json
  106. */
  107. public string selectFolder()
  108. {
  109. List<string> x = new List<string>();
  110. if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
  111. {
  112. if (this.folderBrowserDialog1.SelectedPath.Trim() != "")
  113. {
  114. string folder = this.folderBrowserDialog1.SelectedPath.Trim();
  115. List<Dictionary<string, string>> list = new List<Dictionary<string, string>>();
  116. this.FindFile4(list, folder);
  117. string jsonData = JsonConvert.SerializeObject(list);//序列化
  118. Console.WriteLine(jsonData);
  119. return jsonData;
  120. }
  121. }
  122. return "";
  123. }
  124. public string selectXlsx()
  125. {
  126. if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
  127. {
  128. if (this.openFileDialog1.FileName.Trim() != "")
  129. {
  130. string folder = this.openFileDialog1.FileName.Trim();
  131. return folder;
  132. }
  133. }
  134. return "";
  135. }
  136. public string selectOtherVideo()
  137. {
  138. if (this.openFileDialog2.ShowDialog() == DialogResult.OK)
  139. {
  140. if (this.openFileDialog2.FileName.Trim() != "")
  141. {
  142. string folder = this.openFileDialog2.FileName.Trim();
  143. return folder;
  144. }
  145. }
  146. return "";
  147. }
  148. /// <summary>
  149. /// DataSet转换成Json格式
  150. /// </summary>
  151. /// <paramname="ds">DataSet</param>
  152. ///<returns></returns>
  153. public static string DatasetToJson(DataSet ds, int total = -1)
  154. {
  155. StringBuilder json = new StringBuilder();
  156. foreach (DataTable dt in ds.Tables)
  157. {
  158. //{"total":5,"rows":[
  159. json.Append("{\"total\":");
  160. if (total == -1)
  161. {
  162. json.Append(dt.Rows.Count);
  163. }
  164. else
  165. {
  166. json.Append(total);
  167. }
  168. json.Append(",\"rows\":[");
  169. json.Append(DataTableToJson(dt));
  170. json.Append("]}");
  171. }
  172. return json.ToString();
  173. }
  174. /// <summary>
  175. /// dataTable转换成Json格式
  176. /// </summary>
  177. /// <paramname="dt"></param>
  178. ///<returns></returns>
  179. public static string DataTableToJson(DataTable dt)
  180. {
  181. StringBuilder jsonBuilder = new StringBuilder();
  182. for (int i = 0; i < dt.Rows.Count; i++)
  183. {
  184. jsonBuilder.Append("{");
  185. for (int j = 0; j < dt.Columns.Count; j++)
  186. {
  187. jsonBuilder.Append("\"");
  188. jsonBuilder.Append(dt.Columns[j].ColumnName);
  189. jsonBuilder.Append("\":\"");
  190. jsonBuilder.Append(dt.Rows[i][j].ToString());
  191. jsonBuilder.Append("\",");
  192. }
  193. if (dt.Columns.Count > 0)
  194. {
  195. jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
  196. }
  197. jsonBuilder.Append("},");
  198. }
  199. if (dt.Rows.Count > 0)
  200. {
  201. jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
  202. }
  203. return jsonBuilder.ToString();
  204. }
  205. /// <summary>
  206. /// 修改注册表信息来兼容当前程序
  207. ///
  208. /// </summary>
  209. static void SetWebBrowserFeatures(int ieVersion)
  210. {
  211. // don't change the registry if running in-proc inside Visual Studio
  212. if (LicenseManager.UsageMode != LicenseUsageMode.Runtime)
  213. return;
  214. //获取程序及名称
  215. var appName = System.IO.Path.GetFileName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
  216. //得到浏览器的模式的值
  217. UInt32 ieMode = GeoEmulationModee(ieVersion);
  218. var featureControlRegKey = @"HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\";
  219. //设置浏览器对应用程序(appName)以什么模式(ieMode)运行
  220. Registry.SetValue(featureControlRegKey + "FEATURE_BROWSER_EMULATION",
  221. appName, ieMode, RegistryValueKind.DWord);
  222. // enable the features which are "On" for the full Internet Explorer browser
  223. //不晓得设置有什么用
  224. Registry.SetValue(featureControlRegKey + "FEATURE_ENABLE_CLIPCHILDREN_OPTIMIZATION",
  225. appName, 1, RegistryValueKind.DWord);
  226. //Registry.SetValue(featureControlRegKey + "FEATURE_AJAX_CONNECTIONEVENTS",
  227. // appName, 1, RegistryValueKind.DWord);
  228. //Registry.SetValue(featureControlRegKey + "FEATURE_GPU_RENDERING",
  229. // appName, 1, RegistryValueKind.DWord);
  230. //Registry.SetValue(featureControlRegKey + "FEATURE_WEBOC_DOCUMENT_ZOOM",
  231. // appName, 1, RegistryValueKind.DWord);
  232. //Registry.SetValue(featureControlRegKey + "FEATURE_NINPUT_LEGACYMODE",
  233. // appName, 0, RegistryValueKind.DWord);
  234. }
  235. /// <summary>
  236. /// 获取浏览器的版本
  237. /// </summary>
  238. /// <returns></returns>
  239. static int GetBrowserVersion()
  240. {
  241. int browserVersion = 0;
  242. using (var ieKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer",
  243. RegistryKeyPermissionCheck.ReadSubTree,
  244. System.Security.AccessControl.RegistryRights.QueryValues))
  245. {
  246. var version = ieKey.GetValue("svcVersion");
  247. if (null == version)
  248. {
  249. version = ieKey.GetValue("Version");
  250. if (null == version)
  251. throw new ApplicationException("Microsoft Internet Explorer is required!");
  252. }
  253. int.TryParse(version.ToString().Split('.')[0], out browserVersion);
  254. }
  255. //如果小于7
  256. if (browserVersion < 7)
  257. {
  258. throw new ApplicationException("不支持的浏览器版本!");
  259. }
  260. return browserVersion;
  261. }
  262. /// <summary>
  263. /// 通过版本得到浏览器模式的值
  264. /// </summary>
  265. /// <param name="browserVersion"></param>
  266. /// <returns></returns>
  267. static UInt32 GeoEmulationModee(int browserVersion)
  268. {
  269. UInt32 mode = 11000; // Internet Explorer 11. Webpages containing standards-based !DOCTYPE directives are displayed in IE11 Standards mode.
  270. switch (browserVersion)
  271. {
  272. case 7:
  273. mode = 7000; // Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode.
  274. break;
  275. case 8:
  276. mode = 8000; // Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode.
  277. break;
  278. case 9:
  279. mode = 9000; // Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode.
  280. break;
  281. case 10:
  282. mode = 10000; // Internet Explorer 10.
  283. break;
  284. case 11:
  285. mode = 11000; // Internet Explorer 11
  286. break;
  287. }
  288. return mode;
  289. }
  290. private void Form1_FormClosing_1(object sender, FormClosingEventArgs e)
  291. {
  292. this.webBrowser1.Document.InvokeScript("beforeClose");
  293. DialogResult result = MessageBox.Show("您确定要关闭吗!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
  294. if (result == DialogResult.OK)
  295. {
  296. e.Cancel = false; //点击OK
  297. }
  298. else
  299. {
  300. e.Cancel = true;
  301. }
  302. }
  303. }
  304. }