BaseUtil.cs 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  1. using LabelManager2;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Diagnostics;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Net;
  12. using System.Net.Sockets;
  13. using System.Reflection;
  14. using System.Text;
  15. using System.Text.RegularExpressions;
  16. using System.Threading;
  17. using System.Windows.Forms;
  18. using System.Windows.Forms.DataVisualization.Charting;
  19. using System.Xml;
  20. using UAS_MES_NEW.CustomControl.DataGrid_View;
  21. using UAS_MES_NEW.CustomControl.GroupBoxWithBorder;
  22. using UAS_MES_NEW.CustomControl.TextBoxWithIcon;
  23. using UAS_MES_NEW.CustomControl.ValueLabel;
  24. using UAS_MES_NEW.Entity;
  25. using static System.Windows.Forms.Control;
  26. namespace UAS_MES_NEW.PublicMethod
  27. {
  28. class BaseUtil
  29. {
  30. /// <summary>
  31. /// 通过DataTable的ColumnName和Caption来拼接一条语句
  32. /// </summary>
  33. /// <param name=""></param>
  34. /// <returns></returns>
  35. public static string GetGridViewSelectContent(DataGridView d)
  36. {
  37. StringBuilder selectConetnt = new StringBuilder();
  38. DataTable dt = (DataTable)d.DataSource;
  39. if (dt == null)
  40. {
  41. foreach (DataGridViewColumn dc in d.Columns)
  42. {
  43. if (dc.DataPropertyName != "" && dc.DataPropertyName != null)
  44. {
  45. selectConetnt.Append(dc.Name + " as " + dc.Name + ",");
  46. }
  47. }
  48. }
  49. else
  50. {
  51. foreach (DataColumn dc in dt.Columns)
  52. {
  53. selectConetnt.Append(dc.Caption + " as " + dc.ColumnName + ",");
  54. }
  55. }
  56. return selectConetnt.Remove(selectConetnt.Length - 1, 1).ToString();
  57. }
  58. public static Dictionary<string, object> ToDictionary(string JsonData)
  59. {
  60. object Data = null;
  61. Dictionary<string, object> Dic = new Dictionary<string, object>();
  62. if (JsonData.StartsWith("["))
  63. {
  64. //如果目标直接就为数组类型,则将会直接输出一个Key为List的List<Dictionary<string, object>>集合
  65. //使用示例List<Dictionary<string, object>> ListDic = (List<Dictionary<string, object>>)Dic["List"];
  66. List<Dictionary<string, object>> List = new List<Dictionary<string, object>>();
  67. MatchCollection ListMatch = Regex.Matches(JsonData, @"{[\s\S]+?}");//使用正则表达式匹配出JSON数组
  68. foreach (Match ListItem in ListMatch)
  69. {
  70. List.Add(ToDictionary(ListItem.ToString()));//递归调用
  71. }
  72. Data = List;
  73. Dic.Add("List", Data);
  74. }
  75. else
  76. {
  77. MatchCollection Match = Regex.Matches(JsonData, @"""(.+?)"": {0,1}(\[[\s\S]+?\]|null|"".+?""|-{0,1}\d*)");//使用正则表达式匹配出JSON数据中的键与值
  78. foreach (Match item in Match)
  79. {
  80. try
  81. {
  82. if (item.Groups[2].ToString().StartsWith("["))
  83. {
  84. //如果目标是数组,将会输出一个Key为当前Json的List<Dictionary<string, object>>集合
  85. //使用示例List<Dictionary<string, object>> ListDic = (List<Dictionary<string, object>>)Dic["Json中的Key"];
  86. List<Dictionary<string, object>> List = new List<Dictionary<string, object>>();
  87. MatchCollection ListMatch = Regex.Matches(item.Groups[2].ToString(), @"{[\s\S]+?}");//使用正则表达式匹配出JSON数组
  88. foreach (Match ListItem in ListMatch)
  89. {
  90. List.Add(ToDictionary(ListItem.ToString()));//递归调用
  91. }
  92. Data = List;
  93. }
  94. else if (item.Groups[2].ToString().ToLower() == "null") Data = null;//如果数据为null(字符串类型),直接转换成null
  95. else Data = item.Groups[2].ToString(); //数据为数字、字符串中的一类,直接写入
  96. Dic.Add(item.Groups[1].ToString(), Data);
  97. }
  98. catch { }
  99. }
  100. }
  101. return Dic;
  102. }
  103. /// <summary>
  104. /// 禁止DataGirdView排序
  105. /// </summary>
  106. /// <param name="Dgv"></param>
  107. public static void DataGridViewNotSort(DataGridView Dgv)
  108. {
  109. foreach (DataGridViewColumn item in Dgv.Columns)
  110. item.SortMode = DataGridViewColumnSortMode.NotSortable;
  111. }
  112. public static string GetLocalIP()
  113. {
  114. try
  115. {
  116. string HostName = Dns.GetHostName(); //得到主机名
  117. IPHostEntry IpEntry = Dns.GetHostEntry(HostName);
  118. for (int i = 0; i < IpEntry.AddressList.Length; i++)
  119. {
  120. //从IP地址列表中筛选出IPv4类型的IP地址
  121. //AddressFamily.InterNetwork表示此IP为IPv4,
  122. //AddressFamily.InterNetworkV6表示此地址为IPv6类型
  123. if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
  124. {
  125. return IpEntry.AddressList[i].ToString();
  126. }
  127. }
  128. return "";
  129. }
  130. catch (Exception ex)
  131. {
  132. MessageBox.Show("获取本机IP出错:" + ex.Message);
  133. return "";
  134. }
  135. }
  136. /// <summary>
  137. /// 通过字段和其展示的中文值获取查询的内容
  138. /// </summary>
  139. /// <param name="field"></param>
  140. /// <param name="cnfield"></param>
  141. /// <returns></returns>
  142. public static string GetSelectContentByStringArray(string[] field, string[] cnfield)
  143. {
  144. StringBuilder sb = new StringBuilder();
  145. for (int i = 0; i < field.Length; i++)
  146. {
  147. sb.Append(field[i] + " as " + cnfield[i] + ",");
  148. }
  149. //去掉多余的逗号
  150. sb.Remove(sb.Length - 1, 1);
  151. return sb.ToString();
  152. }
  153. static int SortByIndex(Control A, Control B)
  154. {
  155. return A.TabIndex.CompareTo(B.TabIndex);
  156. }
  157. /// <summary>
  158. /// 传入控件的集合和DataTable,通过判断控件的名称和数据源的列的描述来匹配,支持单层的GroupBox和Panel
  159. /// </summary>
  160. /// <param name="collection"></param>
  161. /// <param name="dt"></param>
  162. public static void SetFormValue(ControlCollection collection, DataTable dt)
  163. {
  164. List<Control> ctl = new List<Control>();
  165. //将控件按照索引排序
  166. for (int i = 0; i < collection.Count; i++)
  167. {
  168. for (int j = 0; j < ctl.Count; j++)
  169. {
  170. if (!ctl.Contains(collection[i]))
  171. {
  172. if (collection[i].TabIndex > ctl[j].TabIndex)
  173. {
  174. ctl.Add(collection[i]);
  175. break;
  176. }
  177. else
  178. {
  179. ctl.Insert(j, collection[i]);
  180. break;
  181. }
  182. }
  183. }
  184. if (ctl.Count == 0)
  185. {
  186. ctl.Add(collection[i]);
  187. }
  188. }
  189. ctl.Sort(SortByIndex);
  190. //DataTable存在数据才进行赋值操作
  191. if (dt.Rows.Count > 0)
  192. {
  193. for (int i = 0; i < ctl.Count; i++)
  194. {
  195. //如果含有子控件则进行递归调用
  196. if (ctl[i].Controls.Count > 0)
  197. SetFormValue(ctl[i].Controls, dt);
  198. string controlName = ctl[i].Name;
  199. string controlsTag = ctl[i].Tag == null ? "" : ctl[i].Tag.ToString();
  200. //默认给TextBox和Label赋值
  201. if (ctl[i] is TextBox || ctl[i] is Label || ctl[i] is SearchTextBox || ctl[i] is MaCodeSearchTextBox || ctl[i] is EnterTextBox || ctl[i] is TextBoxGeneratePaCode || ctl[i] is NumericUpDown || ctl[i] is TextBoxWithTextArea ||ctl[i] is RichTextBox)
  202. {
  203. for (int j = 0; j < dt.Columns.Count; j++)
  204. {
  205. if (controlName.ToUpper() == dt.Columns[j].Caption.ToUpper() || controlsTag.ToUpper() == dt.Columns[j].Caption.ToUpper())
  206. {
  207. //字段含有Status内容的才进行转换
  208. if (controlName.ToUpper().Contains("STATUS") || controlsTag.ToUpper().Contains("STATUS"))
  209. {
  210. //对审批状态进行判断
  211. switch (dt.Rows[0][j].ToString().ToUpper())
  212. {
  213. case "ENTERING":
  214. ctl[i].Text = "在录入";
  215. break;
  216. case "UNAPPROVED":
  217. ctl[i].Text = "未批准";
  218. break;
  219. case "COMMITED":
  220. ctl[i].Text = "已提交";
  221. break;
  222. case "APPROVE":
  223. ctl[i].Text = "已批准";
  224. break;
  225. case "AUDITED":
  226. ctl[i].Text = "已审核";
  227. break;
  228. case "STARTED":
  229. ctl[i].Text = "已下放";
  230. break;
  231. case "UNCHECK":
  232. ctl[i].Text = "待检验";
  233. break;
  234. case "CHECKING":
  235. ctl[i].Text = "检验中";
  236. break;
  237. default:
  238. ctl[i].Text = dt.Rows[0][j].ToString();
  239. break;
  240. }
  241. }
  242. else
  243. ctl[i].Text = dt.Rows[0][j].ToString();
  244. }
  245. }
  246. }
  247. //对封装在GroupBox的和Panel的控件进行批量赋值
  248. if (ctl[i] is GroupBox || ctl[i] is Panel || ctl[i] is GroupBoxWithBorder)
  249. {
  250. for (int j = 0; j < ctl[i].Controls.Count; j++)
  251. {
  252. controlName = ctl[i].Controls[j].Name;
  253. if (ctl[i].Controls[j] is TextBox || ctl[i].Controls[j] is Label || ctl[i].Controls[j] is SearchTextBox || ctl[i].Controls[j] is MaCodeSearchTextBox)
  254. {
  255. for (int k = 0; k < dt.Columns.Count; k++)
  256. {
  257. if (controlName.ToUpper() == dt.Columns[k].Caption.ToUpper())
  258. {
  259. ctl[i].Controls[j].Text = dt.Rows[0][k].ToString();
  260. }
  261. }
  262. }
  263. }
  264. }
  265. }
  266. }
  267. //如果没有记录的话,将dt中含有的列的对应值设置为空
  268. else
  269. {
  270. for (int i = 0; i < ctl.Count; i++)
  271. {
  272. if (ctl[i] is TextBox || ctl[i] is Label || ctl[i] is SearchTextBox)
  273. {
  274. for (int j = 0; j < dt.Columns.Count; j++)
  275. {
  276. if (ctl[i].Name.ToUpper() == dt.Columns[j].Caption.ToUpper())
  277. {
  278. ctl[i].Text = "";
  279. }
  280. }
  281. }
  282. }
  283. }
  284. }
  285. /// <summary>
  286. /// 获取打印标签
  287. /// </summary>
  288. /// <param name="labelName"></param>
  289. /// <param name="labelUrl"></param>
  290. /// <param name="indate"></param>
  291. public static void GetPrintLabel(string labelName, string labelUrl)
  292. {
  293. BaseUtil.GetLabelUrl(labelUrl, labelName);
  294. }
  295. /// <summary>
  296. /// 获取打印标签
  297. /// </summary>
  298. /// <param name="labelName"></param>
  299. /// <param name="labelUrl"></param>
  300. /// <param name="indate"></param>
  301. public static void GetPrintLabel(string labelName, string labelUrl, string indate)
  302. {
  303. string LabelUrl = labelUrl;
  304. string LabelName = labelName;
  305. System.DateTime time = Convert.ToDateTime(indate);
  306. FileInfo file = new FileInfo(ftpOperater.DownLoadTo + LabelName);
  307. if (time.ToString() != file.LastWriteTime.ToString())
  308. BaseUtil.GetLabelUrl(LabelUrl, LabelName, time);
  309. }
  310. /// <summary>
  311. /// 获取标签的路径
  312. /// </summary>
  313. /// <param name="URL"></param>
  314. /// <param name="LabelName"></param>
  315. /// <param name="time"></param>
  316. /// <returns></returns>
  317. public static string GetLabelUrl(string URL, string LabelName, System.DateTime time)
  318. {
  319. ftpOperater ftp = new ftpOperater();
  320. return ftp.DownLoadFromSharePath(URL, LabelName);
  321. }
  322. /// <summary>
  323. /// 移除重复行
  324. /// </summary>
  325. /// <param name="dt"></param>
  326. /// <param name="field"></param>
  327. /// <returns></returns>
  328. public static DataTable DeleteSameRow(DataTable dt, string field)
  329. {
  330. ArrayList indexList = new ArrayList();
  331. // 找出待删除的行索引
  332. for (int i = 0; i < dt.Rows.Count - 1; i++)
  333. {
  334. if (!IsContain(indexList, i))
  335. {
  336. for (int j = i + 1; j < dt.Rows.Count; j++)
  337. {
  338. if (dt.Rows[i][field].ToString() == dt.Rows[j][field].ToString())
  339. {
  340. indexList.Add(j);
  341. }
  342. }
  343. }
  344. }
  345. indexList.Sort();
  346. // 排序
  347. for (int i = indexList.Count - 1; i >= 0; i--)// 根据待删除索引列表删除行
  348. {
  349. int index = Convert.ToInt32(indexList[i]);
  350. dt.Rows.RemoveAt(index);
  351. }
  352. return dt;
  353. }
  354. /// <summary>
  355. /// 判断数组中是否存在
  356. /// </summary>
  357. /// <param name="indexList">数组</param>
  358. /// <param name="index">索引</param>
  359. /// <returns></returns>
  360. public static bool IsContain(ArrayList indexList, int index)
  361. {
  362. for (int i = 0; i < indexList.Count; i++)
  363. {
  364. int tempIndex = Convert.ToInt32(indexList[i]);
  365. if (tempIndex == index)
  366. {
  367. return true;
  368. }
  369. }
  370. return false;
  371. }
  372. //播放音频文件
  373. public static void PlaySound(string FileName)
  374. {
  375. //要加载COM组件:Microsoft speech object Library
  376. if (!System.IO.File.Exists(FileName))
  377. {
  378. return;
  379. }
  380. SpeechLib.SpVoiceClass pp = new SpeechLib.SpVoiceClass();
  381. SpeechLib.SpFileStreamClass spFs = new SpeechLib.SpFileStreamClass();
  382. spFs.Open(FileName, SpeechLib.SpeechStreamFileMode.SSFMOpenForRead, true);
  383. SpeechLib.ISpeechBaseStream Istream = spFs as SpeechLib.ISpeechBaseStream;
  384. pp.SpeakStream(Istream, SpeechLib.SpeechVoiceSpeakFlags.SVSFIsFilename);
  385. spFs.Close();
  386. }
  387. /// <summary>
  388. /// 从DGV获取指定的列的数据形式是数组的形式
  389. /// </summary>
  390. public static ArrayList[] GetColumnDataFromDGV(DataGridView dgv, string[] ColumnName)
  391. {
  392. ArrayList[] array = new ArrayList[ColumnName.Length];
  393. //实例化和查询参数个数一样的ArrayList
  394. for (int i = 0; i < ColumnName.Length; i++)
  395. {
  396. array[i] = new ArrayList();
  397. }
  398. DataTable dt = (DataTable)dgv.DataSource;
  399. //如果第一列是否选框的话
  400. if (dgv.Columns[0] is DataGridViewCheckBoxColumn)
  401. {
  402. for (int i = 0; i < dt.Rows.Count; i++)
  403. {
  404. if (dgv.Rows[i].Cells[0].FormattedValue.ToString() == "True")
  405. {
  406. for (int j = 0; j < ColumnName.Length; j++)
  407. {
  408. array[j].Add(dt.Rows[i][ColumnName[j]]);
  409. }
  410. }
  411. }
  412. }
  413. //否则直接获取全部的数据
  414. else
  415. {
  416. for (int i = 0; i < dgv.RowCount; i++)
  417. {
  418. for (int j = 0; j < ColumnName.Length; j++)
  419. {
  420. array[j].Add(dt.Rows[i][ColumnName[j]]);
  421. }
  422. }
  423. }
  424. return array;
  425. }
  426. /// <summary>
  427. /// 通过DataGridView和需要隐藏的字段的数组来对字段进行隐藏
  428. /// </summary>
  429. /// <param name="dgv"></param>
  430. /// <param name="field"></param>
  431. public static void HideField(DataGridView dgv, string[] field)
  432. {
  433. DataTable dt = (DataTable)dgv.DataSource;
  434. foreach (DataColumn dc in dt.Columns)
  435. {
  436. foreach (string s in field)
  437. {
  438. if (dc.Caption == s)
  439. dgv.Columns[dc.ColumnName].Visible = false;
  440. }
  441. }
  442. }
  443. /// <summary>
  444. ///
  445. /// </summary>
  446. /// <param name="dgv"></param>
  447. public static void ExpandDGVCheck(DataGridViewExpand dgv, DataGridViewCellEventArgs e)
  448. {
  449. if (e.ColumnIndex == 0 && e.RowIndex >= 0)
  450. {
  451. if (dgv.Rows[e.RowIndex] is CollapseDataGridViewRow)
  452. {
  453. int CollapseRowCount = (dgv.Rows[e.RowIndex] as CollapseDataGridViewRow).Rows.Count;
  454. if (CollapseRowCount > 0)
  455. {
  456. for (int i = (e.RowIndex + 2); i < (e.RowIndex + 1 + CollapseRowCount); i++)
  457. {
  458. try
  459. {
  460. dgv.Rows[i].Cells[0].Value = dgv.Rows[e.RowIndex].Cells[0].EditedFormattedValue;
  461. }
  462. catch (Exception) { }
  463. }
  464. }
  465. }
  466. }
  467. }
  468. /// <summary>
  469. /// 通过查询的内容获取到字段的描述
  470. /// </summary>
  471. /// <param name="field"></param>
  472. /// <returns></returns>
  473. public static string[] GetCaptionFromField(string field)
  474. {
  475. string[] caption = field.Split(',');
  476. for (int i = 0; i < caption.Length; i++)
  477. {
  478. caption[i] = caption[i].Substring(0, caption[i].LastIndexOf("as")).Trim();
  479. }
  480. return caption;
  481. }
  482. /// <summary>
  483. /// 通过查询的语句获取查询的字段
  484. /// </summary>
  485. /// <param name="field"></param>
  486. /// <returns></returns>
  487. public static string[] GetField(string field)
  488. {
  489. string[] fields = field.Split(',');
  490. for (int i = 0; i < fields.Length; i++)
  491. {
  492. fields[i] = fields[i].Substring(fields[i].LastIndexOf("as") + 2, fields[i].Length - fields[i].LastIndexOf("as") - 2).Trim();
  493. }
  494. return fields;
  495. }
  496. /// <summary>
  497. /// 通过描述取DataTable的列名,主要用于从配置中取数据
  498. /// </summary>
  499. /// <param name="dt"></param>
  500. /// <param name="caption"></param>
  501. /// <returns></returns>
  502. public static string GetColumnNameByCaption(DataTable dt, string caption)
  503. {
  504. foreach (DataColumn dc in dt.Columns)
  505. {
  506. if (dc.Caption.ToLower() == caption)
  507. {
  508. return dc.ColumnName;
  509. }
  510. }
  511. return null;
  512. }
  513. //用于封装异常,也可以用于错误的提示
  514. public static void ShowError(string errorMessage)
  515. {
  516. throw new Exception(errorMessage);
  517. }
  518. /// <summary>
  519. /// 判断控件的某个事件是否已经绑定了方法
  520. /// </summary>
  521. /// <param name="Control1"></param>
  522. /// <param name="EventName"></param>
  523. /// <returns></returns>
  524. public static bool ControlHasEvent(Control Control1, string EventName)
  525. {
  526. //需要查询的内容
  527. BindingFlags myBindingFlags = BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
  528. Assembly a = Assembly.GetAssembly(Control1.GetType());
  529. Type t = a.GetType(Control1.GetType().FullName, true);
  530. //获取控件的事件
  531. FieldInfo fi = t.GetField(EventName, myBindingFlags);
  532. EventHandlerList ehl = Control1.GetType().GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy).GetValue(Control1, null) as EventHandlerList;
  533. //判断事件的委托数量是否大于0
  534. if (ehl != null)
  535. {
  536. Delegate d = ehl[fi.GetValue(Control1)];
  537. if (d != null && d.GetInvocationList().Length > 0)
  538. {
  539. return true;
  540. }
  541. }
  542. return false;
  543. }
  544. /// <summary>
  545. /// 获取控件的事件列表
  546. /// </summary>
  547. /// <param name="Control1"></param>
  548. /// <returns></returns>
  549. public static FieldInfo[] GetControlsEvent(Control Control1)
  550. {
  551. BindingFlags myBindingFlags = BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
  552. Assembly a = Assembly.GetAssembly(Control1.GetType());
  553. Type t = a.GetType(Control1.GetType().FullName, true);
  554. FieldInfo[] finf = t.GetFields(myBindingFlags);
  555. return finf;
  556. }
  557. /// <summary>
  558. /// 清除DataTable的结构和数据,清除列结构时需要从最后的一列开始删
  559. /// </summary>
  560. /// <param name="dt"></param>
  561. public static void CleanDataTable(DataTable dt)
  562. {
  563. for (int i = dt.Columns.Count - 1; i >= 0; i--)
  564. dt.Columns.Remove(dt.Columns[i]);
  565. }
  566. /// <summary>
  567. /// 获取标签的路径
  568. /// </summary>
  569. /// <param name="URL"></param>
  570. /// <param name="LabelName"></param>
  571. /// <returns></returns>
  572. public static string GetLabelUrl(string URL, string LabelName)
  573. {
  574. ftpOperater ftp = new ftpOperater();
  575. return ftp.DownLoadFromSharePath(URL, LabelName);
  576. }
  577. /// <summary>
  578. /// 往DataTable中添加数据
  579. /// </summary>
  580. public static void AddDataToDataTable(DataTable dt, params string[] param)
  581. {
  582. DataRow dr = dt.NewRow();
  583. for (int i = 0; i < dt.Columns.Count; i++)
  584. {
  585. dr[dt.Columns[i].ColumnName] = param[i];
  586. }
  587. }
  588. /// <summary>
  589. /// 不清除表结构,只清除数据
  590. /// </summary>
  591. /// <param name="dt"></param>
  592. public static void CleanDataTableData(DataTable dt)
  593. {
  594. for (int i = dt.Rows.Count - 1; i >= 0; i--)
  595. {
  596. dt.Rows.Remove(dt.Rows[i]);
  597. }
  598. }
  599. /// <summary>
  600. /// 获取拼接的字段
  601. /// </summary>
  602. /// <param name="dt"></param>
  603. /// <returns></returns>
  604. public static string GetFieldFromDataTable(DataTable dt)
  605. {
  606. StringBuilder sb = new StringBuilder();
  607. foreach (DataColumn dc in dt.Columns)
  608. {
  609. sb.Append(dc.Caption + ",");
  610. }
  611. return sb.ToString().Substring(sb.ToString().Length - 1, 1);
  612. }
  613. /// <summary>
  614. /// 已经定义好的DataGridView绑定数据,operate是用来添加操作列的
  615. /// </summary>
  616. /// <param name="dgv"></param>
  617. /// <param name="dt"></param>
  618. /// <param name="AddOpetateColumn"></param>
  619. /// <param name="operate"></param>
  620. public static void FillDgvWithDataTable(DataGridView dgv, DataTable dt, params DataGridViewImageColumn[] operate)
  621. {
  622. dgv.AutoGenerateColumns = false;
  623. dgv.DataSource = dt;
  624. if (operate.Length > 0)
  625. {
  626. if (dgv.Columns[operate[0].Name] != null)
  627. {
  628. dgv.Columns.Remove(dgv.Columns[operate[0].Name]);
  629. }
  630. dgv.Columns.Add(operate[0]);
  631. }
  632. ////纯英文的列不予展示
  633. //Regex regEnglish = new Regex("^[A-z]+$");
  634. //foreach (DataGridViewColumn dgvc in dgv.Columns)
  635. //{
  636. // if (regEnglish.IsMatch(dgvc.HeaderText))
  637. // {
  638. // dgvc.Visible = false;
  639. // }
  640. //}
  641. }
  642. /// <summary>
  643. /// 清除DataGridView的数据
  644. /// </summary>
  645. /// <param name="dgv"></param>
  646. public static void CleanDGVData(DataGridView dgv)
  647. {
  648. for (int i = dgv.Rows.Count - 1; i >= 0; i--)
  649. {
  650. dgv.Rows.RemoveAt(i);
  651. }
  652. DataTable dt = dgv.DataSource as DataTable;
  653. if (dt != null)
  654. {
  655. dt.AcceptChanges();
  656. }
  657. }
  658. public static void CleanForm(Form Form)
  659. {
  660. for (int i = 0; i < Form.Controls.Count; i++)
  661. {
  662. if (Form.Controls[i].Controls.Count > 0)
  663. {
  664. CleanControls(Form.Controls[i].Controls);
  665. }
  666. if (Form.Controls[i] is EnterTextBox || Form.Controls[i] is TextBox || Form.Controls[i] is ValueLabel || Form.Controls[i] is SearchTextBox || Form.Controls[i] is ValueNumLabel || Form.Controls[i] is MaCodeSearchTextBox)
  667. Form.Controls[i].Text = "";
  668. if (Form.Controls[i] is DataGridView)
  669. CleanDGVData((DataGridView)Form.Controls[i]);
  670. }
  671. }
  672. public static void CleanControls(ControlCollection collection)
  673. {
  674. for (int i = 0; i < collection.Count; i++)
  675. {
  676. if (collection[i].Controls.Count > 0)
  677. CleanControls(collection[i].Controls);
  678. if (collection[i] is EnterTextBox || collection[i] is TextBox || collection[i] is ValueLabel || collection[i] is SearchTextBox || collection[i] is ValueNumLabel || collection[i] is MaCodeSearchTextBox)
  679. collection[i].Text = "";
  680. if (collection[i] is DataGridView)
  681. CleanDGVData((DataGridView)collection[i]);
  682. }
  683. }
  684. public static void CleanControlsText(params Control[] ctl)
  685. {
  686. foreach (Control item in ctl)
  687. item.Text = "";
  688. }
  689. /// <summary>
  690. /// 需要SQL的顺序和DGV的列的顺序一致
  691. /// </summary>
  692. /// <param name="dgv"></param>
  693. /// <param name="dt"></param>
  694. /// <param name="CheckBox"></param>
  695. public static void FillExpandDgvWithDataTable(DataGridViewExpand dgv, DataTable dt, bool CheckBox, bool CheckBoxTrue)
  696. {
  697. CleanDGVData(dgv);
  698. if (CheckBox)
  699. {
  700. for (int i = 0; i < dt.Rows.Count; i++)
  701. {
  702. CollapseDataGridViewRow collapseRow = new CollapseDataGridViewRow();
  703. collapseRow.IsCollapse = true;
  704. DataGridViewCheckBoxCell checkcell = new DataGridViewCheckBoxCell();
  705. collapseRow.Cells.Add(checkcell);
  706. checkcell.Value = CheckBoxTrue;
  707. //因为DGV中可能有空置的列多出,所以需要用DataTable的列进行循环
  708. for (int j = 0; j < dt.Columns.Count; j++)
  709. {
  710. DataGridViewTextBoxCell textcell = new DataGridViewTextBoxCell();
  711. textcell.Value = dt.Rows[i][j].ToString();
  712. collapseRow.Cells.Add(textcell);
  713. textcell.ReadOnly = true;
  714. }
  715. collapseRow.Tag = "MainRow";
  716. dgv.Rows.Add(collapseRow);
  717. }
  718. }
  719. else
  720. {
  721. for (int i = 0; i < dt.Rows.Count; i++)
  722. {
  723. CollapseDataGridViewRow collapseRow = new CollapseDataGridViewRow();
  724. collapseRow.IsCollapse = true;
  725. for (int j = 1; j <= dt.Columns.Count; j++)
  726. {
  727. DataGridViewTextBoxCell textcell = new DataGridViewTextBoxCell();
  728. textcell.Value = dt.Rows[i][j - 1].ToString();
  729. collapseRow.Cells.Add(textcell);
  730. }
  731. dgv.Rows.Add(collapseRow);
  732. collapseRow.ReadOnly = true;
  733. }
  734. }
  735. }
  736. /// <summary>
  737. /// 用于给DGV中的Combox列赋静态值
  738. /// </summary>
  739. /// <param name="dgvc"></param>
  740. /// <param name="displayField"></param>
  741. /// <param name="valueField"></param>
  742. /// <param name="Value"></param>
  743. /// <returns></returns>
  744. public static void SetDgvColumnComboxData(DataGridViewComboBoxColumn dgvc, string DataPropertyName, string displayField, string valueField, string[] Value)
  745. {
  746. DataTable dt = new DataTable();
  747. dt.Columns.Add(displayField);
  748. dt.Columns.Add(valueField);
  749. for (int i = 0; i < Value.Length; i++)
  750. {
  751. DataGridViewRow row = new DataGridViewRow();
  752. dt.Rows.Add(row);
  753. dt.Rows[i][displayField] = Value[i].Split('#')[0];
  754. dt.Rows[i][valueField] = Value[i].Split('#')[1];
  755. }
  756. dgvc.DataPropertyName = DataPropertyName;
  757. dgvc.DataSource = dt;
  758. dgvc.DisplayMember = displayField;
  759. dgvc.ValueMember = valueField;
  760. }
  761. /// <summary>
  762. /// 用于给DGV中的ComboxCell赋静态值
  763. /// </summary>
  764. /// <param name="dgvcc"></param>
  765. /// <param name="displayField"></param>
  766. /// <param name="valueField"></param>
  767. /// <param name="Value"></param>
  768. public static void SetDGVCellComboxData(DataGridViewComboBoxCell dgvcc, string displayField, string valueField, string[] Value)
  769. {
  770. DataTable dt = new DataTable();
  771. dt.Columns.Add(displayField);
  772. dt.Columns.Add(valueField);
  773. for (int i = 0; i < Value.Length; i++)
  774. {
  775. DataRow dr = dt.NewRow();
  776. dr[displayField] = Value[i].Split('#')[0];
  777. dr[valueField] = Value[i].Split('#')[1];
  778. dt.Rows.Add(dr);
  779. }
  780. dgvcc.DisplayMember = displayField;
  781. dgvcc.ValueMember = valueField;
  782. dgvcc.DataSource = dt;
  783. }
  784. /// <summary>
  785. /// 获取刷选的SQL语句,传入的是TextBox的Control,传入的SQL不带Where条件
  786. /// </summary>
  787. /// <param name="SQL"></param>
  788. /// <param name="Condition"></param>
  789. /// <returns></returns>
  790. public static string GetScreenSqlCondition(params Control[] Condition)
  791. {
  792. string condition = "";
  793. //用于统计传入的控件的空值数
  794. int EmptyControlCount = 0;
  795. for (int i = 0; i < Condition.Length; i++)
  796. {
  797. //如果Text不为空再进行条件的拼接
  798. if (Condition[i].Text != "")
  799. {
  800. if (Condition[i] is ComboBox)
  801. {
  802. condition += "(" + Condition[i].Tag + " like " + "'%" + (Condition[i] as ComboBox).SelectedValue + "%' )";
  803. }
  804. else
  805. {
  806. condition += "(" + Condition[i].Tag + " like " + "'%" + Condition[i].Text + "%' )";
  807. }
  808. //如果不是最后要判断之后有没有空值的如果有一个Text的值不为空都需要添加and
  809. //添加了一次And之后跳出循环,因为如果后面多项有值会重复添加and
  810. for (int j = i + 1; j < Condition.Length; j++)
  811. {
  812. if (j < Condition.Length)
  813. {
  814. if (Condition[j].Text != "")
  815. {
  816. condition += " and ";
  817. break;
  818. }
  819. }
  820. }
  821. }
  822. else
  823. {
  824. EmptyControlCount = EmptyControlCount + 1;
  825. }
  826. }
  827. //如果所有的控件传入的都是空值则返回也为空
  828. if (EmptyControlCount == Condition.Length)
  829. return "";
  830. else
  831. condition = " where " + condition;
  832. return condition;
  833. }
  834. public static void CleanDataGridView(DataGridView dgv)
  835. {
  836. for (int i = dgv.Columns.Count - 1; i >= 0; i--)
  837. {
  838. dgv.Columns.RemoveAt(i);
  839. }
  840. }
  841. /// <summary>
  842. /// 取出SQL中的参数占位符
  843. /// </summary>
  844. /// <param name="SQL"></param>
  845. /// <returns></returns>
  846. public static string[] GetParamFromSQL(string SQL)
  847. {
  848. string[] par = SQL.Split(':');
  849. //用来存参数的数组
  850. StringBuilder[] addpar = new StringBuilder[par.Length - 1];
  851. string[] param = new string[par.Length - 1];
  852. for (int i = 0; i < par.Length - 1; i++)
  853. {
  854. //新建一个char类型的数组用来存储每个字节的变量
  855. char[] c = par[i + 1].ToCharArray();
  856. addpar[i] = new StringBuilder();
  857. for (int j = 0; j < c.Length; j++)
  858. {
  859. if (c[j] != ' ' && c[j] != ',' && c[j] != ')')
  860. {
  861. addpar[i].Append(c[j]);
  862. }
  863. else
  864. {
  865. break;
  866. }
  867. }
  868. }
  869. for (int i = 0; i < par.Length - 1; i++)
  870. {
  871. param[i] = addpar[i].ToString();
  872. }
  873. return param;
  874. }
  875. public static void SetFormCenter(Form form)
  876. {
  877. form.StartPosition = FormStartPosition.CenterParent;
  878. }
  879. /// <summary>
  880. /// 设置DataGridView的指定列可编辑
  881. /// </summary>
  882. /// <param name="DGV"></param>
  883. /// <param name="EditAbleField"></param>
  884. public static void SetDataGridViewReadOnly(DataGridView DGV, string[] EditAbleField)
  885. {
  886. foreach (DataGridViewColumn dc in DGV.Columns)
  887. {
  888. dc.ReadOnly = true;
  889. foreach (string s in EditAbleField)
  890. {
  891. if (dc.Name.ToLower() == s.ToLower())
  892. {
  893. DGV.Columns[dc.Name].ReadOnly = false;
  894. }
  895. }
  896. }
  897. }
  898. //判断带有CheckBox的DGV是否有项目勾选了
  899. public static DataTable DGVIfChecked(DataGridView dgv)
  900. {
  901. int CheckCount = 0;
  902. DataTable dt = new DataTable();
  903. //第一列是勾选框,排除在循环之外
  904. for (int i = 1; i < dgv.Columns.Count; i++)
  905. {
  906. dt.Columns.Add(dgv.Columns[i].Name);
  907. }
  908. for (int i = 0; i < dgv.RowCount; i++)
  909. {
  910. if (dgv.Rows[i].Cells[0].Value != null)
  911. {
  912. if (dgv.Rows[i].Cells[0].FormattedValue.ToString() == "True")
  913. {
  914. if (dgv.Rows[i].Tag.ToString() == "SonRow")
  915. {
  916. DataRow dr = dt.NewRow();
  917. for (int j = 1; j < dgv.ColumnCount; j++)
  918. {
  919. dr[dgv.Columns[j].Name] = dgv.Rows[i].Cells[j].FormattedValue;
  920. }
  921. dt.Rows.Add(dr);
  922. CheckCount++;
  923. }
  924. }
  925. }
  926. }
  927. //判断是否勾选了明细
  928. if (CheckCount == 0)
  929. return null;
  930. return dt;
  931. }
  932. public static void GetExpandDGVCheckedRow(DataGridView dgv, DataTable dt, int RowIndex, int DistinctColumnIndex)
  933. {
  934. //第一列是勾选框,排除在循环之外
  935. if (dt.Columns.Count == 0)
  936. {
  937. for (int i = 0; i < dgv.Columns.Count; i++)
  938. dt.Columns.Add(dgv.Columns[i].Name);
  939. }
  940. //是展开的子行的数据
  941. if (dgv.Rows[RowIndex].Tag != null && dgv.Rows[RowIndex].Tag.ToString() == "SonRow")
  942. {
  943. DataRow dr = dt.NewRow();
  944. DataRow[] datarow = (dt.Select(dgv.Columns[DistinctColumnIndex].Name + " ='" + dgv.Rows[RowIndex].Cells[DistinctColumnIndex].FormattedValue + "'"));
  945. //判断值是否存在,存在移除重新添加
  946. if (datarow.Length > 0)
  947. {
  948. dt.Rows.Remove(datarow[0]);
  949. }
  950. for (int j = 0; j < dgv.ColumnCount; j++)
  951. {
  952. dr[dgv.Columns[j].Name] = dgv.Rows[RowIndex].Cells[j].FormattedValue;
  953. }
  954. dt.Rows.Add(dr);
  955. }
  956. }
  957. /// <summary>
  958. /// 设置只允许输入数字
  959. /// </summary>
  960. /// <param name="sender"></param>
  961. /// <param name="e"></param>
  962. public static void NumOnly(object sender, KeyPressEventArgs e)
  963. {
  964. if (e.KeyChar != '\b')//这是允许输入退格键
  965. {
  966. if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字
  967. {
  968. e.Handled = true;
  969. }
  970. }
  971. }
  972. public static string AddField(string[] Fields)
  973. {
  974. string sql = " ";
  975. foreach (string field in Fields)
  976. sql += field + ",";
  977. return sql.Substring(0, sql.Length - 1);
  978. }
  979. /// <summary>
  980. /// 筛选DataTable
  981. /// </summary>
  982. /// <param name="dt"></param>
  983. /// <param name="condition"></param>
  984. /// <returns></returns>
  985. public static DataTable filterDataTable(DataTable dt, String condition)
  986. {
  987. if (dt == null)
  988. return new DataTable();
  989. //获取筛选条件中的列名,值
  990. DataRow[] dataRows = dt.Select(condition);
  991. DataTable ndt = dt.Clone();
  992. for (int i = 0; i < dataRows.Length; i++)
  993. {
  994. ndt.Rows.Add(dataRows[i].ItemArray);
  995. }
  996. return ndt;
  997. }
  998. /// <summary>
  999. /// 图表绘制公共方法样本
  1000. /// </summary>
  1001. /// <param name="chart1"></param>
  1002. /// <param name="_dt"></param>
  1003. /// <param name="seriesChartType"></param>
  1004. /// <param name="_title"></param>
  1005. /// <param name="XValueMember"></param>
  1006. /// <param name="YValueMembers"></param>
  1007. /// <param name="Xname"></param>
  1008. /// <param name="Yname"></param>
  1009. public static void ViewChart(Chart chart1, DataTable _dt, SeriesChartType seriesChartType, string _title, string XValueMember, string YValueMembers, string Xname, string Yname)
  1010. {
  1011. chart1.Series[0].ChartType = seriesChartType;
  1012. chart1.DataSource = _dt;
  1013. chart1.Series[0].MarkerStyle = MarkerStyle.Circle;
  1014. chart1.Series[0].MarkerSize = 8;
  1015. chart1.Series[0].XValueMember = XValueMember;
  1016. chart1.Series[0].YValueMembers = YValueMembers;
  1017. chart1.Series[0].Label = "#VAL";
  1018. chart1.Series[0].LabelToolTip = Xname + ": #VAL\r\n " + Yname + " #VALX";
  1019. chart1.Series[0].BackSecondaryColor = Color.DarkCyan;
  1020. chart1.Series[0].BorderColor = Color.DarkOliveGreen;
  1021. chart1.Series[0].LabelBackColor = Color.Transparent;
  1022. chart1.Series[0].LegendText = Yname;
  1023. chart1.Legends[0].Title = _title;
  1024. //chart1.ChartAreas[0].Area3DStyle.Enable3D = true;
  1025. }
  1026. /// <summary>
  1027. /// 将新增打印进程信息写入静态文件
  1028. /// </summary>
  1029. /// <param name="lbl"></param>
  1030. public static void WriteLbl(ApplicationClass lbl)
  1031. {
  1032. Process[] processes = System.Diagnostics.Process.GetProcessesByName("lppa");
  1033. Dictionary<string, int> ProInf = new Dictionary<string, int>();
  1034. for (int i = 0; i < processes.Length; i++)
  1035. {
  1036. try
  1037. {
  1038. if (processes[i].ProcessName == "lppa")
  1039. {
  1040. ProInf.Add(processes[i].StartTime.ToString(), processes[i].Id);
  1041. }
  1042. }
  1043. catch
  1044. {
  1045. }
  1046. }
  1047. var temp = ProInf.Keys.Max();
  1048. String str = SystemInf.ProcessesID + "|" + ProInf[temp];
  1049. FileStream fs = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Append, FileAccess.Write);
  1050. StreamWriter sw = new StreamWriter(fs);
  1051. sw.WriteLine(str, Encoding.UTF8);
  1052. sw.Close();
  1053. fs.Close();
  1054. }
  1055. public static void WriteLbl()
  1056. {
  1057. Process[] processes = System.Diagnostics.Process.GetProcessesByName("lppa");
  1058. Dictionary<string, int> ProInf = new Dictionary<string, int>();
  1059. for (int i = 0; i < processes.Length; i++)
  1060. {
  1061. try
  1062. {
  1063. if (processes[i].ProcessName == "lppa")
  1064. {
  1065. ProInf.Add(processes[i].StartTime.ToString(), processes[i].Id);
  1066. }
  1067. }
  1068. catch
  1069. {
  1070. }
  1071. }
  1072. var temp = ProInf.Keys.Max();
  1073. String str = SystemInf.ProcessesID + "|" + ProInf[temp];
  1074. FileStream fs = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Append, FileAccess.Write);
  1075. StreamWriter sw = new StreamWriter(fs);
  1076. sw.WriteLine(str, Encoding.UTF8);
  1077. sw.Close();
  1078. fs.Close();
  1079. }
  1080. public static Object GetCacheData(string ParamName)
  1081. {
  1082. try
  1083. {
  1084. Object o = null;
  1085. //根据地址读取xml文件
  1086. XmlDocument doc = new XmlDocument();
  1087. XmlReaderSettings settings = new XmlReaderSettings { CheckCharacters = false };
  1088. //忽略文档里面的注释
  1089. settings.IgnoreComments = true;
  1090. XmlReader reader = XmlReader.Create(SystemInf.CacheFilePath, settings);
  1091. doc.Load(reader);
  1092. //先得到根节点
  1093. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  1094. //再由根节点去找制定的节点
  1095. XmlNodeList nodeList = rootNode.ChildNodes;
  1096. foreach (XmlNode node in nodeList)
  1097. {
  1098. //找到了这个节点名字
  1099. if (node.Name == ParamName)
  1100. {
  1101. //返回节点的内容
  1102. switch (((XmlElement)node).GetAttribute("Type"))
  1103. {
  1104. case "System.String":
  1105. o = node.InnerText;
  1106. break;
  1107. case "System.Int32":
  1108. o = int.Parse(node.InnerText);
  1109. break;
  1110. case "System.Boolean":
  1111. o = node.InnerText == "True" ? true : false;
  1112. break;
  1113. default:
  1114. break;
  1115. }
  1116. break;
  1117. }
  1118. }
  1119. //关闭reader
  1120. reader.Close();
  1121. if (o == null)
  1122. return "";
  1123. else
  1124. return o;
  1125. }
  1126. catch (Exception e)
  1127. {
  1128. LogManager.DoLog(e.Message);
  1129. return "";
  1130. }
  1131. }
  1132. public static void SetCacheData(string ParamName, object Value)
  1133. {
  1134. try
  1135. {
  1136. //根据地址读取xml文件
  1137. XmlDocument doc = new XmlDocument();
  1138. XmlReaderSettings settings = new XmlReaderSettings { CheckCharacters = false };
  1139. //忽略文档里面的注释
  1140. settings.IgnoreComments = true;
  1141. XmlReader reader = XmlReader.Create(SystemInf.CacheFilePath, settings);
  1142. doc.Load(reader);
  1143. //先得到根节点
  1144. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  1145. //再由根节点去找制定的节点
  1146. XmlNodeList nodeList = rootNode.ChildNodes;
  1147. bool flag = false;
  1148. foreach (XmlNode node in nodeList)
  1149. {
  1150. //找到了这个节点名字
  1151. if (node.Name == ParamName)
  1152. {
  1153. //就直接赋值
  1154. node.InnerText = Value.ToString();
  1155. flag = true;
  1156. }
  1157. }
  1158. //如果没有该节点,就创建节点保存结果
  1159. if (!flag)
  1160. {
  1161. //创建节点
  1162. XmlElement newNode = doc.CreateElement(ParamName);
  1163. XmlAttribute attr = doc.CreateAttribute("Type");
  1164. attr.InnerText = Value.GetType().ToString();
  1165. newNode.InnerText = Value.ToString();
  1166. newNode.SetAttributeNode(attr);
  1167. //讲新建的节点挂到根节点上
  1168. rootNode.AppendChild(newNode);
  1169. }
  1170. //关闭Reader
  1171. reader.Close();
  1172. doc.Save(SystemInf.CacheFilePath);
  1173. }
  1174. catch (Exception e)
  1175. {
  1176. LogManager.DoLog(e.Message);
  1177. }
  1178. }
  1179. public static void ClosePrint(ApplicationClass lbl)
  1180. {
  1181. lblpro = lbl;
  1182. Thread close = new Thread(ClosePrintProcess);
  1183. close.Start();
  1184. }
  1185. static ApplicationClass lblpro;
  1186. private static void ClosePrintProcess()
  1187. {
  1188. try
  1189. {
  1190. lblpro.Quit();
  1191. }
  1192. catch (Exception)
  1193. {
  1194. }
  1195. }
  1196. public static bool connectState(string path)
  1197. {
  1198. return connectState(path, "a", "a");
  1199. }
  1200. /// <summary>
  1201. /// 连接远程共享文件夹
  1202. /// </summary>
  1203. /// <param name="path">远程共享文件夹的路径</param>
  1204. /// <param name="userName">用户名</param>
  1205. /// <param name="passWord">密码</param>
  1206. /// <returns></returns>
  1207. public static bool connectState(string path, string userName, string passWord)
  1208. {
  1209. bool Flag = false;
  1210. Process proc = new Process();
  1211. try
  1212. {
  1213. proc.StartInfo.FileName = "cmd.exe";
  1214. proc.StartInfo.UseShellExecute = false;
  1215. proc.StartInfo.RedirectStandardInput = true;
  1216. proc.StartInfo.RedirectStandardOutput = true;
  1217. proc.StartInfo.RedirectStandardError = true;
  1218. proc.StartInfo.CreateNoWindow = true;
  1219. proc.Start();
  1220. string dosLine = "net use " + path + " " + passWord + " /user:" + userName;
  1221. proc.StandardInput.WriteLine(dosLine);
  1222. proc.StandardInput.WriteLine("exit");
  1223. while (!proc.HasExited)
  1224. {
  1225. proc.WaitForExit(1);
  1226. }
  1227. string errormsg = proc.StandardError.ReadToEnd();
  1228. proc.StandardError.Close();
  1229. if (string.IsNullOrEmpty(errormsg))
  1230. {
  1231. Flag = true;
  1232. }
  1233. else
  1234. {
  1235. throw new Exception(errormsg);
  1236. }
  1237. }
  1238. catch (Exception ex)
  1239. {
  1240. LogManager.DoLog(ex.Message);
  1241. }
  1242. finally
  1243. {
  1244. proc.Close();
  1245. proc.Dispose();
  1246. }
  1247. return Flag;
  1248. }
  1249. /// <summary>
  1250. /// 向远程文件夹保存本地内容,或者从远程文件夹下载文件到本地
  1251. /// </summary>
  1252. /// <param name="src">要保存的文件的路径,如果保存文件到共享文件夹,这个路径就是本地文件路径如:@"D:\1.avi"</param>
  1253. /// <param name="dst">保存文件的路径,不含名称及扩展名</param>
  1254. /// <param name="fileName">保存文件的名称以及扩展名</param>
  1255. public static void Transport(string src, string dst, string fileName)
  1256. {
  1257. FileStream inFileStream = new FileStream(src, FileMode.Open);
  1258. if (!Directory.Exists(dst))
  1259. {
  1260. Directory.CreateDirectory(dst);
  1261. }
  1262. dst = dst + fileName;
  1263. FileStream outFileStream = new FileStream(dst, FileMode.OpenOrCreate);
  1264. byte[] buf = new byte[inFileStream.Length];
  1265. int byteCount;
  1266. while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0)
  1267. {
  1268. outFileStream.Write(buf, 0, byteCount);
  1269. }
  1270. inFileStream.Flush();
  1271. inFileStream.Close();
  1272. outFileStream.Flush();
  1273. outFileStream.Close();
  1274. }
  1275. /// <summary>
  1276. /// 取两个DataTable的交集,删除重复数据
  1277. /// </summary>
  1278. /// <param name="sourceDataTable"></param>
  1279. /// <param name="targetDataTable"></param>
  1280. /// <param name="primaryKey"></param>
  1281. /// <returns></returns>
  1282. public static DataTable DataTableMerge(DataTable sourceDataTable, DataTable targetDataTable, string primaryKey)
  1283. {
  1284. if (sourceDataTable != null || targetDataTable != null || !sourceDataTable.Equals(targetDataTable))
  1285. {
  1286. sourceDataTable.PrimaryKey = new DataColumn[] { sourceDataTable.Columns[primaryKey] };
  1287. DataTable dt = targetDataTable.Copy();
  1288. foreach (DataRow tRow in dt.Rows)
  1289. {
  1290. try
  1291. {
  1292. //拒绝自上次调用 System.Data.DataRow.AcceptChanges() 以来对该行进行的所有更改。
  1293. //因为行状态为DataRowState.Deleted时无法访问ItemArray的值
  1294. tRow.RejectChanges();
  1295. //在加载数据时关闭通知、索引维护和约束。
  1296. sourceDataTable.BeginLoadData();
  1297. //查找和更新特定行。如果找不到任何匹配行,则使用给定值创建新行。
  1298. DataRow temp = sourceDataTable.LoadDataRow(tRow.ItemArray, false);
  1299. sourceDataTable.EndLoadData();
  1300. sourceDataTable.Rows.Remove(temp);
  1301. }
  1302. catch
  1303. {
  1304. }
  1305. }
  1306. }
  1307. sourceDataTable.AcceptChanges();
  1308. return sourceDataTable;
  1309. }
  1310. //将DataRow[] 转换成DataTable
  1311. public static DataTable ToDataTable(DataRow[] rows)
  1312. {
  1313. if (rows == null || rows.Length == 0) return new DataTable();
  1314. DataTable tmp = rows[0].Table.Clone(); // 复制DataRow的表结构
  1315. foreach (DataRow row in rows)
  1316. tmp.Rows.Add(row.ItemArray); // 将DataRow添加到DataTable中
  1317. return tmp;
  1318. }
  1319. /// <summary>
  1320. ///
  1321. /// </summary>
  1322. /// <param name="form"></param>
  1323. /// <param name="InOrOut">True表示打开窗体,False表示关闭窗体</param>
  1324. public static void FormStepInOrOut(Form form, bool InOrOut)
  1325. {
  1326. if (InOrOut)
  1327. {
  1328. for (int iNum = 0; iNum <= 10; iNum++)
  1329. {
  1330. //变更窗体的不透明度
  1331. form.Opacity = 0.1 * iNum;
  1332. //暂停
  1333. System.Threading.Thread.Sleep(20);
  1334. }
  1335. }
  1336. else
  1337. {
  1338. for (int iNum = 10; iNum >= 0; iNum--)
  1339. {
  1340. //变更窗体的不透明度
  1341. form.Opacity = 0.1 * iNum;
  1342. //暂停
  1343. System.Threading.Thread.Sleep(20);
  1344. }
  1345. }
  1346. }
  1347. }
  1348. }