BaseUtil.cs 51 KB

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