BaseUtil.cs 54 KB

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