BaseUtil.cs 52 KB

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