BaseUtil.cs 53 KB

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