2 using System.Collections.Generic;
3 using System.Text.RegularExpressions;
4 using System.Text;
5
6
7 namespace InsApp.word
8 {
9 /**//// <summary>
10 /// string CreateRandomCode(int codeCount) 根据长度生成随机的数字和字母
11 /// bool toFilter(string thePara) 检测非法字符,如果参数是空/包含非法字符,返回false/否则返回 true
12 /// bool CheckNumber(string GetNum) 判断是否是数字格式
13 /// bool CheckNumberRegx(string GetNum) 判断是否是正负数字含小数
14 /// bool CheckNullstr(string Getstr) 判断是否是空值null 返回true || false
15 /// </summary>
16 public class CreateCode
17 {
18 生成随机的数字和字母 codeCount是希望生成的长度#region 生成随机的数字和字母 codeCount是希望生成的长度
19 /**//// <summary>
20 /// 生成随机的数字和字母
21 /// </summary>
22 /// <param name="codeCount">codeCount是希望生成的长度</param>
23 /// <returns></returns>
24 public string CreateRandomCode(int codeCount) //codeCount是希望生成的长度
25 {
26 string allChar = "0,1,2,3,4,5,6,7,8,9";
27 string[] allCharArray = allChar.Split(',');
28 string randomCode = "";
29 Random rand = new Random();
30 int temp = -1;
31 for (int i = 0; i < codeCount; i++)
32 {
33 if (temp != -1)
34 {
35 rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
36 }
37 int t = rand.Next(0,10);
38
39 temp = t;
40 randomCode += allCharArray[t];
41 }
42 return randomCode;
43 }
44 #endregion
45
46 判断是否是数字格式#region 判断是否是数字格式
47 /**//// <summary>
48 /// 判断是否是数字格式
49 /// </summary>
50 /// <param name="GetNum"></param>
51 public bool CheckNumber(string GetNum)
52 {
53
54 Regex r = new Regex(@"^[0-9]+$");
55 if (r.IsMatch(GetNum))
56 {
57 return true;
58 }
59 else
60 {
61 return false;
62 }
63 }
64 #endregion
65
66 检测非法字符,防止sql注入#region 检测非法字符,防止sql注入
67 /**//// <summary>
68 /// 检测非法字符,防止sql注入
69 /// 如果参数是空,返回false
70 /// 如果参数中包含非法字符,返回false
71 ///// 否则返回 true
72 /// </summary>
73 /// <param name="thePara"></param>
74 /// <returns></returns>
75 public bool toFilter(string thePara)
76 {
77 string[] BadCode = new string[] { "'", "\"", "exec", "cmd", ">", "<", "and", "=", "\\", ";" };
78 try
79 {
80 if (CheckNullstr(thePara) == false) //如果参数是空值,返回false
81 {
82 throw new Exception("参数为空");
83 }
84 else
85 {
86 for (int i = 0; i < BadCode.Length; i++)
87 {
88 if (thePara.IndexOf(BadCode[i]) > 0)
89 {
90 throw new Exception("包含非法字符");
91 }
92 }
93 }
94 return true;
95 }
96 catch
97 {
98 return false;
99 }
100
101
102 }
103 #endregion
104
105 bool CheckNullstr(string Getstr)判断是否是空值#region bool CheckNullstr(string Getstr)判断是否是空值
106 /**//// <summary>
107 /// Getstr得到参数判断是否是空值
108 /// </summary>
109 /// <param name="Getstr">需要检查的值</param>
110 /// <param name="GetShow">这个字段的功能说明:姓名,sex</param>
111 public bool CheckNullstr(string Getstr)
112 {
113 try
114 {
115 if (Getstr == null || Getstr == "" || Getstr.Length < 1)
116 {
117 return false;
118 }
119 else
120 {
121 return true;
122 }
123 }
124 catch
125 {
126 return false;
127 }
128
129 }
130 #endregion
131
132 bool CheckNumberRegx(string GetNum)正则表达式 判断是否是正负数字含小数#region bool CheckNumberRegx(string GetNum)正则表达式 判断是否是正负数字含小数
133 /**//// <summary>
134 /// 判断是否是数字格式
135 /// </summary>
136 /// <param name="GetNum"></param>
137 public bool CheckNumberRegx(string GetNum)
138 {
139 //^[+-]?\d+(\.\d+)?$正负数字含小数 数字含小数^\d+(\.\d+)?$
140 Regex r = new Regex(@"^\d+(\.\d+)?$");
141 if (r.IsMatch(GetNum))
142 {
143 return true;
144 }
145 else
146 {
147 return false;
148 }
149 }
150 #endregion
151
152 用C#截取指定长度的中英文混合字符串#region 用C#截取指定长度的中英文混合字符串
153 /**//// <summary>
154 /// s接受的字符
155 /// l长度
156 /// </summary>
157 /// <param name="s"></param>
158 /// <param name="l"></param>
159 /// <returns></returns>
160 public static string CutStr(string s, int l)
161 {
162 string temp = s;
163 if (Regex.Replace(temp, "[\u4e00-\u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= l)
164 {
165 return temp;
166 }
167 for (int i = temp.Length; i >= 0; i--)
168 {
169 temp = temp.Substring(0, i);
170 if (Regex.Replace(temp, "[\u4e00-\u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= l - 3)
171 {
172 return temp + "";
173 }
174 }
175 return "";
176 }
177 #endregion
178
179 数字和字母随机数#region 数字和字母随机数
180 /**//// <summary>
181 /// 数字和字母随机数
182 /// </summary>
183 /// <param name="n">生成长度</param>
184 /// <returns></returns>
185 public static string Rand_Number_AZ_Code(int n)
186 {
187 char[] arrChar = new char[]{
188 'a','b','d','c','e','f','g','h','i','j','k','l','m','n','p','r','q','s','t','u','v','w','z','y','x',
189 '0','1','2','3','4','5','6','7','8','9',
190 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','Q','P','R','T','S','V','U','W','X','Y','Z'
191 };
192
193 StringBuilder num = new StringBuilder();
194
195 Random rnd = new Random(DateTime.Now.Millisecond);
196 for (int i = 0; i < n; i++)
197 {
198 num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString());
199 }
200
201 return num.ToString();
202 }
203 #endregion
204
205 字母随机数#region 字母随机数
206 /**//// <summary>
207 /// 字母随机数
208 /// </summary>
209 /// <param name="n">生成长度</param>
210 /// <returns></returns>
211 public static string RandLetter(int n)
212 {
213 char[] arrChar = new char[]{
214 'a','b','d','c','e','f','g','h','i','j','k','l','m','n','p','r','q','s','t','u','v','w','z','y','x',
215 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','Q','P','R','T','S','V','U','W','X','Y','Z'
216 };
217
218 StringBuilder num = new StringBuilder();
219
220 Random rnd = new Random(DateTime.Now.Millisecond);
221 for (int i = 0; i < n; i++)
222 {
223 num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString());
224
225 }
226
227 return num.ToString();
228 }
229 #endregion
230
231 日期随机函数#region 日期随机函数
232 /**//// <summary>
233 /// 日期随机函数
234 /// </summary>
235 /// <param name="ra">长度</param>
236 /// <returns></returns>
237 public static string DateRndName(Random ra)
238 {
239 DateTime d = DateTime.Now;
240 string s = null, y, m, dd, h, mm, ss;
241 y = d.Year.ToString();
242 m = d.Month.ToString();
243 if (m.Length < 2) m = "0" + m;
244 dd = d.Day.ToString();
245 if (dd.Length < 2) dd = "0" + dd;
246 h = d.Hour.ToString();
247 if (h.Length < 2) h = "0" + h;
248 mm = d.Minute.ToString();
249 if (mm.Length < 2) mm = "0" + mm;
250 ss = d.Second.ToString();
251 if (ss.Length < 2) ss = "0" + ss;
252 s += y + m + dd + h + mm + ss;
253 s += ra.Next(100, 999).ToString();
254 return s;
255 }
256 #endregion
257
258 生成GUID#region 生成GUID
259 /**//// <summary>
260 /// 生成GUID
261 /// </summary>
262 /// <returns></returns>
263 public static string GetGuid()
264 {
265 System.Guid g = System.Guid.NewGuid();
266 return g.ToString();
267 }
268 #endregion
269 }
270}
271
由枚举值生成数组,可以用于绑定到ComboBox上或者DropDownList。
class
Utils
{
public static object [] GetEnumValues(Type type)
{
List < object > list = new List < object > ();
for ( int i = 0 ; ; i ++ )
{
string val = Enum.GetName(type, i);
if ( ! string .IsNullOrEmpty(val))
list.Add(val);
else
break ;
}
return list.ToArray();
}
}
{
public static object [] GetEnumValues(Type type)
{
List < object > list = new List < object > ();
for ( int i = 0 ; ; i ++ )
{
string val = Enum.GetName(type, i);
if ( ! string .IsNullOrEmpty(val))
list.Add(val);
else
break ;
}
return list.ToArray();
}
}
使用:
object
[] list
=
Utils.GetEnumValues(
typeof
(ChartType))