Repository navigation
Expand file tree
/
Copy pathTools.cs
More file actions
532 lines (491 loc) · 21.7 KB
/
Copy pathTools.cs
File metadata and controls
532 lines (491 loc) · 21.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Web;
namespace Utility
{
public class Tools
{
[Obsolete("请用Utility.DateTimeNew.Now", true)]
public static DateTime DateTime
{
get
{
return DateTimeNew.Now;
}
}
public static System.IO.MemoryStream GetValidateImg(out string code, string bgImg = "/Images/vcodebg.png")
{
code = GetValidateCode();
Random rnd = new Random();
System.Drawing.Bitmap img = new System.Drawing.Bitmap((int)Math.Ceiling((code.Length * 17.2)), 28);
System.Drawing.Image bg = System.Drawing.Bitmap.FromFile(HttpContext.Current.Server.MapPath(bgImg));
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img);
System.Drawing.Font font = new System.Drawing.Font("Arial", 16, (System.Drawing.FontStyle.Regular | System.Drawing.FontStyle.Italic));
System.Drawing.Font fontbg = new System.Drawing.Font("Arial", 16, (System.Drawing.FontStyle.Regular | System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new System.Drawing.Rectangle(0, 0, img.Width, img.Height), System.Drawing.Color.Blue, System.Drawing.Color.DarkRed, 1.2f, true);
g.DrawImage(bg, 0, 0, new System.Drawing.Rectangle(rnd.Next(bg.Width - img.Width), rnd.Next(bg.Height - img.Height), img.Width, img.Height), System.Drawing.GraphicsUnit.Pixel);
g.DrawString(code, fontbg, System.Drawing.Brushes.White, 0, 1);
g.DrawString(code, font, System.Drawing.Brushes.Green, 0, 1);//字颜色
//画图片的背景噪音线
int x = img.Width;
int y1 = rnd.Next(5, img.Height);
int y2 = rnd.Next(5, img.Height);
g.DrawLine(new System.Drawing.Pen(System.Drawing.Color.Green, 2), 1, y1, x - 2, y2);
g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.Transparent), 0, 10, img.Width - 1, img.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
return ms;
}
private static string GetValidateCode()
{ //产生五位的随机字符串
int number;
char code;
string checkCode = String.Empty;
System.Random random = new Random();
for (int i = 0; i < 4; i++)
{
number = random.Next();
if (number % 2 == 0)
code = (char)('0' + (char)(number % 10));
else if (number % 3 == 0)
code = (char)('a' + (char)(number % 26));
else
code = (char)('A' + (char)(number % 26));
checkCode += code == '0' || code == 'O' ? "x" : code.ToString();
}
return checkCode;
}
/// <summary>
/// 获取远程浏览器端 IP 地址
/// </summary>
/// <returns>返回 IPv4 地址</returns>
public static string GetIPAddress()
{
string userHostAddress = HttpContext.Current.Request.UserHostAddress;
if (userHostAddress.IsNullOrEmpty())
{
userHostAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
return userHostAddress;
}
/// <summary>
/// 得到用户浏览器类型
/// </summary>
/// <returns></returns>
public static string GetBrowse()
{
return System.Web.HttpContext.Current.Request.Browser.Type;
}
/// <summary>
/// 获取浏览器端操作系统名称
/// </summary>
/// <returns></returns>
public static string GetOSName()
{
string osVersion = System.Web.HttpContext.Current.Request.Browser.Platform;
string userAgent = System.Web.HttpContext.Current.Request.UserAgent;
if (userAgent.Contains("NT 6.3"))
{
osVersion = "Windows8.1";
}
else if (userAgent.Contains("NT 6.2"))
{
osVersion = "Windows8";
}
else if (userAgent.Contains("NT 6.1"))
{
osVersion = "Windows7";
}
else if (userAgent.Contains("NT 6.0"))
{
osVersion = "WindowsVista";
}
else if (userAgent.Contains("NT 5.2"))
{
osVersion = "WindowsServer2003";
}
else if (userAgent.Contains("NT 5.1"))
{
osVersion = "WindowsXP";
}
else if (userAgent.Contains("NT 5"))
{
osVersion = "Windows2000";
}
else if (userAgent.Contains("NT 4"))
{
osVersion = "WindowsNT4.0";
}
else if (userAgent.Contains("Me"))
{
osVersion = "WindowsMe";
}
else if (userAgent.Contains("98"))
{
osVersion = "Windows98";
}
else if (userAgent.Contains("95"))
{
osVersion = "Windows95";
}
else if (userAgent.Contains("Mac"))
{
osVersion = "Mac";
}
else if (userAgent.Contains("Unix"))
{
osVersion = "UNIX";
}
else if (userAgent.Contains("Linux"))
{
osVersion = "Linux";
}
else if (userAgent.Contains("SunOS"))
{
osVersion = "SunOS";
}
return osVersion;
}
/// <summary>
/// 得到分页HTML
/// </summary>
/// <param name="recordCount">记录总数</param>
/// <param name="pageSize">每页条数</param>
/// <param name="pageNumber">当前页</param>
/// <param name="queryString">查询字符串</param>
/// <returns></returns>
public static string GetPagerHtml(long recordCount, int pageSize, int pageNumber, string queryString)
{
//得到共有多少页
long PageCount = recordCount <= 0 ? 1 : recordCount % pageSize == 0 ? recordCount / pageSize : recordCount / pageSize + 1;
long pNumber = pageNumber;
if (pNumber < 1)
{
pNumber = 1;
}
else if (pNumber > PageCount)
{
pNumber = PageCount;
}
//如果只有一页则返回空分页字符串
if (PageCount <= 1)
{
return "";
}
StringBuilder ReturnPagerString = new StringBuilder(1500);
string JsFunctionName = string.Empty;
//构造分页字符串
int DisplaySize = 10;//中间显示的页数
ReturnPagerString.Append("<div>");
ReturnPagerString.Append("<span style='margin-right:15px;'>共 " + recordCount.ToString() + " 条 每页 <input type='text' id='tnt_count' title='输入数字可改变每页显示条数' class='pagertxt' onchange=\"javascript:_toPage_" + JsFunctionName + "(" + pNumber.ToString() + ",this.value);\" value='" + pageSize.ToString() + "' /> 条 ");
ReturnPagerString.Append("转到 <input type='text' id='paernumbertext' title='输入数字可跳转页' value=\"" + pNumber.ToString() + "\" onchange=\"javascript:_toPage_" + JsFunctionName + "(this.value," + pageSize.ToString() + ");\" class='pagertxt'/> 页</span>");
if (pNumber > 1)
ReturnPagerString.Append("<a class=\"pager\" href=\"javascript:_toPage_" + JsFunctionName + "(" + (pNumber - 1).ToString() + "," + pageSize.ToString() + ");\"><span class=\"pagerarrow\">«</span></a>");
//添加第一页
if (pNumber >= DisplaySize / 2 + 3)
ReturnPagerString.Append("<a class=\"pager\" href=\"javascript:_toPage_" + JsFunctionName + "(1," + pageSize.ToString() + ");\">1…</a>");
else
ReturnPagerString.Append("<a class=\"" + (1 == pNumber ? "pagercurrent" : "pager") + "\" href=\"javascript:_toPage_" + JsFunctionName + "(1," + pageSize.ToString() + ");\">1</a>");
//添加中间数字
long star = pNumber - DisplaySize / 2;
long end = pNumber + DisplaySize / 2;
if (star < 2)
{
end += 2 - star;
star = 2;
}
if (end > PageCount - 1)
{
star -= end - (PageCount - 1);
end = PageCount - 1;
}
if (star < 2)
star = 2;
for (long i = star; i <= end; i++)
ReturnPagerString.Append("<a class=\"" + (i == pNumber ? "pagercurrent" : "pager") + "\" href=\"javascript:_toPage_" + JsFunctionName + "(" + i.ToString() + "," + pageSize.ToString() + ");\">" + i.ToString() + "</a>");
//添加最后页
if (pNumber <= PageCount - (DisplaySize / 2))
ReturnPagerString.Append("<a class=\"pager\" href=\"javascript:_toPage_" + JsFunctionName + "(" + PageCount.ToString() + "," + pageSize.ToString() + ");\">…" + PageCount.ToString() + "</a>");
else if (PageCount > 1)
ReturnPagerString.Append("<a class=\"" + (PageCount == pNumber ? "pagercurrent" : "pager") + "\" href=\"javascript:_toPage_" + JsFunctionName + "(" + PageCount.ToString() + "," + pageSize.ToString() + ");\">" + PageCount.ToString() + "</a>");
if (pNumber < PageCount)
ReturnPagerString.Append("<a class=\"pager\" href=\"javascript:_toPage_" + JsFunctionName + "(" + (pNumber + 1).ToString() + "," + pageSize.ToString() + ");\"><span class=\"pagerarrow\">»</span></a>");
ReturnPagerString.Append("</div>");
//构造分页JS函数
ReturnPagerString.Append("<script type=\"text/javascript\" lanuage=\"javascript\">");
ReturnPagerString.Append("function _toPage_" + JsFunctionName + "(page,size){");
ReturnPagerString.Append("var par=\"" + queryString + "\";");
ReturnPagerString.Append("window.location=\"?pagenumber=\"+page+\"&pagesize=\"+size+par;");
ReturnPagerString.Append("}");
ReturnPagerString.Append("</script>");
return ReturnPagerString.ToString();
}
/// <summary>
/// 得到页尺寸
/// </summary>
/// <returns></returns>
public static int GetPageSize()
{
string size = System.Web.HttpContext.Current.Request["pagesize"] ?? "15";
int size1;
return size.IsInt(out size1) ? size1 : 15;
}
/// <summary>
/// 得到页号
/// </summary>
/// <returns></returns>
public static int GetPageNumber()
{
string number = System.Web.HttpContext.Current.Request["pagenumber"] ?? "1";
int number1;
return number.IsInt(out number1) ? number1 : 1;
}
/// <summary>
/// 得到列表项
/// </summary>
/// <param name="list">列表, 标题,值</param>
/// <param name="value">默认值</param>
/// <param name="showEmpty">是不显示空选项</param>
/// <param name="emptyTitle">空选项显示标题</param>
/// <returns></returns>
public static System.Web.UI.WebControls.ListItem[] GetListItems(IList<string[]> list, string value, bool showEmpty = false, string emptyTitle = "")
{
List<System.Web.UI.WebControls.ListItem> items = new List<System.Web.UI.WebControls.ListItem>();
if (showEmpty)
{
items.Add(new System.Web.UI.WebControls.ListItem(emptyTitle, ""));
}
foreach (var li in list)
{
if (li.Length < 2)
{
continue;
}
var item = new System.Web.UI.WebControls.ListItem(li[0], li[1]);
item.Selected = !value.IsNullOrEmpty() && value == li[1] && !items.Exists(p => p.Selected);
items.Add(item);
}
return items.ToArray();
}
public static System.Web.UI.WebControls.ListItem[] GetListItems(IList<string> list, string value, bool showEmpty = false, string emptyTitle = "")
{
List<string[]> newList = new List<string[]>();
foreach (string str in list)
{
newList.Add(new string[] { str, str });
}
return GetListItems(newList, value, showEmpty, emptyTitle);
}
/// <summary>
/// 将服务器控件列表项转换为select列表项
/// </summary>
/// <param name="items"></param>
/// <returns></returns>
public static string GetOptionsString(System.Web.UI.WebControls.ListItem[] items)
{
StringBuilder options = new StringBuilder(items.Length * 50);
foreach (var item in items)
{
options.AppendFormat("<option value=\"{0}\" {1}>", item.Value.Replace("\"","'"), item.Selected ? "selected=\"selected\"" : "");
options.Append(item.Text);
options.Append("</option>");
}
return options.ToString();
}
/// <summary>
/// 将服务器控件列表项转换为Checkbox项
/// </summary>
/// <param name="items"></param>
/// <returns></returns>
public static string GetCheckBoxString(System.Web.UI.WebControls.ListItem[] items, string name, string[] values, string otherAttr = "")
{
StringBuilder options = new StringBuilder(items.Length * 50);
foreach (var item in items)
{
string tempid = Guid.NewGuid().ToString("N");
options.AppendFormat("<input type=\"checkbox\" value=\"{0}\" {1} id=\"{2}\" name=\"{3}\" {4} style=\"vertical-align:middle\" />",
item.Value.Replace("\"", "'"),
values != null && values.Contains(item.Value) ? "checked=\"checked\"" : "",
string.Format("{0}_{1}", name, tempid),
name,
otherAttr
);
options.AppendFormat("<label style=\"vertical-align:middle;margin-right:2px;\" for=\"{0}\">", string.Format("{0}_{1}", name, tempid));
options.Append(item.Text);
options.Append("</label>");
}
return options.ToString();
}
/// <summary>
/// 将服务器控件列表项转换为Radio项
/// </summary>
/// <param name="items"></param>
/// <returns></returns>
public static string GetRadioString(System.Web.UI.WebControls.ListItem[] items, string name, string otherAttr = "")
{
StringBuilder options = new StringBuilder(items.Length * 50);
foreach (var item in items)
{
string tempid = Guid.NewGuid().ToString("N");
options.AppendFormat("<input type=\"radio\" value=\"{0}\" {1} id=\"{2}\" name=\"{3}\" {4} style=\"vertical-align:middle\" />",
item.Value.Replace("\"", "'"),
item.Selected ? "checked=\"checked\"" : "",
string.Format("{0}_{1}", name, tempid),
name,
otherAttr
);
options.AppendFormat("<label style=\"vertical-align:middle;margin-right:2px;\" for=\"{0}\">", string.Format("{0}_{1}", name, tempid));
options.Append(item.Text);
options.Append("</label>");
}
return options.ToString();
}
/// <summary>
/// 得到是否选择项
/// </summary>
/// <param name="value"></param>
/// <param name="showEmpty"></param>
/// <param name="emptyString"></param>
/// <returns></returns>
public static System.Web.UI.WebControls.ListItem[] GetYesNoListItems(string value, bool showEmpty = false, string emptyString = "")
{
List<string[]> list = new List<string[]>();
list.Add(new string[] { "是", "1" });
list.Add(new string[] { "否", "0" });
return GetListItems(list, value, showEmpty, emptyString);
}
/// <summary>
/// 得到sql语句in里的字符串
/// </summary>
/// <param name="str">字符串</param>
/// <returns></returns>
public static string GetSqlInString(string str, bool isSingleQuotes = true, string split = ",")
{
string[] strArray = str.Split(new string[] { split }, StringSplitOptions.RemoveEmptyEntries);
return GetSqlInString(strArray, isSingleQuotes);
}
/// <summary>
/// 得到sql语句in里的字符串
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="strArray"></param>
/// <param name="isSingleQuotes">是否加单引号,字符串要加,数字不加</param>
/// <returns></returns>
public static string GetSqlInString<T>(T[] strArray, bool isSingleQuotes = true)
{
StringBuilder inStr = new StringBuilder(strArray.Length * 40);
foreach (var s in strArray)
{
if (s.ToString().IsNullOrEmpty())
{
continue;
}
if (isSingleQuotes)
{
inStr.Append("'");
}
inStr.Append(s.ToString().Trim());
if (isSingleQuotes)
{
inStr.Append("'");
}
inStr.Append(",");
}
return inStr.ToString().TrimEnd(',');
}
/// <summary>
/// 产生不重复随机数
/// </summary>
/// <param name="count">共产生多少随机数</param>
/// <param name="minValue">最小值</param>
/// <param name="maxValue">最大值</param>
/// <returns>int[]数组</returns>
public static int[] GetRandomNum(int count, int minValue, int maxValue)
{
Random rnd = new Random(Guid.NewGuid().GetHashCode());
int length = maxValue - minValue + 1;
byte[] keys = new byte[length];
rnd.NextBytes(keys);
int[] items = new int[length];
for (int i = 0; i < length; i++)
{
items[i] = i + minValue;
}
Array.Sort(keys, items);
int[] result = new int[count];
Array.Copy(items, result, count);
return result;
}
/// <summary>
/// 产生随机字符串
/// </summary>
/// <returns>字符串位数</returns>
public static string GetRandomString(int length = 5)
{
int number;
char code;
string checkCode = String.Empty;
System.Random random = new Random(Guid.NewGuid().GetHashCode());
for (int i = 0; i < length + 1; i++)
{
number = random.Next();
if (number % 2 == 0)
code = (char)('0' + (char)(number % 10));
else
code = (char)('A' + (char)(number % 26));
checkCode += code.ToString();
}
return checkCode;
}
/// <summary>
/// 产生随机字母
/// </summary>
/// <returns>字符串位数</returns>
public static string GetRandomLetter(int length = 2)
{
int number;
char code;
string checkCode = String.Empty;
System.Random random = new Random(Guid.NewGuid().GetHashCode());
for (int i = 0; i < length; i++)
{
number = random.Next();
code = (char)('A' + (char)(number % 26));
checkCode += code.ToString();
}
return checkCode;
}
/// <summary>
/// 得到一个文件的大小
/// </summary>
/// <returns></returns>
public static string GetFileSize(string file)
{
if (!System.IO.File.Exists(file))
{
return "";
}
System.IO.FileInfo fi = new System.IO.FileInfo(file);
return fi.Length.ToString("###,###");
}
public static string DataTableToJsonString(System.Data.DataTable dt)
{
LitJson.JsonData json = new LitJson.JsonData();
foreach(System.Data.DataRow dr in dt.Rows)
{
LitJson.JsonData drJson = new LitJson.JsonData();
for (int i = 0; i < dt.Columns.Count; i++)
{
LitJson.JsonData drJson1 = new LitJson.JsonData();
drJson1 = dr[i].ToString();
drJson.Add(drJson1);
}
json.Add(drJson);
}
return json.ToJson();
}
}
}