Utility.cs 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:Utility.cs
  5. * 2.功能描述:系统各种常用函数和方法类
  6. * 3.函数/方法列表:
  7. * A.IsNull:判断空值
  8. * B.EnableFormCloseBox:设定窗体的关闭按钮是否有效
  9. * C.IsValidDate:判断是否是合法的日期
  10. * D.ChineseToPinyinCap:根据输入汉字,转化成汉字首字母串
  11. * 编辑履历:
  12. * 作者 日期 版本 修改内容
  13. * 欧阳涛 2012/06/07 1.00 新建
  14. * 王文君 2012/07/10 1.00 修改 添加IsUnique方法
  15. * 欧阳涛 2012/07/19 1.00 增加了GetBytesByFilePath()方法,
  16. * 根据文件路径返回文件字节流
  17. *******************************************************************************/
  18. using System;
  19. using System.Data;
  20. using System.Drawing;
  21. using System.Drawing.Imaging;
  22. using System.IO;
  23. using System.Runtime.InteropServices;
  24. using System.Runtime.Serialization.Formatters.Binary;
  25. using System.Text;
  26. using System.Text.RegularExpressions;
  27. using System.Windows.Forms;
  28. using Dongke.IBOSS.PRD.Basics.BaseResources;
  29. namespace Dongke.IBOSS.PRD.Basics.Library
  30. {
  31. /// <summary>
  32. /// 系统各种常用函数和方法类
  33. /// </summary>
  34. public static class Utility
  35. {
  36. #region IsDirty
  37. public static bool IsDirty(DataSet dataSet)
  38. {
  39. if (dataSet == null)
  40. {
  41. return false;
  42. }
  43. foreach (DataTable table in dataSet.Tables)
  44. {
  45. foreach (DataRow row in table.Rows)
  46. {
  47. row.EndEdit();
  48. }
  49. }
  50. DataSet changes = dataSet.GetChanges();
  51. if (changes == null)
  52. {
  53. return false;
  54. }
  55. foreach (DataTable table in changes.Tables)
  56. {
  57. if (IsDirty(table))
  58. {
  59. return true;
  60. }
  61. }
  62. return false;
  63. }
  64. public static bool IsDirty(DataTable table)
  65. {
  66. if (table == null)
  67. {
  68. return false;
  69. }
  70. if (table.GetChanges(DataRowState.Added) != null)
  71. {
  72. return true;
  73. }
  74. if (table.GetChanges(DataRowState.Deleted) != null)
  75. {
  76. return true;
  77. }
  78. DataTable changes = table.GetChanges(DataRowState.Modified);
  79. if (changes != null)
  80. {
  81. foreach (DataRow row in changes.Rows)
  82. {
  83. if (IsDirty(row))
  84. {
  85. return true;
  86. }
  87. }
  88. }
  89. return false;
  90. }
  91. public static bool IsDirty(DataRow row)
  92. {
  93. if (row == null)
  94. {
  95. return false;
  96. }
  97. if (row.RowState == DataRowState.Added
  98. || row.RowState == DataRowState.Deleted)
  99. {
  100. return true;
  101. }
  102. if (row.RowState == DataRowState.Modified)
  103. {
  104. foreach (DataColumn column in row.Table.Columns)
  105. {
  106. if (column.ReadOnly)
  107. {
  108. continue;
  109. }
  110. if (!Utility.Compare(row[column, DataRowVersion.Original],
  111. row[column, DataRowVersion.Current]))
  112. {
  113. return true;
  114. }
  115. }
  116. }
  117. return false;
  118. }
  119. #endregion
  120. #region 申明API函数
  121. /// <summary>
  122. /// 写入INI文件
  123. /// </summary>
  124. /// <param name="section">节点名称[如[TypeName]]</param>
  125. /// <param name="key">键</param>
  126. /// <param name="val">值</param>
  127. /// <param name="filepath">文件路径</param>
  128. /// <returns></returns>
  129. [DllImport("kernel32")]
  130. private static extern long WritePrivateProfileString(string section, string key, string val, string filepath);
  131. /// <summary>
  132. /// 读取INI文件
  133. /// </summary>
  134. /// <param name="section">节点名称</param>
  135. /// <param name="key">键</param>
  136. /// <param name="def">值</param>
  137. /// <param name="retval">stringbulider对象</param>
  138. /// <param name="size">字节大小</param>
  139. /// <param name="filePath">文件路径</param>
  140. /// <returns></returns>
  141. [DllImport("kernel32")]
  142. private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath);
  143. #endregion
  144. #region 成员变量
  145. // 获取屏幕DPI用
  146. private static readonly Control _control;
  147. // 图形
  148. private static readonly Graphics _graphics;
  149. /// <summary>
  150. /// 毫米和像素的转换系数
  151. /// </summary>
  152. private const float MILLIMETER_PER_INCH = 25.4f;
  153. /// <summary>
  154. /// 磅和像素的转换系数
  155. /// </summary>
  156. private const float POINT_PER_INCH = 72f;
  157. /// <summary>
  158. /// 屏幕的DPI
  159. /// </summary>
  160. private static float SCREEN_DPI;
  161. #endregion
  162. #region 构造函数
  163. static Utility()
  164. {
  165. _control = new Control();
  166. _graphics = _control.CreateGraphics();
  167. SCREEN_DPI = _graphics.DpiX;
  168. }
  169. #endregion
  170. #region FindWindow()
  171. [DllImport("user32.dll")]
  172. public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  173. #endregion
  174. #region WakeupWindow()
  175. public static bool WakeupWindow(IntPtr hWnd)
  176. {
  177. if (hWnd == System.IntPtr.Zero)
  178. {
  179. return false;
  180. }
  181. if (Win32.IsIconic(hWnd) || !Win32.IsWindowVisible(hWnd))
  182. {
  183. Win32.ShowWindowAsync(hWnd, Win32.SW_RESTORE);
  184. }
  185. Win32.SetForegroundWindow(hWnd);
  186. return true;
  187. }
  188. #endregion
  189. #region 基本共通函数
  190. /// <summary>
  191. /// 空值判断
  192. /// </summary>
  193. /// <param name="value">判断对象</param>
  194. /// <returns>
  195. /// True:是空值
  196. /// False:不是空值
  197. /// </returns>
  198. public static bool IsNull(object value)
  199. {
  200. if (value == null || value == DBNull.Value)
  201. {
  202. return true;
  203. }
  204. return false;
  205. }
  206. /// <summary>
  207. /// 过滤sql语句中非法的字符,并替换
  208. /// </summary>
  209. /// <param name="value">替换前的sql语句</param>
  210. /// <returns>替换后的sql语句字符</returns>
  211. public static string SelectFilterLike(string value)
  212. {
  213. if (string.IsNullOrEmpty(value))
  214. {
  215. return string.Empty;
  216. }
  217. return value.Replace(@"'", @"''").Replace(@"[", @"[[]")
  218. .Replace(@"_", @"[_]").Replace(@"%", @"[%]").Replace(@"*", @"[*]");
  219. }
  220. /// <summary>
  221. /// 控制窗体的关闭按钮是否能用
  222. /// </summary>
  223. /// <param name="hWnd">窗体句柄</param>
  224. /// <param name="bEnable">True:可用 False:不可用</param>
  225. /// <returns>
  226. /// 设置成功与否
  227. /// </returns>
  228. public static bool EnableFormCloseBox(IntPtr hWnd, bool bEnable)
  229. {
  230. if (hWnd == System.IntPtr.Zero)
  231. {
  232. return false;
  233. }
  234. IntPtr hMenu = Win32.GetSystemMenu(hWnd, 0);
  235. if (hMenu == System.IntPtr.Zero)
  236. {
  237. return false;
  238. }
  239. if (bEnable)
  240. {
  241. Win32.EnableMenuItem(hMenu, Win32.SC_CLOSE, Win32.MF_ENABLED);
  242. }
  243. else
  244. {
  245. Win32.EnableMenuItem(hMenu, Win32.SC_CLOSE,
  246. Win32.MF_DISABLED | Win32.MF_GRAYED);
  247. }
  248. return true;
  249. }
  250. /// <summary>
  251. /// 判断是否是正确的日期格式
  252. /// </summary>
  253. /// <param name="strDateTime">被判断的日期</param>
  254. /// <param name="dateTime">返回被格式化的日期</param>
  255. /// <returns>
  256. /// True:日期格式
  257. /// False:不是日期格式
  258. /// </returns>
  259. public static bool IsValidDate(string strDateTime, out DateTime? dateTime)
  260. {
  261. DateTime returnDatetime;
  262. if (DateTime.TryParse(strDateTime, out returnDatetime))
  263. {
  264. dateTime = returnDatetime;
  265. return true;
  266. }
  267. else
  268. {
  269. dateTime = null;
  270. return false;
  271. }
  272. }
  273. /// <summary>
  274. /// 取得显示顺序的最大值
  275. /// </summary>
  276. /// <param name="table">数据表</param>
  277. /// <param name="maxValName">列名</param>
  278. /// <returns>
  279. /// 该数据表里的最大显示顺序
  280. /// </returns>
  281. public static int GetMaxValue(DataTable table, string maxValName)
  282. {
  283. if (table == null)
  284. {
  285. return -1;
  286. }
  287. if (table.Columns.Contains(maxValName))
  288. {
  289. DataRow[] rows = table.Select("0 <= " + maxValName, maxValName + " DESC");
  290. if (rows == null || rows.Length == 0)
  291. {
  292. return -1;
  293. }
  294. object value = rows[0][maxValName];
  295. if (value == null)
  296. {
  297. return -1;
  298. }
  299. int maxValue = int.Parse(value.ToString());
  300. if (maxValue == short.MaxValue)
  301. {
  302. maxValue--;
  303. }
  304. return maxValue;
  305. }
  306. else
  307. {
  308. return -1;
  309. //DataRow[] rows = table.Select("0 <= " + maxValName,maxValName + "DESC");
  310. //object value = rows[0][maxValName];
  311. //int maxValue = int.Parse(value.ToString());
  312. //return maxValue;
  313. }
  314. }
  315. /// <summary>
  316. /// 取得数据表里的最大编码值
  317. /// </summary>
  318. /// <param name="table">数据表</param>
  319. /// <param name="maxValName">列名</param>
  320. /// <returns>该数据表里的最大编码值(string类型)</returns>
  321. /// <remarks>
  322. /// 2012.07.10 余再伟 创建
  323. /// </remarks>
  324. public static string GetStringMaxValue(DataTable table, string maxValName)
  325. {
  326. // 表为空时候直接返回空值
  327. if (table == null)
  328. {
  329. return string.Empty;
  330. }
  331. // 表中包含该列名的时候返回该数据表的最大编码值
  332. if (table.Columns.Contains(maxValName))
  333. {
  334. DataRow[] rows = table.Select("'0' <= " + maxValName, maxValName + " DESC");
  335. if (rows == null || rows.Length == 0)
  336. {
  337. return string.Empty;
  338. }
  339. object value = rows[0][maxValName];
  340. if (value == null)
  341. {
  342. return string.Empty;
  343. }
  344. string maxValue = value.ToString();
  345. return maxValue;
  346. }
  347. else
  348. {
  349. return string.Empty;
  350. }
  351. }
  352. /// <summary>
  353. /// 校验值是否重复
  354. /// </summary>
  355. /// <param name="curCellValue">当前Column的值</param>
  356. /// <param name="curRow">当前行的行号</param>
  357. /// <param name="dataGridView">所在的DataGridView</param>
  358. /// <param name="uniqueValue">比较值</param>
  359. /// <returns>是否存在</returns>
  360. public static bool IsUnique(string curCellValue, int curRow, DataGridView dataGridView, string uniqueValue)
  361. {
  362. foreach (DataGridViewRow rowItem in dataGridView.Rows)
  363. {
  364. if (rowItem.Index == curRow)
  365. {
  366. continue;
  367. }
  368. // 字典名称
  369. if (rowItem.Cells[uniqueValue].Value != null)
  370. {
  371. if (rowItem.Cells[uniqueValue].Value.ToString().Trim().
  372. Equals(curCellValue.Trim()))
  373. {
  374. return false;
  375. }
  376. }
  377. }
  378. return true;
  379. }
  380. /// <summary>
  381. /// 根据输入的汉字,返回所有汉字拼音的首字母串
  382. /// </summary>
  383. /// <param name="chineseString">输入的汉字</param>
  384. /// <returns>汉字拼音的首字母串</returns>
  385. public static string ChineseToPinyinCap(string chineseString)
  386. {
  387. string pinyinCapString = ""; // 转换后的拼音首字母字符串
  388. // 每个汉字存储需要两个字节
  389. byte[] ZW = new byte[2];
  390. long ChineseStr_int;
  391. string CharStr;
  392. string ChinaStr = "";
  393. chineseString = ToDBC(chineseString);
  394. for (int i = 0; i <= chineseString.Length - 1; i++)
  395. {
  396. // 取出一个汉字
  397. CharStr = chineseString.Substring(i, 1).ToString();
  398. // 得到汉字的字节表示
  399. ZW = System.Text.Encoding.Default.GetBytes(CharStr);
  400. // 得到汉字符的字节数组
  401. if (ZW.Length == 2)
  402. {
  403. int i1 = (short)(ZW[0]);
  404. int i2 = (short)(ZW[1]);
  405. ChineseStr_int = i1 * 256 + i2;
  406. #region 参数
  407. // table of the constant list
  408. // 'A';      //45217..45252
  409. // 'B';      //45253..45760
  410. // 'C';      //45761..46317
  411. // 'D';      //46318..46825
  412. // 'E';      //46826..47009
  413. // 'F';      //47010..47296
  414. // 'G';      //47297..47613
  415. // 'H';      //47614..48118
  416. // 'J';      //48119..49061
  417. // 'K';      //49062..49323
  418. // 'L';      //49324..49895
  419. // 'M';      //49896..50370
  420. // 'N';      //50371..50613
  421. // 'O';      //50614..50621
  422. // 'P';      //50622..50905
  423. // 'Q';      //50906..51386
  424. // 'R';      //51387..51445
  425. // 'S';      //51446..52217
  426. // 'T';      //52218..52697
  427. //没有U,V
  428. // 'W';      //52698..52979
  429. // 'X';      //52980..53640
  430. // 'Y';      //53689..54480
  431. // 'Z';      //54481..55289
  432. #endregion
  433. if ((ChineseStr_int >= 45217) && (ChineseStr_int <= 45252))
  434. {
  435. ChinaStr = "A";
  436. }
  437. else if ((ChineseStr_int >= 45253) && (ChineseStr_int <= 45760))
  438. {
  439. ChinaStr = "B";
  440. }
  441. else if ((ChineseStr_int >= 45761) && (ChineseStr_int <= 46317))
  442. {
  443. ChinaStr = "C";
  444. }
  445. else if ((ChineseStr_int >= 46318) && (ChineseStr_int <= 46825))
  446. {
  447. ChinaStr = "D";
  448. }
  449. else if ((ChineseStr_int >= 46826) && (ChineseStr_int <= 47009))
  450. {
  451. ChinaStr = "E";
  452. }
  453. else if ((ChineseStr_int >= 47010) && (ChineseStr_int <= 47296))
  454. {
  455. ChinaStr = "F";
  456. }
  457. else if ((ChineseStr_int >= 47297) && (ChineseStr_int <= 47613))
  458. {
  459. ChinaStr = "G";
  460. }
  461. else if ((ChineseStr_int >= 47614) && (ChineseStr_int <= 48118))
  462. {
  463. ChinaStr = "H";
  464. }
  465. else if ((ChineseStr_int >= 48119) && (ChineseStr_int <= 49061))
  466. {
  467. ChinaStr = "J";
  468. }
  469. else if ((ChineseStr_int >= 49062) && (ChineseStr_int <= 49323))
  470. {
  471. ChinaStr = "K";
  472. }
  473. else if ((ChineseStr_int >= 49324) && (ChineseStr_int <= 49895))
  474. {
  475. ChinaStr = "L";
  476. }
  477. else if ((ChineseStr_int >= 49896) && (ChineseStr_int <= 50370))
  478. {
  479. ChinaStr = "M";
  480. }
  481. else if ((ChineseStr_int >= 50371) && (ChineseStr_int <= 50613))
  482. {
  483. ChinaStr = "N";
  484. }
  485. else if ((ChineseStr_int >= 50614) && (ChineseStr_int <= 50621))
  486. {
  487. ChinaStr = "O";
  488. }
  489. else if ((ChineseStr_int >= 50622) && (ChineseStr_int <= 50905))
  490. {
  491. ChinaStr = "P";
  492. }
  493. else if ((ChineseStr_int >= 50906) && (ChineseStr_int <= 51386))
  494. {
  495. ChinaStr = "Q";
  496. }
  497. else if ((ChineseStr_int >= 51387) && (ChineseStr_int <= 51445))
  498. {
  499. ChinaStr = "R";
  500. }
  501. else if ((ChineseStr_int >= 51446) && (ChineseStr_int <= 52217))
  502. {
  503. ChinaStr = "S";
  504. }
  505. else if ((ChineseStr_int >= 52218) && (ChineseStr_int <= 52697))
  506. {
  507. ChinaStr = "T";
  508. }
  509. else if ((ChineseStr_int >= 52698) && (ChineseStr_int <= 52979))
  510. {
  511. ChinaStr = "W";
  512. }
  513. else if ((ChineseStr_int >= 52980) && (ChineseStr_int <= 53640))
  514. {
  515. ChinaStr = "X";
  516. }
  517. else if ((ChineseStr_int >= 53689) && (ChineseStr_int <= 54480))
  518. {
  519. ChinaStr = "Y";
  520. }
  521. else if ((ChineseStr_int >= 54481) && (ChineseStr_int <= 55289))
  522. {
  523. ChinaStr = "Z";
  524. }
  525. else
  526. {
  527. ChinaStr = CharStr;
  528. }
  529. pinyinCapString = pinyinCapString + ChinaStr;
  530. }
  531. else
  532. {
  533. pinyinCapString = pinyinCapString + CharStr.ToUpper();
  534. }
  535. }
  536. return pinyinCapString;
  537. }
  538. #endregion
  539. #region 半角全角转换
  540. /// <summary>
  541. /// 转半角的函数(DBC)
  542. /// 其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
  543. /// 全角空格为12288,半角空格为32
  544. /// </summary>
  545. /// <param name="inputString">任意字符串</param>
  546. /// <returns>半角字符串</returns>
  547. public static string ToDBC(string inputString)
  548. {
  549. char[] c = inputString.ToCharArray();
  550. for (int i = 0; i < c.Length; i++)
  551. {
  552. if (c[i] == 12288)
  553. {
  554. c[i] = (char)32;
  555. continue;
  556. }
  557. if (c[i] > 65280 && c[i] < 65375)
  558. {
  559. c[i] = (char)(c[i] - 65248);
  560. }
  561. }
  562. return new String(c);
  563. }
  564. /// <summary>
  565. /// 转全角的函数(SBC)
  566. /// 其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
  567. /// 全角空格为12288,半角空格为32
  568. /// </summary>
  569. /// <param name="inputString">任意字符串</param>
  570. /// <returns>全角字符串</returns>
  571. public static string ToSBC(string inputString)
  572. {
  573. // 半角转全角:
  574. char[] c = inputString.ToCharArray();
  575. for (int i = 0; i < c.Length; i++)
  576. {
  577. if (c[i] == 32)
  578. {
  579. c[i] = (char)12288;
  580. continue;
  581. }
  582. if (c[i] < 127)
  583. c[i] = (char)(c[i] + 65248);
  584. }
  585. return new String(c);
  586. }
  587. #endregion
  588. #region 正则判断数据合法性
  589. /// <summary>
  590. /// 验证邮箱格式
  591. /// </summary>
  592. /// <param name="email">要验证的邮箱地址</param>
  593. /// <returns>
  594. /// True:邮箱格式正确
  595. /// False:邮箱格式错误
  596. /// </returns>
  597. public static bool IsValidEmail(string email)
  598. {
  599. if (!Regex.IsMatch(email,
  600. @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$"))
  601. {
  602. return false;
  603. }
  604. else
  605. {
  606. return true;
  607. }
  608. }
  609. ///// <summary>
  610. ///// 验证身份证输入是否正确
  611. ///// </summary>
  612. ///// <param name="IdCard">要验证的身份证号</param>
  613. ///// <returns>
  614. ///// True:身份证格式正确
  615. ///// False:身份证格式错误
  616. ///// </returns>
  617. //public static bool IsIDcard(string IdCard)
  618. //{
  619. // bool result = false;
  620. // if (IdCard.Length <= 15)
  621. // {
  622. // result = Regex.IsMatch(IdCard, Constant.IDCARD_STRING15);
  623. // }
  624. // else
  625. // {
  626. // result = Regex.IsMatch(IdCard, Constant.IDCARD_STRING);
  627. // }
  628. // return result;
  629. //}
  630. /// <summary>
  631. /// 判断身份证号是否合法(对外部公开)
  632. /// </summary>
  633. /// <param name="strCardID">身份证号(旧/新)</param>
  634. /// <returns>false:不合法 true:合法</returns>
  635. public static bool IsIDcard(string strCardID)
  636. {
  637. strCardID = strCardID.ToLower();
  638. // 验证身份证的位数
  639. string strRegex = @"(\d{15}$)|(\d{17}(\d|x)$)";
  640. if (!Regex.IsMatch(strCardID, strRegex))
  641. {
  642. return false;
  643. }
  644. // 身份证数据有效性验证
  645. string strNewCardID = strCardID;
  646. if (strCardID.Length == 15)
  647. {
  648. strNewCardID = Convert15To18(strCardID);
  649. }
  650. if (!CheckCardIDInfo(strNewCardID))
  651. {
  652. return false;
  653. }
  654. return true;
  655. }
  656. /// <summary>
  657. /// 判断身份证号是否合法
  658. /// </summary>
  659. /// <param name="strCardID">新身份证号</param>
  660. /// <returns>false:不合法 true:合法</returns>
  661. private static bool CheckCardIDInfo(string strCardID)
  662. {
  663. string[] aCity = new string[] { null, null, null, null, null, null, null, null, null,
  664. null, null, "北京", "天津 ", "河北", "山西", "内蒙古", null, null, null, null,
  665. null, "辽宁", "吉林", "黑龙江", null, null, null, null, null, null, null, "上海",
  666. "江苏", "浙江", "安微", "福建", "江西", "山东", null, null, null, "河南",
  667. "湖北", "湖南", "广东", "广西", "海南", null, null, null, "重庆", "四川",
  668. "贵州", "云南", "西藏", null, null, null, null, null, null, "陕西", "甘肃",
  669. "青海", "宁夏", "新疆", null, null, null, null, null, "台湾", null, null, null,
  670. null, null, null, null, null, null, "香港", "澳门", null, null, null, null, null,
  671. null, null, null, "国外" };
  672. strCardID = strCardID.ToLower();
  673. strCardID = strCardID.Replace("x", "a");
  674. try
  675. {
  676. if (aCity[int.Parse(strCardID.Substring(0, 2))] == null)
  677. {
  678. return false;
  679. }
  680. DateTime.Parse(strCardID.Substring(6, 4) + "-"
  681. + strCardID.Substring(10, 2) + "-"
  682. + strCardID.Substring(12, 2));
  683. }
  684. catch
  685. {
  686. return false;
  687. }
  688. double iSum = 0;
  689. for (int i = 17; i >= 0; i--)
  690. {
  691. iSum += (System.Math.Pow(2, i) % 11) * int.Parse(strCardID[17 - i].ToString(),
  692. System.Globalization.NumberStyles.HexNumber);
  693. }
  694. if (iSum % 11 != 1)
  695. {
  696. return false;
  697. }
  698. return true;
  699. }
  700. /// <summary>
  701. /// 从老身份证升位到新身份证
  702. /// </summary>
  703. /// <param name="strCardID">老身份证号码</param>
  704. /// <returns>新身份证号码</returns>
  705. private static string Convert15To18(string strCardID)
  706. {
  707. int intN = 0;
  708. // 加权因子常数
  709. int[] intW = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
  710. // 校验码常数
  711. string strLastCode = "10X98765432";
  712. // 新身份证号
  713. string strNewCardID;
  714. strNewCardID = strCardID.Substring(0, 6);
  715. // 填在第6位及第7位上填上‘1’,‘9’两个数字
  716. strNewCardID += "19";
  717. strNewCardID += strCardID.Substring(6, 9);
  718. // 进行加权求和
  719. for (int i = 0; i < 17; i++)
  720. {
  721. intN += int.Parse(strNewCardID.Substring(i, 1)) * intW[i];
  722. }
  723. // 取模运算,得到模值
  724. int intY = intN % 11;
  725. // 从strLastCode中取得以模为索引号的值,加到身份证的最后一位,即为新身份证号。
  726. strNewCardID += strLastCode.Substring(intY, 1);
  727. return strNewCardID;
  728. }
  729. /// <summary>
  730. /// 验证车牌号输入是否正确
  731. /// </summary>
  732. /// <param name="PlateNumber">要验证的车牌号</param>
  733. /// <returns>
  734. /// True:车牌号格式正确
  735. /// False:车牌号格式错误
  736. /// </returns>
  737. public static bool IsPlateNumber(string PlateNumber)
  738. {
  739. return Regex.IsMatch(PlateNumber, Constant.REGEX_PLATENUMBER_STRING);
  740. }
  741. /// <summary>
  742. /// 验证车架号输入是否正确
  743. /// </summary>
  744. /// <param name="PlateNumber">要验证的车架号</param>
  745. /// <returns>
  746. /// True:车架号格式正确
  747. /// False:车架号格式错误
  748. /// </returns>
  749. public static bool IsVehicleNumber(string VehicleNumber)
  750. {
  751. return Regex.IsMatch(VehicleNumber, Constant.REGEX_VEHICLENUM_STRING);
  752. }
  753. /// <summary>
  754. /// 验证公司网址输入格式是否正确
  755. /// </summary>
  756. /// <param name="http">要验证的网址</param>
  757. /// True:网址格式正确
  758. /// False:网址格式错误
  759. /// <returns></returns>
  760. public static bool IsValidHttp(string http)
  761. {
  762. if (!Regex.IsMatch(http, Constant.REGEX_HTTP_STRING))
  763. {
  764. return false;
  765. }
  766. else
  767. {
  768. return true;
  769. }
  770. }
  771. /// <summary>
  772. /// 验证输入字符串是否为数字类型(包括带小数的数字)
  773. /// </summary>
  774. /// <param name="value">要验证的字符串</param>
  775. /// <returns>
  776. /// True:字符串是数字
  777. /// False:字符串不是数字
  778. /// </returns>
  779. public static bool IsNumeric(string value)
  780. {
  781. return System.Text.RegularExpressions.Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");
  782. }
  783. /// <summary>
  784. /// 验证输入字符串是否为合法时间(HH:mm或者HH:mm:ss)
  785. /// </summary>
  786. /// <param name="value">要验证的字符串</param>
  787. /// <returns>
  788. /// True:字符串是合法时间(HH:mm或者HH:mm:ss)
  789. /// False:字符串不是合法时间(HH:mm或者HH:mm:ss)
  790. /// </returns>
  791. public static bool IsTime(string value)
  792. {
  793. return System.Text.RegularExpressions.Regex.IsMatch(value, @"^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$");
  794. }
  795. /// <summary>
  796. /// 验证输入字符串是否为合法日期(yyyy-MM-dd)
  797. /// </summary>
  798. /// <param name="value">要验证的字符串</param>
  799. /// <returns>
  800. /// True:字符串是合法日期(yyyy-MM-dd)
  801. /// False:字符串不是合法日期(yyyy-MM-dd)
  802. /// </returns>
  803. public static bool IsDate(string value)
  804. {
  805. return System.Text.RegularExpressions.Regex.IsMatch(value,
  806. @"^((?:19|20)\d\d)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$");
  807. }
  808. #endregion
  809. #region 二进制与文件之间转换
  810. /// <summary>
  811. /// 将文件转换成字节流
  812. /// </summary>
  813. /// <param name="filePath">文件的绝对路径</param>
  814. /// <returns>
  815. /// 文件的字节流
  816. /// </returns>
  817. public static byte[] GetBytesByFilePath(string filePath)
  818. {
  819. // 传入的值非法的情况下,返回NULL
  820. if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
  821. {
  822. return null;
  823. }
  824. // 读取文件到字节流中
  825. FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
  826. BinaryReader binaryReader = new BinaryReader(fileStream);
  827. byte[] fileBytes = binaryReader.ReadBytes((int)fileStream.Length);
  828. fileStream.Flush();
  829. fileStream.Close();
  830. return fileBytes;
  831. }
  832. /// <summary>
  833. /// 将二进制转换成文件
  834. /// </summary>
  835. /// <param name="filePath">文件存放的全路径</param>
  836. /// <param name="fileByte">二进制文件</param>
  837. /// <returns>
  838. /// True:保存成功
  839. /// False:保存失败
  840. /// </returns>
  841. public static bool BinaryToFile(string filePath, byte[] fileByte)
  842. {
  843. if (string.IsNullOrEmpty(filePath) || fileByte == null)
  844. {
  845. return false;
  846. }
  847. try
  848. {
  849. FileStream filestream = File.Create(filePath, fileByte.Length);
  850. filestream.Write(fileByte, 0, fileByte.Length);
  851. return true;
  852. }
  853. catch (Exception ex)
  854. {
  855. throw ex;
  856. }
  857. }
  858. #endregion
  859. #region 图片与二进制之间转换
  860. /// <summary>
  861. /// 将图片文件序列化成二进制流
  862. /// </summary>
  863. /// <param name="imagePath">文件路径</param>
  864. /// <returns>二进制流文件</returns>
  865. public static MemoryStream ImageToBinary(string imagePath)
  866. {
  867. if (string.IsNullOrEmpty(imagePath) || !File.Exists(imagePath))
  868. {
  869. return null;
  870. }
  871. try
  872. {
  873. // 将图片文件序列化成二进制流文件
  874. Image image = new Bitmap(imagePath);
  875. MemoryStream memorystream = new MemoryStream();
  876. BinaryFormatter formatter = new BinaryFormatter();
  877. formatter.Serialize(memorystream, image);
  878. return memorystream;
  879. }
  880. catch (Exception ex)
  881. {
  882. throw ex;
  883. }
  884. }
  885. /// <summary>
  886. /// 将二进制流反序列化成图片文件
  887. /// </summary>
  888. /// <param name="memoryStream">二进制流文件</param>
  889. /// <returns>图片文件</returns>
  890. public static Image StreamToImage(MemoryStream memoryStream)
  891. {
  892. if (memoryStream == null)
  893. {
  894. return null;
  895. }
  896. try
  897. {
  898. // 将流文件反序列化成图片文件
  899. memoryStream.Position = 0;
  900. BinaryFormatter formatter = new BinaryFormatter();
  901. return (Image)formatter.Deserialize(memoryStream);
  902. }
  903. catch (Exception ex)
  904. {
  905. throw ex;
  906. }
  907. }
  908. /// <summary>
  909. /// 将二进制转换成图片文件
  910. /// </summary>
  911. /// <param name="imageBinary">二进制</param>
  912. /// <returns>image</returns>
  913. public static Image BinaryToImage(byte[] imageBinary)
  914. {
  915. if (imageBinary == null)
  916. {
  917. return null;
  918. }
  919. try
  920. {
  921. MemoryStream memoryStream = new MemoryStream(imageBinary);
  922. return Image.FromStream(memoryStream);
  923. }
  924. catch (Exception ex)
  925. {
  926. throw ex;
  927. }
  928. }
  929. /// <summary>
  930. /// 根据Image对象转换成二进制
  931. /// </summary>
  932. /// <param name="image">image对象</param>
  933. /// <returns>
  934. /// 二进制数组
  935. /// </returns>
  936. public static byte[] ImageToByteArray(Image image)
  937. {
  938. if (image == null)
  939. {
  940. return null;
  941. }
  942. try
  943. {
  944. MemoryStream memoryStream = new MemoryStream();
  945. image.Save(memoryStream, ImageFormat.Jpeg);
  946. return memoryStream.ToArray();
  947. }
  948. catch (Exception ex)
  949. {
  950. throw ex;
  951. }
  952. }
  953. #endregion
  954. #region Compare比较函数
  955. [Flags]
  956. public enum CompareType
  957. {
  958. None = 0,
  959. // DBNull.Value值与null值视为同一值
  960. DBNullAsNull = 1,
  961. // string.Empty值null值视为同一值
  962. EmptyAsNull = 2,
  963. // 全部都视为同一值
  964. All = DBNullAsNull | EmptyAsNull
  965. }
  966. /// <summary>
  967. /// 两个值比较函数
  968. /// </summary>
  969. /// <param name="value1">值1</param>
  970. /// <param name="value2">值2</param>
  971. /// <returns>
  972. /// True:两个值相等的情况
  973. /// False:两个值相等的以外情况
  974. /// </returns>
  975. public static bool Compare(object value1, object value2)
  976. {
  977. return Compare(value1, value2, CompareType.None);
  978. }
  979. /// <summary>
  980. /// 两个值比较函数
  981. /// </summary>
  982. /// <param name="value1">值1</param>
  983. /// <param name="value2">值2</param>
  984. /// <param name="compareFlag">比较方式</param>
  985. /// <returns>
  986. /// True:两个值相等的情况
  987. /// False:两个值相等的以外情况
  988. /// </returns>
  989. public static bool Compare(object value1, object value2, CompareType compareFlag)
  990. {
  991. bool isNull1 = (value1 == null);
  992. bool isNull2 = (value2 == null);
  993. if ((compareFlag & CompareType.DBNullAsNull) == CompareType.DBNullAsNull)
  994. {
  995. if (value1 == DBNull.Value)
  996. {
  997. isNull1 = true;
  998. }
  999. if (value2 == DBNull.Value)
  1000. {
  1001. isNull2 = true;
  1002. }
  1003. }
  1004. if ((compareFlag & CompareType.EmptyAsNull) == CompareType.EmptyAsNull)
  1005. {
  1006. if (string.Empty.Equals(value1))
  1007. {
  1008. isNull1 = true;
  1009. }
  1010. if (string.Empty.Equals(value2))
  1011. {
  1012. isNull2 = true;
  1013. }
  1014. }
  1015. if (isNull1 && isNull2)
  1016. {
  1017. return true;
  1018. }
  1019. if (value1 == null || value2 == null)
  1020. {
  1021. return false;
  1022. }
  1023. else
  1024. {
  1025. return CompareSub(value1, value2);
  1026. }
  1027. }
  1028. /// <summary>
  1029. /// 两个值比较函数
  1030. /// </summary>
  1031. /// <param name="value1">值1</param>
  1032. /// <param name="value2">值2</param>
  1033. /// <returns>
  1034. /// True:两个值相等的情况
  1035. /// False:两个值相等的以外情况
  1036. /// </returns>
  1037. private static bool CompareSub(object value1, object value2)
  1038. {
  1039. // 数据类型不一样的情况下,返回false
  1040. if (value1.GetType().Equals(value2.GetType()) == false)
  1041. {
  1042. return false;
  1043. }
  1044. // 枚举比较的情况,不能使用Equals,需单独写方法比较
  1045. if (value1 is System.Collections.IEnumerable
  1046. && value2 is System.Collections.IEnumerable)
  1047. {
  1048. return CompareSub((System.Collections.IEnumerable)value1,
  1049. (System.Collections.IEnumerable)value2);
  1050. }
  1051. else
  1052. {
  1053. return value1.Equals(value2);
  1054. }
  1055. }
  1056. /// <summary>
  1057. /// 两个枚举类型的比较
  1058. /// </summary>
  1059. /// <param name="enumerable1">值1</param>
  1060. /// <param name="enumerable2">值2</param>
  1061. /// <returns>
  1062. /// True:两个值相等的情况
  1063. /// False:两个值相等的以外情况
  1064. /// </returns>
  1065. private static bool CompareSub(System.Collections.IEnumerable enumerable1,
  1066. System.Collections.IEnumerable enumerable2)
  1067. {
  1068. System.Collections.IEnumerator e1 = enumerable1.GetEnumerator();
  1069. System.Collections.IEnumerator e2 = enumerable2.GetEnumerator();
  1070. while (e1.MoveNext())
  1071. {
  1072. if (!e2.MoveNext())
  1073. {
  1074. return false;
  1075. }
  1076. if (!e1.Current.Equals(e2.Current))
  1077. {
  1078. return false;
  1079. }
  1080. }
  1081. return !e2.MoveNext();
  1082. }
  1083. #endregion
  1084. #region 校验数字
  1085. /// <summary>
  1086. /// 校验数据是否符合要求
  1087. /// </summary>
  1088. /// <param name="number">待验证的参数</param>
  1089. /// <param name="numberName">待验证的参数名称</param>
  1090. /// <param name="type">校验的种类</param>
  1091. /// <returns>错误信息</returns>
  1092. /// <remarks>
  1093. /// 2012.10.8 周兴 新建
  1094. /// </remarks>
  1095. public static string IsValidNumber(string number, string numberName, byte type)
  1096. {
  1097. try
  1098. {
  1099. // 如果第一位是-,表面可能是负号,去掉之后进行校验
  1100. if (!string.IsNullOrEmpty(number))
  1101. {
  1102. if (number.IndexOf("-") == 0)
  1103. {
  1104. number = number.Substring(1);
  1105. }
  1106. }
  1107. string errorMsg = string.Empty;
  1108. string maxNumber = string.Empty;
  1109. string regex = string.Empty;
  1110. // 验证Numeric(16,6)
  1111. if (type == 1)
  1112. {
  1113. regex = @"^(0|([1-9][0-9]{0,9}))(\.[0-9]{0,6})?$";
  1114. maxNumber = "9999999999.999999";
  1115. }
  1116. // 验证Numeric(14,6)
  1117. else if (type == 2)
  1118. {
  1119. regex = @"^(0|([1-9][0-9]{0,7}))(\.[0-9]{0,6})?$";
  1120. maxNumber = "99999999.999999";
  1121. }
  1122. // 验证Numeric(12,6)
  1123. else if (type == 3)
  1124. {
  1125. regex = @"^(0|([1-9][0-9]{0,5}))(\.[0-9]{0,6})?$";
  1126. maxNumber = "999999.999999";
  1127. }
  1128. // 验证Numeric(5,2)
  1129. else if (type == 4)
  1130. {
  1131. regex = @"^(0|([1-9][0-9]{0,2}))(\.[0-9]{0,2})?$";
  1132. maxNumber = "999.99";
  1133. }
  1134. // 验证Numeric(5,2)(采购返利方式的返点率)
  1135. else if (type == 5)
  1136. {
  1137. regex = @"^((0|([1-9][0-9]{0,1}))(\.[0-9]{0,2})?|(100(\.0{0,2})?))$";
  1138. maxNumber = "100.00";
  1139. }
  1140. // 验证Numeric(6,2)(系统参数舍零金额限制最大值1000.00)
  1141. else if (type == 6)
  1142. {
  1143. regex = @"^((0|([1-9][0-9]{0,2}))(\.[0-9]{0,2})?|(1000(\.0{0,2})?))$";
  1144. maxNumber = "1000.00";
  1145. }
  1146. // 验证Numeric(12,2)(任务策略中任务金额限制)
  1147. else if (type == 7)
  1148. {
  1149. regex = @"^(0|([1-9][0-9]{0,9}))(\.[0-9]{0,2})?$";
  1150. maxNumber = "9999999999.99";
  1151. }
  1152. // 验证Numeric(6,2)(商品编码中费用分摊系数)
  1153. else if (type == 8)
  1154. {
  1155. regex = @"^(0|([1-9][0-9]{0,3}))(\.[0-9]{0,2})?$";
  1156. maxNumber = "9999.99";
  1157. }
  1158. // 验证Numeric(9,2)(CA员工业绩岗位考核工资基数)
  1159. else if (type == 9)
  1160. {
  1161. regex = @"^(0|([1-9][0-9]{0,6}))(\.[0-9]{0,2})?$";
  1162. maxNumber = "9999999.99";
  1163. }
  1164. // 验证Numeric(3,2)(CA员工业绩提成比率)
  1165. else if (type == 10)
  1166. {
  1167. regex = @"^(0|([0-9]))(\.[0-9]{0,2})?$";
  1168. maxNumber = "9.99";
  1169. }
  1170. // 验证Numeric(3,1)(CB员工业绩提成比率)
  1171. else if (type == 11)
  1172. {
  1173. regex = @"^(0|([1-9][0-9]{0,1}))(\.[0-9]{0,1})?$";
  1174. maxNumber = "99.9";
  1175. }
  1176. // 验证Numeric(7,2)(店长提成策略出库净额递增额)
  1177. else if (type == 12)
  1178. {
  1179. regex = @"^(0|([1-9][0-9]{0,4}))(\.[0-9]{0,2})?$";
  1180. maxNumber = "99999.99";
  1181. }
  1182. if (!Regex.IsMatch(number, regex))
  1183. {
  1184. //errorMsg = string.Format(Messages.MESSAGE_W004, numberName, maxNumber);
  1185. }
  1186. return errorMsg;
  1187. }
  1188. catch (Exception ex)
  1189. {
  1190. throw ex;
  1191. }
  1192. }
  1193. #endregion
  1194. #region 校验电话格式
  1195. /// <summary>
  1196. /// 校验电话是否符合格式
  1197. /// </summary>
  1198. /// <param name="telephone"></param>
  1199. /// <returns></returns>
  1200. public static bool IsValidTelephone(string telephone)
  1201. {
  1202. try
  1203. {
  1204. /**
  1205. * 手机号码
  1206. * 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
  1207. * 联通:130,131,132,152,155,156,185,186
  1208. * 电信:133,1349,153,180,181,189
  1209. */
  1210. string mobile = "^1(3[0-9]|5[0-35-9]|8[012345-9])\\d{8}$";
  1211. /**
  1212. * 大陆地区固话及小灵通 区号和电话之间没有-
  1213. * 区号:010,020,021,022,023,024,025,027,028,029
  1214. * 号码:七位或八位
  1215. */
  1216. string phs = "^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
  1217. if (!Regex.IsMatch(telephone, mobile) && !Regex.IsMatch(telephone, phs))
  1218. {
  1219. return false;
  1220. }
  1221. return true;
  1222. }
  1223. catch (Exception ex)
  1224. {
  1225. throw ex;
  1226. }
  1227. }
  1228. #endregion
  1229. #region 验证规格是否符合计算面积格式
  1230. /// <summary>
  1231. /// 验证规格是否符合计算面积格式
  1232. /// </summary>
  1233. /// <param name="specification">规格</param>
  1234. /// <returns></returns>
  1235. public static bool IsValidAcreage(string specification)
  1236. {
  1237. try
  1238. {
  1239. string regex = @"^\d+\*\d+$";
  1240. if (Regex.IsMatch(specification, regex))
  1241. {
  1242. return true;
  1243. }
  1244. return false;
  1245. }
  1246. catch (Exception ex)
  1247. {
  1248. throw ex;
  1249. }
  1250. }
  1251. #endregion
  1252. #region 验证规格是否符合计算体积格式
  1253. /// <summary>
  1254. /// 验证规格是否符合计算体积格式
  1255. /// </summary>
  1256. /// <param name="specification">规格</param>
  1257. /// <returns></returns>
  1258. public static bool IsValidVolume(string specification)
  1259. {
  1260. try
  1261. {
  1262. string regex = @"^\d+\*\d+\*\d+$";
  1263. if (Regex.IsMatch(specification, regex))
  1264. {
  1265. return true;
  1266. }
  1267. return false;
  1268. }
  1269. catch (Exception ex)
  1270. {
  1271. throw ex;
  1272. }
  1273. }
  1274. #endregion
  1275. #region 长度与像素之间转换
  1276. /// <summary>
  1277. /// 1/100 英寸转换为毫米
  1278. /// </summary>
  1279. /// <remarks>
  1280. /// 表示倍率 100%
  1281. /// </remarks>
  1282. /// <param name="pixel">1/100 英寸</param>
  1283. /// <returns>毫米</returns>
  1284. public static float Inch100ToMillimeter(int inch100)
  1285. {
  1286. float mm = inch100 * MILLIMETER_PER_INCH / 100;
  1287. return mm;
  1288. }
  1289. /// <summary>
  1290. /// 1/100 英寸转换为毫米
  1291. /// </summary>
  1292. /// <remarks>
  1293. /// 表示倍率 100%
  1294. /// </remarks>
  1295. /// <param name="pixel">1/100 英寸</param>
  1296. /// <returns>毫米</returns>
  1297. public static float Inch100ToMillimeter(float inch100)
  1298. {
  1299. float mm = inch100 * MILLIMETER_PER_INCH / 100;
  1300. return mm;
  1301. }
  1302. /// <summary>
  1303. /// 磅转换为毫米
  1304. /// </summary>
  1305. /// <remarks>
  1306. /// 表示倍率 100%
  1307. /// </remarks>
  1308. /// <param name="point">磅</param>
  1309. /// <returns>毫米</returns>
  1310. public static float PointToMillimeter(float point)
  1311. {
  1312. float mm = point / POINT_PER_INCH * MILLIMETER_PER_INCH;
  1313. return mm;
  1314. }
  1315. /// <summary>
  1316. /// 磅转换为像素
  1317. /// </summary>
  1318. /// <remarks>
  1319. /// 表示倍率 100%
  1320. /// </remarks>
  1321. /// <param name="point">磅</param>
  1322. /// <returns>像素</returns>
  1323. public static int PointToPixel(float point)
  1324. {
  1325. decimal pixel = System.Convert.ToDecimal(point) /
  1326. System.Convert.ToDecimal(POINT_PER_INCH) *
  1327. System.Convert.ToDecimal(SCREEN_DPI);
  1328. return System.Convert.ToInt32(Math.Round(pixel, 0));
  1329. }
  1330. /// <summary>
  1331. /// 毫米转换为1/100 英寸
  1332. /// </summary>
  1333. /// <remarks>
  1334. /// 表示倍率 100%
  1335. /// </remarks>
  1336. /// <param name="mm">毫米</param>
  1337. /// <returns>1/100 英寸</returns>
  1338. public static int MillimeterToInch100(float mm)
  1339. {
  1340. float inch100 = mm / MILLIMETER_PER_INCH * 100;
  1341. return System.Convert.ToInt32(Math.Round(inch100, 0));
  1342. }
  1343. /// <summary>
  1344. /// 毫米转换为1/100 英寸
  1345. /// </summary>
  1346. /// <remarks>
  1347. /// 表示倍率 100%
  1348. /// </remarks>
  1349. /// <param name="mm">毫米</param>
  1350. /// <returns>1/100 英寸</returns>
  1351. public static float MillimeterToInch100Single(float mm)
  1352. {
  1353. float inch100 = mm / MILLIMETER_PER_INCH * 100;
  1354. return inch100;
  1355. }
  1356. /// <summary>
  1357. /// 毫米转换为像素
  1358. /// </summary>
  1359. /// <remarks>
  1360. /// 表示倍率 100%
  1361. /// </remarks>
  1362. /// <param name="mm">毫米</param>
  1363. /// <returns>像素</returns>
  1364. public static int MillimeterToPixel(float mm)
  1365. {
  1366. decimal pixel = System.Convert.ToDecimal(mm) /
  1367. System.Convert.ToDecimal(MILLIMETER_PER_INCH) *
  1368. System.Convert.ToDecimal(SCREEN_DPI);
  1369. return System.Convert.ToInt32(Math.Round(pixel, 0));
  1370. }
  1371. /// <summary>
  1372. /// 毫米转换为像素
  1373. /// </summary>
  1374. /// <param name="mm">毫米</param>
  1375. /// <param name="paperZoom">表示倍率%</param>
  1376. /// <returns>像素</returns>
  1377. public static int MillimeterToPixel(float mm, int paperZoom)
  1378. {
  1379. if (0 < paperZoom)
  1380. {
  1381. decimal pixel = System.Convert.ToDecimal(mm) /
  1382. System.Convert.ToDecimal(MILLIMETER_PER_INCH) *
  1383. System.Convert.ToDecimal(SCREEN_DPI) *
  1384. System.Convert.ToDecimal(paperZoom) / 100m;
  1385. return System.Convert.ToInt32(Math.Round(pixel, 0));
  1386. }
  1387. else
  1388. {
  1389. return 0;
  1390. }
  1391. }
  1392. /// <summary>
  1393. /// 毫米转换为像素
  1394. /// </summary>
  1395. /// <param name="mm">毫米</param>
  1396. /// <param name="paperZoom">表示倍率%</param>
  1397. /// <returns>像素</returns>
  1398. public static int MillimeterToPixel(float mm, double paperZoom)
  1399. {
  1400. if (0 < paperZoom)
  1401. {
  1402. decimal pixel = System.Convert.ToDecimal(mm) /
  1403. System.Convert.ToDecimal(MILLIMETER_PER_INCH) *
  1404. System.Convert.ToDecimal(SCREEN_DPI) *
  1405. System.Convert.ToDecimal(paperZoom);
  1406. return System.Convert.ToInt32(Math.Round(pixel, 0));
  1407. }
  1408. else
  1409. {
  1410. return 0;
  1411. }
  1412. }
  1413. /// <summary>
  1414. /// 毫米转换为像素
  1415. /// </summary>
  1416. /// <param name="mm">毫米</param>
  1417. /// <param name="paperZoom">表示倍率%</param>
  1418. /// <returns>像素</returns>
  1419. public static float MillimeterToPixelSingle(float mm, int paperZoom)
  1420. {
  1421. if (0 < paperZoom)
  1422. {
  1423. decimal pixel = System.Convert.ToDecimal(mm) /
  1424. System.Convert.ToDecimal(MILLIMETER_PER_INCH) *
  1425. System.Convert.ToDecimal(SCREEN_DPI) *
  1426. System.Convert.ToDecimal(paperZoom) / 100m;
  1427. return System.Convert.ToSingle(pixel);
  1428. }
  1429. else
  1430. {
  1431. return 0f;
  1432. }
  1433. }
  1434. /// <summary>
  1435. /// 像素转换为毫米
  1436. /// </summary>
  1437. /// <remarks>
  1438. /// 表示倍率 100%
  1439. /// </remarks>
  1440. /// <param name="pixel">像素</param>
  1441. /// <returns>毫米</returns>
  1442. public static float PixelToMillimeter(int pixel)
  1443. {
  1444. decimal mm = System.Convert.ToDecimal(pixel) *
  1445. System.Convert.ToDecimal(MILLIMETER_PER_INCH) /
  1446. System.Convert.ToDecimal(SCREEN_DPI);
  1447. return System.Convert.ToSingle(mm);
  1448. }
  1449. /// <summary>
  1450. /// 像素转换为毫米
  1451. /// </summary>
  1452. /// <param name="pixel">像素</param>
  1453. /// <param name="paperZoom">表示倍率%</param>
  1454. /// <returns>毫米</returns>
  1455. public static float PixelToMillimeter(int pixel, int paperZoom)
  1456. {
  1457. if (0 < paperZoom)
  1458. {
  1459. decimal mm = System.Convert.ToDecimal(pixel) *
  1460. System.Convert.ToDecimal(MILLIMETER_PER_INCH) /
  1461. System.Convert.ToDecimal(SCREEN_DPI) /
  1462. System.Convert.ToDecimal(paperZoom) * 100m;
  1463. return System.Convert.ToSingle(mm);
  1464. }
  1465. else
  1466. {
  1467. return 0f;
  1468. }
  1469. }
  1470. /// <summary>
  1471. /// 像素转换为毫米
  1472. /// </summary>
  1473. /// <param name="pixel">像素</param>
  1474. /// <param name="paperZoom">表示倍率%</param>
  1475. /// <param name="dpi">DPI</param>
  1476. /// <returns>毫米</returns>
  1477. public static float PixelToMillimeter(int pixel, int paperZoom, float dpi)
  1478. {
  1479. if (0 < paperZoom)
  1480. {
  1481. decimal mm = System.Convert.ToDecimal(pixel) *
  1482. System.Convert.ToDecimal(MILLIMETER_PER_INCH) /
  1483. System.Convert.ToDecimal(dpi) /
  1484. System.Convert.ToDecimal(paperZoom) * 100m;
  1485. return System.Convert.ToSingle(mm);
  1486. }
  1487. else
  1488. {
  1489. return 0f;
  1490. }
  1491. }
  1492. /// <summary>
  1493. /// 像素转换为毫米
  1494. /// </summary>
  1495. /// <param name="pixel">像素</param>
  1496. /// <param name="paperZoom">表示倍率%</param>
  1497. /// <returns>毫米</returns>
  1498. public static float PixelToMillimeter(int pixel, double paperZoom)
  1499. {
  1500. if (0 < paperZoom)
  1501. {
  1502. decimal mm = System.Convert.ToDecimal(pixel) *
  1503. System.Convert.ToDecimal(MILLIMETER_PER_INCH) /
  1504. System.Convert.ToDecimal(SCREEN_DPI) /
  1505. System.Convert.ToDecimal(paperZoom);
  1506. return System.Convert.ToSingle(mm);
  1507. }
  1508. else
  1509. {
  1510. return 0f;
  1511. }
  1512. }
  1513. /// <summary>
  1514. /// 截断到指定的精度。
  1515. /// </summary>
  1516. /// <param name="value">截断的目标</param>
  1517. /// <param name="digits">精度</param>
  1518. /// <returns>截断后的数值</returns>
  1519. public static float Truncate(float value, int digits)
  1520. {
  1521. double v = Math.Pow(10, digits);
  1522. return Convert.ToSingle(Math.Truncate((double)value * v) / v);
  1523. }
  1524. /// <summary>
  1525. /// 截断到指定的精度。
  1526. /// </summary>
  1527. /// <param name="value">截断的目标</param>
  1528. /// <param name="digits">精度</param>
  1529. /// <returns>截断后的数值</returns>
  1530. public static decimal Truncate(decimal value, int digits)
  1531. {
  1532. decimal v = (decimal)Math.Pow(10, digits);
  1533. return Math.Truncate(value * v) / v;
  1534. }
  1535. #endregion
  1536. #region 字符转成条码图片
  1537. /// <summary>
  1538. /// 根据条码生成条码图片
  1539. /// </summary>
  1540. /// <param name="barcode">条码字符</param>
  1541. /// <returns>条码图片的Stream</returns>
  1542. public static Image GetCode39Image(string barcode)
  1543. {
  1544. try
  1545. {
  1546. byte[] bytes = GetCode39Byte(barcode);
  1547. MemoryStream ms = new MemoryStream(bytes);
  1548. Image returnImage = Image.FromStream(ms);
  1549. return returnImage;
  1550. }
  1551. catch (Exception ex)
  1552. {
  1553. throw ex;
  1554. }
  1555. }
  1556. /// <summary>
  1557. /// 根据条码生成条码图片
  1558. /// </summary>
  1559. /// <param name="barcode">条码字符</param>
  1560. /// <returns>条码图片的Stream</returns>
  1561. public static Stream GetCode39Stream(string barcode)
  1562. {
  1563. try
  1564. {
  1565. byte[] bytes = GetCode39Byte(barcode);
  1566. Stream stream = new MemoryStream(bytes);
  1567. return stream;
  1568. }
  1569. catch (Exception ex)
  1570. {
  1571. throw ex;
  1572. }
  1573. }
  1574. /// <summary>
  1575. /// 根据条码生成条码图片
  1576. /// </summary>
  1577. /// <param name="barcode">条码字符</param>
  1578. /// <returns>条码图片的byte[]</returns>
  1579. public static byte[] GetCode39Byte(string barcode)
  1580. {
  1581. try
  1582. {
  1583. MemoryStream memoryStream = new MemoryStream();
  1584. Bitmap barcodeBitmap = GetCode39Bitmap(barcode);
  1585. barcodeBitmap.Save(memoryStream, ImageFormat.Png);
  1586. byte[] byteImage = new Byte[memoryStream.Length];
  1587. byteImage = memoryStream.ToArray();
  1588. return byteImage;
  1589. }
  1590. catch (Exception ex)
  1591. {
  1592. throw ex;
  1593. }
  1594. }
  1595. /// <summary>
  1596. /// 根据条码生成条码图片
  1597. /// </summary>
  1598. /// <param name="barcode">条码字符</param>
  1599. /// <returns>
  1600. /// Bitmap条码图片
  1601. /// </returns>
  1602. public static Bitmap GetCode39Bitmap(string barcode)
  1603. {
  1604. try
  1605. {
  1606. int x = 5; // 左边界
  1607. int y = 0; // 上边界
  1608. int WidLength = 2; // 粗BarCode长度
  1609. int NarrowLength = 1; // 细BarCode长度
  1610. int BarCodeHeight = 50; // BarCode高度
  1611. int intSourceLength = barcode.Length;
  1612. string strEncode = "010010100"; // 编码字符串·初始值为 起始符号 *
  1613. string AlphaBet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*"; // Code39的字母
  1614. string[] Code39 = //Code39的各字母对应码
  1615. {
  1616. /**//* 0 */ "000110100",
  1617. /**//* 1 */ "100100001",
  1618. /**//* 2 */ "001100001",
  1619. /**//* 3 */ "101100000",
  1620. /**//* 4 */ "000110001",
  1621. /**//* 5 */ "100110000",
  1622. /**//* 6 */ "001110000",
  1623. /**//* 7 */ "000100101",
  1624. /**//* 8 */ "100100100",
  1625. /**//* 9 */ "001100100",
  1626. /**//* A */ "100001001",
  1627. /**//* B */ "001001001",
  1628. /**//* C */ "101001000",
  1629. /**//* D */ "000011001",
  1630. /**//* E */ "100011000",
  1631. /**//* F */ "001011000",
  1632. /**//* G */ "000001101",
  1633. /**//* H */ "100001100",
  1634. /**//* I */ "001001100",
  1635. /**//* J */ "000011100",
  1636. /**//* K */ "100000011",
  1637. /**//* L */ "001000011",
  1638. /**//* M */ "101000010",
  1639. /**//* N */ "000010011",
  1640. /**//* O */ "100010010",
  1641. /**//* P */ "001010010",
  1642. /**//* Q */ "000000111",
  1643. /**//* R */ "100000110",
  1644. /**//* S */ "001000110",
  1645. /**//* T */ "000010110",
  1646. /**//* U */ "110000001",
  1647. /**//* V */ "011000001",
  1648. /**//* W */ "111000000",
  1649. /**//* X */ "010010001",
  1650. /**//* Y */ "110010000",
  1651. /**//* Z */ "011010000",
  1652. /**//* - */ "010000101",
  1653. /**//* . */ "110000100",
  1654. /**//*' '*/ "011000100",
  1655. /**//* $ */ "010101000",
  1656. /**//* / */ "010100010",
  1657. /**//* + */ "010001010",
  1658. /**//* % */ "000101010",
  1659. /**//* * */ "010010100"
  1660. };
  1661. // 条码转成大写
  1662. barcode = barcode.ToUpper();
  1663. // 做成图片
  1664. Bitmap objBitmap = new Bitmap(
  1665. ((WidLength * 3 + NarrowLength * 7) * (intSourceLength + 2)) + (x * 2),
  1666. BarCodeHeight + (y * 2));
  1667. Graphics objGraphics = Graphics.FromImage(objBitmap);
  1668. // 底色
  1669. objGraphics.FillRectangle(Brushes.White, 0, 0, objBitmap.Width, objBitmap.Height);
  1670. for (int i = 0; i < intSourceLength; i++)
  1671. {
  1672. // 检查是否有非法字符
  1673. if (AlphaBet.IndexOf(barcode[i]) == -1 || barcode[i] == '*')
  1674. {
  1675. objGraphics.DrawString("含有非法字符",
  1676. SystemFonts.DefaultFont, Brushes.Red, x, y);
  1677. return objBitmap;
  1678. }
  1679. // 查表编码
  1680. strEncode = string.Format("{0}0{1}", strEncode, Code39[AlphaBet.IndexOf(barcode[i])]);
  1681. }
  1682. strEncode = string.Format("{0}0010010100", strEncode); // 补充结束符号 *
  1683. int intEncodeLength = strEncode.Length; // 编码后边的长度
  1684. int intBarWidth;
  1685. for (int i = 0; i < intEncodeLength; i++) // 根据码画出Code39 BarCode
  1686. {
  1687. intBarWidth = strEncode[i] == '1' ? WidLength : NarrowLength;
  1688. objGraphics.FillRectangle(i % 2 == 0 ? Brushes.Black : Brushes.White,
  1689. x, y, intBarWidth, BarCodeHeight);
  1690. x += intBarWidth;
  1691. }
  1692. return objBitmap;
  1693. }
  1694. catch (Exception ex)
  1695. {
  1696. throw ex;
  1697. }
  1698. }
  1699. #endregion
  1700. #region INI文件读写
  1701. /// <summary>
  1702. /// 写入INI文件
  1703. /// </summary>
  1704. /// <param name="section">节点名称[如[TypeName]]</param>
  1705. /// <param name="key">键</param>
  1706. /// <param name="value">值</param>
  1707. /// <param name="filePath">文件路径</param>
  1708. /// <returns></returns>
  1709. public static long WriteIniFile(string section, string key, string value, string filePath)
  1710. {
  1711. try
  1712. {
  1713. return WritePrivateProfileString(section, key, value, filePath);
  1714. }
  1715. catch (Exception ex)
  1716. {
  1717. throw ex;
  1718. }
  1719. }
  1720. /// <summary>
  1721. /// 读取INI文件
  1722. /// </summary>
  1723. /// <param name="section">节点名称</param>
  1724. /// <param name="key">键</param>
  1725. /// <param name="filePath">文件路径</param>
  1726. /// <returns></returns>
  1727. public static string ReadIniFile(string section, string key, string filePath)
  1728. {
  1729. try
  1730. {
  1731. StringBuilder returnValue = new StringBuilder(1024);
  1732. GetPrivateProfileString(section, key, "", returnValue, 1024, filePath);
  1733. return returnValue.ToString();
  1734. }
  1735. catch (Exception ex)
  1736. {
  1737. throw ex;
  1738. }
  1739. }
  1740. #endregion
  1741. #region DataGridView限制
  1742. /// <summary>
  1743. /// 设定DataGridView的排序方式
  1744. /// </summary>
  1745. /// <param name="dataGridView">需要设置排序的DataGridView</param>
  1746. /// <param name="sortMode">排序方式</param>
  1747. public static void SetDataGridViewColumnsSortMode(DataGridView dataGridView,
  1748. DataGridViewColumnSortMode sortMode)
  1749. {
  1750. if (dataGridView == null)
  1751. {
  1752. return;
  1753. }
  1754. for (int i = 0; i < dataGridView.Columns.Count; i++)
  1755. {
  1756. dataGridView.Columns[i].SortMode = sortMode;
  1757. }
  1758. }
  1759. #endregion
  1760. #region 时间字符串转换成DateTime类型
  1761. /// <summary>
  1762. /// 时间字符串转换成DateTime类型
  1763. /// </summary>
  1764. /// <param name="serverTime">服务器时间</param>
  1765. /// <param name="timeStr">时间格式字符串</param>
  1766. /// <returns></returns>
  1767. public static DateTime ConvertTimeStr(DateTime serverTime, string timeStr)
  1768. {
  1769. try
  1770. {
  1771. DateTime dateTime = serverTime;
  1772. string[] times = timeStr.Split(':');
  1773. if (times.Length == 3)
  1774. {
  1775. dateTime = new DateTime(dateTime.Year, dateTime.Month,
  1776. dateTime.Day, Convert.ToInt16(times[0]), Convert.ToInt16(times[1]),
  1777. Convert.ToInt16(times[2]));
  1778. }
  1779. return dateTime;
  1780. }
  1781. catch (Exception ex)
  1782. {
  1783. throw ex;
  1784. }
  1785. }
  1786. #endregion
  1787. public static int? ToNullableInt32(this object obj)
  1788. {
  1789. if (obj == null || obj == DBNull.Value)
  1790. {
  1791. return null;
  1792. }
  1793. return Convert.ToInt32(obj);
  1794. }
  1795. }
  1796. }