├── packages
└── WebSocketSharp-NonPreRelease.1.0.0
│ ├── .signature.p7s
│ ├── lib
│ └── net35
│ │ └── websocket-sharp.dll
│ └── WebSocketSharp-NonPreRelease.1.0.0.nupkg
├── ToolBox.Cmd
├── ToolBox.Cmd.csproj
└── CmdHelper.cs
├── ToolBox.Ftp
└── ToolBox.Ftp.csproj
├── ToolBox.Common
├── ToolBox.Common.csproj
├── RandomHelper.cs
├── Common.cs
└── ConverHelper.cs
├── ToolBox.Framework.Test
├── packages.config
├── App.config
├── Properties
│ └── AssemblyInfo.cs
├── Program.cs
└── ToolBox.Framework.Test.csproj
├── ToolBox.Object
├── ToolBox.Object.csproj
├── Serialize.cs
├── ObjectHelper.cs
└── ObjectConversion.cs
├── ToolBox.Process
├── ToolBox.Process.csproj
└── ProcessHelper.cs
├── ToolBox.Reflection
├── ToolBox.Reflection.csproj
├── EachHelper.cs
├── PropertyExpressionParser.cs
├── ObjectMapper.cs
├── GenericUtil.cs
└── ReflectionUtil.cs
├── ToolBox.DateTime
├── ToolBox.DateTimeTool.csproj.user
├── ToolBox.DateTimeTool.csproj
└── ToolBox.DateTimeTool.xml
├── ToolBox.Json
├── ToolBox.Json.csproj
└── JsonHelper.cs
├── ToolBox.Http
├── ResultType.cs
├── HttpInfo.cs
├── ResultCookieType.cs
├── PostDataType.cs
├── ToolBox.Http.csproj
├── HttpResult.cs
├── CookieHelper.cs
└── HttpItem.cs
├── ToolBox.UserAgentParse
├── ToolBox.UserAgentParse.csproj
└── UaUnit.cs
├── .gitignore
├── ToolBox.Log
├── QueueModel.cs
├── LogType.cs
├── ToolBox.Log.csproj
├── ToolBox.Log.xml
└── LogUtil.cs
├── ToolBox.Phone
├── ToolBox.PhoneModelParse.csproj
└── PhoneModelToName.cs
├── ToolBox.Security
├── ToolBox.Security.csproj
├── HexUtil.cs
├── Base64Helper.cs
├── SHA256Helper.cs
├── HashHelper.cs
├── Md5Helper.cs
├── CryptHelper.cs
├── DesHelper.cs
└── SymmetricHelper.cs
├── ToolBox.String
├── ToolBox.String.csproj
├── StringOther.cs
├── SqlFilterHelper.cs
└── RMBHelper.cs
├── ToolBox.Test
├── ToolBox.Test.csproj
└── Program.cs
├── ToolBox.Socket
├── ToolBox.Socket.csproj
├── ClientMode.cs
├── ServerCallBack.cs
├── UdpServer.cs
├── SocketTools.cs
├── SocketDesHelper.cs
└── TcpClient.cs
├── ToolBox.Files
├── Drive.cs
├── ToolBox.Files.csproj
└── ToolBox.Files.xml
├── ToolBox
├── ToolBox.Log
│ └── ToolBox.Log.xml
└── ToolBox.DateTime
│ └── ToolBox.DateTimeTool.xml
└── ToolBox.sln
/packages/WebSocketSharp-NonPreRelease.1.0.0/.signature.p7s:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yulongjiang1997/ToolBox/HEAD/packages/WebSocketSharp-NonPreRelease.1.0.0/.signature.p7s
--------------------------------------------------------------------------------
/ToolBox.Cmd/ToolBox.Cmd.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/ToolBox.Ftp/ToolBox.Ftp.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/ToolBox.Common/ToolBox.Common.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/ToolBox.Framework.Test/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/ToolBox.Object/ToolBox.Object.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/ToolBox.Process/ToolBox.Process.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/packages/WebSocketSharp-NonPreRelease.1.0.0/lib/net35/websocket-sharp.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yulongjiang1997/ToolBox/HEAD/packages/WebSocketSharp-NonPreRelease.1.0.0/lib/net35/websocket-sharp.dll
--------------------------------------------------------------------------------
/ToolBox.Reflection/ToolBox.Reflection.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/packages/WebSocketSharp-NonPreRelease.1.0.0/WebSocketSharp-NonPreRelease.1.0.0.nupkg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yulongjiang1997/ToolBox/HEAD/packages/WebSocketSharp-NonPreRelease.1.0.0/WebSocketSharp-NonPreRelease.1.0.0.nupkg
--------------------------------------------------------------------------------
/ToolBox.Framework.Test/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/ToolBox.DateTime/ToolBox.DateTimeTool.csproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | false
5 |
6 |
--------------------------------------------------------------------------------
/ToolBox.Json/ToolBox.Json.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/ToolBox.Http/ResultType.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace ToolBox.Http
6 | {
7 | ///
8 | /// 返回类型
9 | ///
10 | public enum ResultType
11 | {
12 | ///
13 | /// 字符串
14 | ///
15 | String,
16 | ///
17 | /// 字节
18 | ///
19 | Byte
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/ToolBox.Http/HttpInfo.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace ToolBox.Http
6 | {
7 | public class HttpInfo
8 | {
9 | public bool Is_bool { get; set; } = false;//成功为true
10 | public string Html { get; set; } = string.Empty;//成功后的 返回数据
11 | public string J_code { get; set; } = string.Empty;//其它数据
12 | public byte[] Img { get; set; } = null;//图片数据
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/ToolBox.UserAgentParse/ToolBox.UserAgentParse.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0
5 | true
6 | 1.0.0
7 | 浏览器UA解析
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ################################################################################
2 | # 此 .gitignore 文件已由 Microsoft(R) Visual Studio 自动创建。
3 | ################################################################################
4 |
5 | ################################################################################
6 | # 此 .gitignore 文件已由 Microsoft(R) Visual Studio 自动创建。
7 | ################################################################################
8 |
9 | .vs
10 | /*/obj
11 | /*/bin
--------------------------------------------------------------------------------
/ToolBox.Log/QueueModel.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace ToolBox.Log
6 | {
7 | ///
8 | /// 队列类
9 | ///
10 | public class QueueModel
11 | {
12 | ///
13 | /// 路径
14 | ///
15 | public string Path { get; set; }
16 |
17 | ///
18 | /// 日志消息
19 | ///
20 | public string Msg { get; set; }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/ToolBox.Phone/ToolBox.PhoneModelParse.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0
5 | true
6 | 更改包名
7 | yulongjiang
8 | 1.0.5
9 | ToolBox.PhoneModelParse
10 | ToolBox.PhoneModelParse
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/ToolBox.Log/LogType.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace ToolBox.Log
6 | {
7 | ///
8 | ///日志类型
9 | ///
10 | public enum LogType
11 | {
12 | ///
13 | /// 普通
14 | ///
15 | Debug,
16 | ///
17 | /// 信息
18 | ///
19 | Info,
20 | ///
21 | /// 错误
22 | ///
23 | Error,
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/ToolBox.Http/ResultCookieType.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace ToolBox.Http
6 | {
7 | ///
8 | /// Cookite返回类型
9 | ///
10 | public enum ResultCookieType
11 | {
12 | ///
13 | /// 只返回字符串类型的Cookie
14 | ///
15 | String,
16 | ///
17 | /// CookieCollection格式的Cookie集合同时也返回String类型的cookie
18 | ///
19 | CookieCollection
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/ToolBox.Security/ToolBox.Security.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0
5 | 各种加密工具
6 | Aidenxl , Yulongjiang
7 | 1.0.3
8 |
9 |
10 |
11 | D:\project\toolbox\ToolBox\ToolBox.Security\ToolBox.Security.xml
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/ToolBox.Http/PostDataType.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace ToolBox.Http
6 | {
7 | ///
8 | /// Post的数据格式默认为string
9 | ///
10 | public enum PostDataType
11 | {
12 | ///
13 | /// 字符串类型,这时编码Encoding可不设置
14 | ///
15 | String,
16 | ///
17 | /// Byte类型,需要设置PostdataByte参数的值编码Encoding可设置为空
18 | ///
19 | Byte,
20 | ///
21 | /// 传文件,Postdata必须设置为文件的绝对路径,必须设置Encoding的值
22 | ///
23 | FilePath
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/ToolBox.String/ToolBox.String.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0
5 | Aidenxl YuLongJiang
6 | 1.0.1
7 |
8 |
9 |
10 | D:\project\toolbox\ToolBox\ToolBox.String\ToolBox.String.xml
11 |
12 |
13 |
14 |
15 | Always
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/ToolBox.Test/ToolBox.Test.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/ToolBox.Socket/ToolBox.Socket.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0
5 | Aidenxl YuLongJiang
6 | easy tcp,udp socket
7 | 1.1.0
8 |
9 |
10 |
11 | D:\project\toolbox\ToolBox\ToolBox.Socket\ToolBox.Socket.xml
12 |
13 |
14 |
15 |
16 | Always
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/ToolBox.Files/Drive.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 | using ToolBox.Log;
5 |
6 | namespace ToolBox.Files
7 | {
8 | public partial class FileUtil
9 | {
10 | ///
11 | /// 获取本地驱动器名列表
12 | ///
13 | ///
14 | public static string[] GetLocalDrives()
15 | {
16 | try
17 | {
18 | return System.IO.Directory.GetLogicalDrives();
19 | }
20 | catch (Exception ex)
21 | {
22 | ("获取本地驱动器名列表 错误" + ex.Message).WriteErrorLog();
23 | throw;
24 | }
25 |
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/ToolBox.Http/ToolBox.Http.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0
5 | Aidenxl YuLongJiang
6 | http请求工具类
7 | 1.0.2
8 |
9 |
10 |
11 | D:\project\toolbox\ToolBox\ToolBox.Http\ToolBox.Http.xml
12 |
13 |
14 |
15 | D:\project\toolbox\ToolBox\ToolBox.Http\ToolBox.Http.xml
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/ToolBox.Log/ToolBox.Log.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0
5 | 设置文件路径方法名变动,
6 | 一个超级简单的日志输出帮助类
7 | yulongjiang,Aidenxl
8 | 1.0.7
9 | 1.0.0.0
10 |
11 |
12 |
13 | D:\Projects\ToolBox\ToolBox\ToolBox.Log\ToolBox.Log.xml
14 |
15 |
16 |
17 |
18 | Always
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/ToolBox.Files/ToolBox.Files.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0
5 | Aidenxl YuLongJiang
6 | 1.0.3
7 | 文件工具类
8 | 整理工具类并加上异常检测和日志输出
9 |
10 |
11 |
12 | D:\project\toolbox\ToolBox\ToolBox.Files\ToolBox.Files.xml
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 | Always
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/ToolBox.Json/JsonHelper.cs:
--------------------------------------------------------------------------------
1 | using Newtonsoft.Json;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Text;
5 |
6 | namespace ToolBox.Json
7 | {
8 | public static class JsonHelper
9 | {
10 |
11 | ///
12 | /// 对象转json
13 | ///
14 | ///
15 | ///
16 | public static string ObjectToJson(this object obj)
17 | {
18 | return JsonConvert.SerializeObject(obj);
19 | }
20 |
21 | ///
22 | /// json转实体
23 | ///
24 | ///
25 | ///
26 | ///
27 | public static T JsonToModel(this string str)
28 | {
29 | return JsonConvert.DeserializeObject(str);
30 | }
31 |
32 |
33 |
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/ToolBox.Phone/PhoneModelToName.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Linq;
5 |
6 | namespace ToolBox.PhoneModelParse
7 | {
8 | ///
9 | /// 手机型号工具
10 | ///
11 | public class PhoneModelNumberTool
12 | {
13 | public string PhomeModels { get; set; }
14 | private List PhoneMoedls { get; set; }
15 | public PhoneModelNumberTool()
16 | {
17 | PhoneMoedls= new PhoneModelList().Init();
18 | }
19 |
20 | public string ModelNumberToName(string modelNumber)
21 | {
22 | return PhoneMoedls.FirstOrDefault(i => i.ModelNumber == modelNumber)?.Name ?? modelNumber;
23 | }
24 |
25 | }
26 |
27 |
28 | public class PhoneModel
29 | {
30 | public string ModelNumber { get; set; }
31 | public string Name { get; set; }
32 | public string Brand { get; set; }
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/ToolBox.Security/HexUtil.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace ToolBox.Security
6 | {
7 | public static class HexUtil
8 | {
9 | public static string ByteToHex(this byte[] bytes)
10 | {
11 | string returnStr = "";
12 | if (bytes != null)
13 | {
14 | for (int i = 0; i < bytes.Length; i++)
15 | {
16 | returnStr += bytes[i].ToString("X2");
17 | }
18 | }
19 | return returnStr;
20 | }
21 |
22 | public static byte[] HexToByte(this string hexString)
23 | {
24 | hexString = hexString.Replace(" ", "");
25 | if ((hexString.Length % 2) != 0)
26 | hexString += " ";
27 | byte[] returnBytes = new byte[hexString.Length / 2];
28 | for (int i = 0; i < returnBytes.Length; i++)
29 | returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
30 | return returnBytes;
31 | }
32 |
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/ToolBox.DateTime/ToolBox.DateTimeTool.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0
5 | ToolBox.DateTimeTool
6 | ToolBox.DateTimeTool
7 | 修复方法注释不显示
8 | yulongjiang,Aidenxl
9 | 1.0.2
10 | 时间工具扩展类
11 |
12 |
13 |
14 | D:\Projects\ToolBox\ToolBox\ToolBox.DateTime\ToolBox.DateTimeTool.xml
15 |
16 |
17 |
18 | D:\Projects\ToolBox\ToolBox\ToolBox.DateTime\ToolBox.DateTimeTool.xml
19 |
20 |
21 |
22 |
23 | Always
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/ToolBox.Framework.Test/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // 有关程序集的一般信息由以下
6 | // 控制。更改这些特性值可修改
7 | // 与程序集关联的信息。
8 | [assembly: AssemblyTitle("ToolBox.Framework.Test")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("ToolBox.Framework.Test")]
13 | [assembly: AssemblyCopyright("Copyright © 2019")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // 将 ComVisible 设置为 false 会使此程序集中的类型
18 | //对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
19 | //请将此类型的 ComVisible 特性设置为 true。
20 | [assembly: ComVisible(false)]
21 |
22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
23 | [assembly: Guid("ea2ae0de-3b56-4b8c-885c-668228872c55")]
24 |
25 | // 程序集的版本信息由下列四个值组成:
26 | //
27 | // 主版本
28 | // 次版本
29 | // 生成号
30 | // 修订号
31 | //
32 | // 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
33 | // 方法是按如下所示使用“*”: :
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/ToolBox.Reflection/EachHelper.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections;
3 | using System.Collections.Generic;
4 | using System.Text;
5 |
6 | namespace ToolBox.Reflection
7 | {
8 | public class EachHelper
9 | {
10 | public static void EachListHeader(object list, Action handle)
11 | {
12 | var index = 0;
13 | var dict = GenericUtil.GetListProperties(list);
14 | foreach (var item in dict)
15 | handle(index++, item.Key, item.Value);
16 | }
17 |
18 | public static void EachListRow(object list, Action handle)
19 | {
20 | var index = 0;
21 | IEnumerator enumerator = ((dynamic)list).GetEnumerator();
22 | while (enumerator.MoveNext())
23 | handle(index++, enumerator.Current);
24 | }
25 |
26 | public static void EachObjectProperty(object row, Action handle)
27 | {
28 | var index = 0;
29 | var dict = GenericUtil.GetDictionaryValues(row);
30 | foreach (var item in dict)
31 | handle(index++, item.Key, item.Value);
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/ToolBox.Security/Base64Helper.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace ToolBox.Security
6 | {
7 | public static class Base64Helper
8 | {
9 |
10 | ///
11 | /// Base64加密
12 | ///
13 | /// 需要加密的字符串
14 | /// 加密后的数据
15 | public static string Base64Encrypt(this string str, Encoding encoding = null)
16 | {
17 | if (encoding == null)
18 | {
19 | encoding = Encoding.UTF8;
20 | }
21 |
22 | byte[] encbuff = encoding.GetBytes(str);
23 | return Convert.ToBase64String(encbuff);
24 | }
25 |
26 | ///
27 | /// Base64解密
28 | ///
29 | /// 需要解密的字符串
30 | /// 解密后的数据
31 | public static string Base64Decrypt(this string str, Encoding encoding = null)
32 | {
33 | if (encoding == null)
34 | {
35 | encoding = Encoding.UTF8;
36 | }
37 |
38 | byte[] decbuff = Convert.FromBase64String(str);
39 | return encoding.GetString(decbuff);
40 | }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/ToolBox.Framework.Test/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 | using WebSocketSharp;
7 | using WebSocketSharp.Server;
8 | using ToolBox.Log;
9 | using ToolBox.Socket;
10 |
11 |
12 | namespace ToolBox.Framework.Test
13 | {
14 |
15 |
16 |
17 | class Program
18 | {
19 | static void Main(string[] args)
20 | {
21 |
22 | TcpClient tcpClient = new TcpClient();
23 | tcpClient.IsOpenDesEnc = false;
24 |
25 | tcpClient.SetEncryptKey("ddccbbaa");
26 |
27 | tcpClient.StartConnect(1988);
28 |
29 | tcpClient.OnSuccess = (s) =>
30 | {
31 |
32 | Console.WriteLine("成功");
33 |
34 | // tcpClient.SendMsg("4456465");
35 | };
36 |
37 | tcpClient.OnMessage = (s) =>
38 | {
39 |
40 | Console.WriteLine(s);
41 |
42 | };
43 |
44 | tcpClient.OnError = (s) =>
45 | {
46 | Console.WriteLine(s);
47 | };
48 |
49 |
50 | while (true) {
51 |
52 | string str= Console.ReadLine();
53 |
54 | tcpClient.SendMsg(str);
55 |
56 |
57 | }
58 |
59 |
60 |
61 | //wssv.Stop();
62 | }
63 |
64 |
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/ToolBox.Socket/ClientMode.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Net.Sockets;
4 | using System.Text;
5 | using System.Threading;
6 |
7 | namespace ToolBox.Socket
8 | {
9 | ///
10 | /// 服务器端的客户
11 | ///
12 | public class ClientMode
13 | {
14 |
15 | ///
16 | /// 客户端构造函数
17 | ///
18 | ///
19 | ///
20 | ///
21 | ///
22 | public ClientMode(string ip, Thread thread, System.Net.Sockets.Socket socket, string id)
23 | {
24 |
25 | this.ip = ip;
26 | this.socket = socket;
27 | this.thr = thread;
28 | this.id = id;
29 | lastTickTime = SocketTools.GetTimeStamp();
30 |
31 | }
32 |
33 |
34 | public string ip { get; set; } //客户端ip
35 | public string id { get; set; } //客户端id
36 | public Thread thr { get; set; } //客户端线程
37 | public System.Net.Sockets.Socket socket { get; set; } //客户端套接字
38 | public long lastTickTime { get; set; } = long.MinValue; //时间标签
39 |
40 |
41 |
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/ToolBox.Security/SHA256Helper.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Security.Cryptography;
4 | using System.Text;
5 |
6 | namespace ToolBox.Security
7 | {
8 | public static partial class SHA256Helper
9 | {
10 | ///
11 | /// SHA256函数
12 | ///
13 | /// /// 原始字符串
14 | /// SHA256结果
15 | public static string SHA256(string str)
16 | {
17 | byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
18 | SHA256Managed Sha256 = new SHA256Managed();
19 | byte[] Result = Sha256.ComputeHash(SHA256Data);
20 | return Convert.ToBase64String(Result); //返回长度为44字节的字符串
21 | }
22 |
23 | ///
24 | /// SHA256函数
25 | ///
26 | /// 原始字符串
27 | /// SHA256结果(16进制字节)
28 | public static string SHA256(this string str, Encoding encoding = null)
29 | {
30 | if (encoding == null)
31 | {
32 | encoding = Encoding.UTF8;
33 | }
34 |
35 | byte[] sha256Data = encoding.GetBytes(str);
36 | var sha256 = new SHA256Managed();
37 | byte[] result = sha256.ComputeHash(sha256Data);
38 |
39 | return result.ByteToHex().ToLower();
40 |
41 | //return Convert.ToBase64String(result); //返回base64
42 | }
43 |
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/ToolBox.Reflection/PropertyExpressionParser.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq.Expressions;
4 | using System.Reflection;
5 | using System.Text;
6 |
7 | namespace ToolBox.Reflection
8 | {
9 | public class PropertyExpressionParser
10 | {
11 | private readonly object _item;
12 | private PropertyInfo _property;
13 |
14 | public PropertyExpressionParser(object item, Expression> propertyExpression)
15 | {
16 | _item = item;
17 | _property = GetProperty(propertyExpression);
18 | }
19 |
20 | private static PropertyInfo GetProperty(Expression> exp)
21 | {
22 | PropertyInfo result;
23 | if (exp.Body.NodeType == ExpressionType.Convert)
24 | result = ((MemberExpression)((UnaryExpression)exp.Body).Operand).Member as PropertyInfo;
25 | else result = ((MemberExpression)exp.Body).Member as PropertyInfo;
26 |
27 | if (result != null)
28 | return typeof(T).GetProperty(result.Name);
29 |
30 | throw new ArgumentException(string.Format("Expression '{0}' does not refer to a property.", exp.ToString()));
31 | }
32 |
33 | public object Value
34 | {
35 | get { return ReflectionUtil.GetPropertyValue(_item, _property); }
36 | }
37 |
38 | public string Name
39 | {
40 | get { return _property.Name; }
41 | }
42 |
43 | public Type Type
44 | {
45 | get { return ReflectionUtil.GetPropertyType(_property); }
46 | }
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/ToolBox.Http/HttpResult.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Net;
4 | using System.Net.Http.Headers;
5 | using System.Text;
6 |
7 | namespace ToolBox.Http
8 | {
9 | ///
10 | /// 请求结果
11 | ///
12 | public class HttpResult
13 | {
14 | ///
15 | /// cookite
16 | ///
17 | public string Cookie { get; set; }
18 |
19 |
20 | ///
21 | /// html
22 | ///
23 | public string Html { get; set; } = string.Empty;
24 |
25 | ///
26 | /// 返回的Byte数组 只有ResultType.Byte时才返回数据,其它情况为空
27 | ///
28 | public byte[] ResultByte { get; set; }
29 |
30 | ///
31 | /// header对象
32 | ///
33 | public HttpContentHeaders Header { get; set; }
34 |
35 |
36 | ///
37 | ///http 是否响应成功
38 | ///
39 | public bool IsSuccessStatusCode { get; set; }
40 |
41 |
42 | ///
43 | /// http响应 描述信息
44 | ///
45 | public string StatusDescription { get; set; }
46 |
47 |
48 | ///
49 | /// 返回状态码,默认为OK
50 | ///
51 | public HttpStatusCode StatusCode { get; set; }
52 |
53 |
54 | ///
55 | /// 最后访问的URl
56 | ///
57 | public string ResponseUri { get; set; }
58 |
59 |
60 | ///
61 | /// 获取重定向的URl
62 | ///
63 | public string RedirectUrl
64 | {
65 | get; set;
66 | }
67 |
68 |
69 |
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/ToolBox.Security/HashHelper.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Security.Cryptography;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace CommomTools
9 | {
10 |
11 | ///
12 | /// 得到随机安全码(哈希加密)。
13 | ///
14 | public static class HashHelper
15 | {
16 |
17 |
18 | ///
19 | /// 得到随机哈希加密字符串
20 | ///
21 | ///
22 | public static string GetSecurity()
23 | {
24 | string Security = HashEncoding(GetRandomValue());
25 | return Security;
26 | }
27 | ///
28 | /// 得到一个随机数值
29 | ///
30 | ///
31 | public static string GetRandomValue()
32 | {
33 | Random Seed = new Random();
34 | string RandomVaule = Seed.Next(1, int.MaxValue).ToString();
35 | return RandomVaule;
36 | }
37 |
38 | ///
39 | /// 哈希加密一个字符串
40 | ///
41 | /// 需要加密的字符串
42 | /// 加密后的数据
43 | public static string HashEncoding(this string security)
44 | {
45 | var code = new UnicodeEncoding();
46 | byte[] message = code.GetBytes(security);
47 | var arithmetic = new SHA512Managed();
48 | var value = arithmetic.ComputeHash(message);
49 | var sb = new StringBuilder();
50 | foreach (byte o in value)
51 | {
52 | sb.Append((int)o + "O");
53 | }
54 |
55 | return sb.ToString();
56 | }
57 |
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/ToolBox.Reflection/ObjectMapper.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Reflection;
5 | using System.Text;
6 |
7 | namespace ToolBox.Reflection
8 | {
9 | ///
10 | /// Object对象工具类
11 | ///
12 | public class ObjectMapper
13 | {
14 | public static IList GetMapperProperties(Type sourceType, Type targetType)
15 | {
16 | var sourceProperties = sourceType.GetProperties();
17 | var targetProperties = targetType.GetProperties();
18 |
19 | return (from s in sourceProperties
20 | from t in targetProperties
21 | where s.Name == t.Name && s.CanRead && t.CanWrite && s.PropertyType == t.PropertyType
22 | select new PropertyMapper
23 | {
24 | SourceProperty = s,
25 | TargetProperty = t
26 | }).ToList();
27 | }
28 |
29 | ///
30 | /// 复制对象,将source对象赋值到target对象中
31 | ///
32 | /// 源对象
33 | /// 目标对象
34 | public static void CopyProperties(object source, object target)
35 | {
36 | var sourceType = source.GetType();
37 | var targetType = target.GetType();
38 | var mapperProperties = GetMapperProperties(sourceType, targetType);
39 |
40 | for (int index = 0, count = mapperProperties.Count; index < count; index++)
41 | {
42 | var property = mapperProperties[index];
43 | var sourceValue = property.SourceProperty.GetValue(source, null);
44 | property.TargetProperty.SetValue(target, sourceValue, null);
45 | }
46 | }
47 | }
48 |
49 | public class PropertyMapper
50 | {
51 | public PropertyInfo SourceProperty { get; set; }
52 | public PropertyInfo TargetProperty { get; set; }
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/ToolBox.Common/RandomHelper.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace ToolBox.Common
6 | {
7 | ///
8 | /// 使用Random类生成伪随机数
9 | ///
10 | public class RandomHelper
11 | {
12 | //随机数对象
13 | private Random _random;
14 |
15 | #region 构造函数
16 | ///
17 | /// 构造函数
18 | ///
19 | public RandomHelper()
20 | {
21 | //为随机数对象赋值
22 | this._random = new Random();
23 | }
24 | #endregion
25 |
26 | #region 生成一个指定范围的随机整数
27 | ///
28 | /// 生成一个指定范围的随机整数,该随机数范围包括最小值,但不包括最大值
29 | ///
30 | /// 最小值
31 | /// 最大值
32 | public int GetRandomInt(int minNum, int maxNum)
33 | {
34 | return this._random.Next(minNum, maxNum);
35 | }
36 | #endregion
37 |
38 | #region 生成一个0.0到1.0的随机小数
39 | ///
40 | /// 生成一个0.0到1.0的随机小数
41 | ///
42 | public double GetRandomDouble()
43 | {
44 | return this._random.NextDouble();
45 | }
46 | #endregion
47 |
48 | #region 对一个数组进行随机排序
49 | ///
50 | /// 对一个数组进行随机排序
51 | ///
52 | /// 数组的类型
53 | /// 需要随机排序的数组
54 | public void GetRandomArray(T[] arr)
55 | {
56 | //对数组进行随机排序的算法:随机选择两个位置,将两个位置上的值交换
57 |
58 | //交换的次数,这里使用数组的长度作为交换次数
59 | int count = arr.Length;
60 |
61 | //开始交换
62 | for (int i = 0; i < count; i++)
63 | {
64 | //生成两个随机数位置
65 | int randomNum1 = GetRandomInt(0, arr.Length);
66 | int randomNum2 = GetRandomInt(0, arr.Length);
67 |
68 | //定义临时变量
69 | T temp;
70 |
71 | //交换两个随机数位置的值
72 | temp = arr[randomNum1];
73 | arr[randomNum1] = arr[randomNum2];
74 | arr[randomNum2] = temp;
75 | }
76 | }
77 | #endregion
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/ToolBox.Security/Md5Helper.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Security.Cryptography;
5 | using System.Text;
6 |
7 | namespace ToolBox.Security
8 | {
9 | public static class Md5Helper
10 | {
11 | ///
12 | /// MD5函数,需引用:using System.Security.Cryptography;
13 | ///
14 | /// 原始字符串
15 | /// MD5结果
16 | public static string MD5(this string str)
17 | {
18 | //微软md5方法参考return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "md5");
19 | byte[] b = Encoding.Default.GetBytes(str);
20 | b = new MD5CryptoServiceProvider().ComputeHash(b);
21 | string ret = "";
22 | for (int i = 0; i < b.Length; i++)
23 | ret += b[i].ToString("x").PadLeft(2, '0');
24 | return ret;
25 | }
26 |
27 |
28 |
29 | ///
30 | /// MD5加密byte
31 | ///
32 | /// 字节数组
33 | /// md5加密结果
34 | public static string MD5(this byte[] bytes)
35 | {
36 | var md5 = new MD5CryptoServiceProvider();
37 | byte[] hash = md5.ComputeHash(bytes, 0, bytes.Length);
38 | StringBuilder sb = new StringBuilder();
39 | foreach (byte value in hash)
40 | {
41 | sb.AppendFormat("{0:x2}", value);
42 | }
43 | return sb.ToString();
44 | }
45 |
46 |
47 | ///
48 | /// 读取文件MD5值
49 | ///
50 | /// 文件路径
51 | /// MD5
52 | public static string FileMd5(string filePath)
53 | {
54 | var cfilePath = filePath + "e";
55 | if (File.Exists(cfilePath))
56 | File.Delete(cfilePath);
57 | File.Copy(filePath, cfilePath);//复制一份,防止占用
58 |
59 |
60 | if (File.Exists(cfilePath))
61 | {
62 | var buffer = File.ReadAllBytes(cfilePath);
63 | System.IO.File.Delete(cfilePath);
64 | return buffer.MD5();
65 | }
66 | else
67 | {
68 | throw new Exception("读取文件MD5出错!");
69 | }
70 | }
71 |
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/ToolBox.Security/CryptHelper.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Linq;
5 | using System.Security.Cryptography;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace ToolBox.Security
10 | {
11 | ///
12 | /// 简单加密类
13 | ///
14 | public static class CryptHelper
15 | {
16 |
17 |
18 | ///
19 | /// 文本_加密 简单加密
20 | ///
21 | /// 待加密的文本
22 | /// 加密的密码
23 | ///
24 | public static string StringEncrypt(string str, string pass, Encoding encoding = null)
25 | {
26 | if (encoding == null)
27 | {
28 | encoding = Encoding.UTF8;
29 | }
30 |
31 | byte[] bin = encoding.GetBytes(str);
32 | List list = new List();
33 | for (int i = 0; i < bin.Length; i++)
34 | {
35 | var ch = (byte)(bin[i] ^ 3600);
36 | list.Add(ch);
37 | }
38 |
39 |
40 |
41 | string md5 = pass.MD5().Substring(2, 9);
42 |
43 | string hex =list.ToArray().ByteToHex();
44 |
45 |
46 | return hex + md5.ToUpper();
47 | }
48 |
49 | ///
50 | /// 文本解密 (对应易语言模块)
51 | ///
52 | /// 待解密的文本
53 | /// 解密的密文
54 | ///
55 | public static string StringDecrypt(string str, string pass, Encoding encoding = null)
56 | {
57 | if (encoding == null)
58 | {
59 | encoding = Encoding.UTF8;
60 | }
61 |
62 |
63 |
64 | string md5 = pass.MD5().Substring(2, 9).ToUpper();
65 | if (!str.EndsWith(md5))
66 | {
67 | return "";
68 | }
69 |
70 | string item = str.Substring(0, str.Length - 9);
71 |
72 | byte[] bin = item.HexToByte();
73 | List list = new List();
74 | for (int i = 0; i < bin.Length; i++)
75 | {
76 | var ch = (byte)(bin[i] ^ 3600);
77 | list.Add(ch);
78 | }
79 |
80 | string html = encoding.GetString(list.ToArray());
81 |
82 | return html;
83 | }
84 |
85 |
86 |
87 |
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/ToolBox.String/StringOther.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Drawing;
4 | using System.Text;
5 | using System.Text.RegularExpressions;
6 |
7 | namespace ToolBox.String
8 | {
9 | public partial class StringUtil
10 | {
11 |
12 |
13 |
14 |
15 | ///
16 | /// 将字符串转换为Color
17 | ///
18 | /// 字符串颜色,如:#ccc
19 | /// 返回Color对象
20 | public static Color ToColor(string color)
21 | {
22 | int red, green, blue = 0;
23 | char[] rgb;
24 | color = color.TrimStart('#');
25 | color = Regex.Replace(color.ToLower(), "[g-zG-Z]", "");
26 | switch (color.Length)
27 | {
28 | case 3:
29 | rgb = color.ToCharArray();
30 | red = Convert.ToInt32(rgb[0].ToString() + rgb[0].ToString(), 16);
31 | green = Convert.ToInt32(rgb[1].ToString() + rgb[1].ToString(), 16);
32 | blue = Convert.ToInt32(rgb[2].ToString() + rgb[2].ToString(), 16);
33 | return Color.FromArgb(red, green, blue);
34 | case 6:
35 | rgb = color.ToCharArray();
36 | red = Convert.ToInt32(rgb[0].ToString() + rgb[1].ToString(), 16);
37 | green = Convert.ToInt32(rgb[2].ToString() + rgb[3].ToString(), 16);
38 | blue = Convert.ToInt32(rgb[4].ToString() + rgb[5].ToString(), 16);
39 | return Color.FromArgb(red, green, blue);
40 | default:
41 | return Color.FromName(color);
42 |
43 | }
44 | }
45 |
46 | ///
47 | /// 格式化字节数字符串(用于显示文件大小)
48 | ///
49 | /// 字节
50 | /// 返回string
51 | public static string FormatBytesStr(int bytes)
52 | {
53 | if (bytes > 1073741824)
54 | {
55 | return ((double)(bytes / 1073741824)).ToString("0") + "G";
56 | }
57 | if (bytes > 1048576)
58 | {
59 | return ((double)(bytes / 1048576)).ToString("0") + "M";
60 | }
61 | if (bytes > 1024)
62 | {
63 | return ((double)(bytes / 1024)).ToString("0") + "K";
64 | }
65 | return bytes.ToString() + "Bytes";
66 | }
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/ToolBox.String/SqlFilterHelper.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 | using System.Text.RegularExpressions;
5 |
6 | namespace ToolBox.String
7 | {
8 | ///
9 | /// 防注入过滤函数
10 | ///
11 | public static class SqlFilterHelper
12 | {
13 | ///
14 | /// 防注入过滤函数
15 | ///
16 | ///需要过滤字符串
17 | ///过滤后的字符串
18 | public static string Filter(string inputString)
19 | {
20 | if (inputString != "")
21 | {
22 | string sql = SqlFilters(inputString);
23 | if (sql == "")
24 | {
25 | sql = "敏感字符";
26 | }
27 | return sql;
28 | }
29 | else
30 | {
31 | return inputString;
32 | }
33 | }
34 |
35 | ///
36 | /// 过滤字符串中注入SQL脚本的方法
37 | ///
38 | ///传入的字符串
39 | ///过滤后的字符串
40 | private static string SqlFilters(string source)
41 | {
42 | //半角括号替换为全角括号
43 | source = source.Replace("'", "'''").Replace(";", ";").Replace("(", "(").Replace(")", ")");
44 | //去除执行SQL语句的命令关键字
45 | source = Regex.Replace(source, "select", "", RegexOptions.IgnoreCase);
46 | source = Regex.Replace(source, "insert", "", RegexOptions.IgnoreCase);
47 | source = Regex.Replace(source, "update", "", RegexOptions.IgnoreCase);
48 | source = Regex.Replace(source, "delete", "", RegexOptions.IgnoreCase);
49 | source = Regex.Replace(source, "drop", "", RegexOptions.IgnoreCase);
50 | source = Regex.Replace(source, "truncate", "", RegexOptions.IgnoreCase);
51 | source = Regex.Replace(source, "declare", "", RegexOptions.IgnoreCase);
52 | source = Regex.Replace(source, "xp_cmdshell", "", RegexOptions.IgnoreCase);
53 | source = Regex.Replace(source, "/add", "", RegexOptions.IgnoreCase);
54 | source = Regex.Replace(source, "net user", "", RegexOptions.IgnoreCase);
55 | //去除执行存储过程的命令关键字
56 | source = Regex.Replace(source, "exec", "", RegexOptions.IgnoreCase);
57 | source = Regex.Replace(source, "execute", "", RegexOptions.IgnoreCase);
58 | //去除系统存储过程或扩展存储过程关键字
59 | source = Regex.Replace(source, "xp_", "x p_", RegexOptions.IgnoreCase);
60 | source = Regex.Replace(source, "sp_", "s p_", RegexOptions.IgnoreCase);
61 | //防止16进制注入
62 | source = Regex.Replace(source, "0x", "0 x", RegexOptions.IgnoreCase);
63 | return source;
64 | }
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/ToolBox.Object/Serialize.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Linq;
5 | using System.Runtime.Serialization.Formatters.Binary;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 | using System.Xml;
9 | using System.Xml.Serialization;
10 |
11 | namespace ToolBox.Object
12 | {
13 | public static class Serialize
14 | {
15 | ///
16 | /// 获取对象序列化的二进制版本
17 | ///
18 | /// 对象实体
19 | /// 如果对象实体为Null,则返回结果为Null。
20 | public static byte[] GetBytes(object pObj)
21 | {
22 | if (pObj == null) { return null; }
23 | MemoryStream serializationStream = new MemoryStream();
24 | new BinaryFormatter().Serialize(serializationStream, pObj);
25 | serializationStream.Position = 0L;
26 | byte[] buffer = new byte[serializationStream.Length];
27 | serializationStream.Read(buffer, 0, buffer.Length);
28 | serializationStream.Close();
29 | return buffer;
30 | }
31 |
32 | ///
33 | /// 获取对象序列化的XmlDocument版本
34 | ///
35 | /// 对象实体
36 | /// 如果对象实体为Null,则返回结果为Null。
37 | public static XmlDocument GetXmlDoc(object pObj)
38 | {
39 | if (pObj == null) { return null; }
40 | XmlSerializer serializer = new XmlSerializer(pObj.GetType());
41 | StringBuilder sb = new StringBuilder();
42 | StringWriter writer = new StringWriter(sb);
43 | serializer.Serialize((TextWriter)writer, pObj);
44 | XmlDocument document = new XmlDocument();
45 | document.LoadXml(sb.ToString());
46 | writer.Close();
47 | return document;
48 | }
49 |
50 | ///
51 | /// 从已序列化数据中(byte[])获取对象实体
52 | ///
53 | /// 要返回的数据类型
54 | /// 二进制数据
55 | /// 对象实体
56 | public static T GetObject(byte[] binData)
57 | {
58 | if (binData == null) { return default(T); }
59 | BinaryFormatter formatter = new BinaryFormatter();
60 | MemoryStream serializationStream = new MemoryStream(binData);
61 | return (T)formatter.Deserialize(serializationStream);
62 | }
63 |
64 | ///
65 | /// 从已序列化数据(XmlDocument)中获取对象实体
66 | ///
67 | /// 要返回的数据类型
68 | /// 已序列化的文档对象
69 | /// 对象实体
70 | public static T GetObject(XmlDocument xmlDoc)
71 | {
72 | if (xmlDoc == null) { return default(T); }
73 | XmlNodeReader xmlReader = new XmlNodeReader(xmlDoc.DocumentElement);
74 | XmlSerializer serializer = new XmlSerializer(typeof(T));
75 | return (T)serializer.Deserialize(xmlReader);
76 | }
77 |
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/ToolBox.Test/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using ToolBox.Http;
3 | using ToolBox.DateTimeTool;
4 | using ToolBox.Log;
5 | using System.Threading.Tasks;
6 | using ToolBox.Socket;
7 | using System.IO;
8 | using ToolBox.UserAgentParse;
9 | using System.Collections.Generic;
10 |
11 | namespace ToolBox.Test
12 | {
13 | class Program
14 | {
15 | static void Main(string[] args)
16 | {
17 | List aa = new List();
18 |
19 | using (StreamReader sr = new StreamReader(@"C:\Users\Administrator\Desktop\tt.txt"))
20 | {
21 | var txt = sr.ReadLine();
22 | var count = 1;
23 | while (!string.IsNullOrEmpty(txt))
24 | {
25 | var ua = new UaUnit(txt).Parse();
26 | Console.WriteLine($"{count}.浏览器内核:{ua.BrowserKernel}\r\n浏览器名称:{ua.BrowserName}\r\n浏览器版本:{ua.BrowserVersion}\r\n手机型号:{ua.PhoneModelName}\r\n手机型号代码:{ua.PhoneModelCode}\r\n平台:{ua.Platform}\r\n操作系统:{ua.SystemName}\r\n操作系统版本:{ua.SystemVersion}\r\n");
27 | count++;
28 | txt = sr.ReadLine();
29 | }
30 | }
31 | using (StreamWriter sw = new StreamWriter(@"C:\Users\Administrator\Desktop\tt1.txt"))
32 | {
33 | foreach (var item in aa)
34 | {
35 | sw.WriteLine(item);
36 | }
37 | }
38 | Console.WriteLine("wanshier");
39 | Console.ReadKey();
40 | }
41 |
42 | private static void TcpServer_OnRecMessage(object sender, SocketMsgArgs e)
43 | {
44 | Console.WriteLine($"接收到{e.clientRecInfo.ip} + {e.clientRecInfo.msg}");
45 |
46 |
47 | }
48 |
49 | private static void TcpServer_OnClientClose(object sender, SocketArgs e)
50 | {
51 | Console.WriteLine($"退出了{e.ClientInfo.id} + {e.ClientInfo.ip}");
52 | }
53 |
54 | private static void TcpServer_OnClientAdd(object sender, SocketArgs e)
55 | {
56 | Console.WriteLine($"添加了{e.ClientInfo.id} + {e.ClientInfo.ip}");
57 | }
58 |
59 | public enum test
60 | {
61 | test1,
62 | test2
63 | }
64 |
65 | private static void RunTask()
66 | {
67 | Task.Run(() =>
68 | {
69 | "test1".WriteErrorLog();
70 | "test2".WriteErrorLog();
71 | "test3".WriteErrorLog();
72 | "test4".WriteErrorLog();
73 | "test5".WriteErrorLog();
74 | "test6".WriteErrorLog();
75 | "test7".WriteErrorLog();
76 | "test8".WriteErrorLog();
77 | "test9".WriteErrorLog();
78 | "test10".WriteErrorLog();
79 | "test11".WriteErrorLog();
80 | "test1".WriteDebugLog();
81 | "test2".WriteDebugLog();
82 | "test3".WriteDebugLog();
83 | "test4".WriteDebugLog();
84 | "test5".WriteDebugLog();
85 | "test6".WriteDebugLog();
86 | "test7".WriteDebugLog();
87 | "test8".WriteDebugLog();
88 | "test9".WriteDebugLog();
89 | "test10".WriteDebugLog();
90 | "test11".WriteDebugLog();
91 | });
92 | }
93 | }
94 | }
95 |
--------------------------------------------------------------------------------
/ToolBox.Log/ToolBox.Log.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | ToolBox.Log
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 | 设置日志文件夹路径,true是成功,flase是失败(默认软件运行目录下log文件夹中写入)
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 |
--------------------------------------------------------------------------------
/ToolBox/ToolBox.Log/ToolBox.Log.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | ToolBox.Log
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 | 设置日志文件夹路径,true是成功,flase是失败(默认软件运行目录下log文件夹中写入)
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 |
--------------------------------------------------------------------------------
/ToolBox.Socket/ServerCallBack.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace ToolBox.Socket
6 | {
7 |
8 | ///
9 | /// Socket info args
10 | ///
11 | public class SocketArgs : EventArgs
12 | {
13 | ///
14 | /// clinet info
15 | ///
16 | public ClientInfo ClientInfo { get; set; }
17 |
18 | ///
19 | ///
20 | ///
21 | ///
22 | public SocketArgs(ClientInfo ClientInfo)
23 | {
24 |
25 | this.ClientInfo = ClientInfo;
26 | }
27 |
28 | }
29 |
30 | ///
31 | /// clinet info
32 | ///
33 | public class ClientInfo {
34 |
35 | ///
36 | /// id
37 | ///
38 | public string id { get; set; }
39 |
40 | ///
41 | /// ip
42 | ///
43 | public string ip { get; set; }
44 | }
45 |
46 |
47 |
48 | ///
49 | /// SocketMsgArgs
50 | ///
51 | public class SocketMsgArgs : EventArgs
52 | {
53 |
54 | public ClientRecInfo clientRecInfo { get; set; }
55 |
56 | ///
57 | /// /
58 | ///
59 | ///
60 | public SocketMsgArgs(ClientRecInfo clientRecInfo)
61 | {
62 |
63 | this.clientRecInfo = clientRecInfo;
64 | }
65 |
66 | }
67 |
68 |
69 | ///
70 | /// ClientRecInfo
71 | ///
72 | public class ClientRecInfo {
73 |
74 | ///
75 | /// ip
76 | ///
77 | public string ip { get; set; }
78 |
79 | ///
80 | /// msg
81 | ///
82 | public string msg { get; set; }
83 |
84 | }
85 |
86 |
87 |
88 | public partial class TcpServer
89 | {
90 |
91 | ///
92 | /// all debug info Output
93 | ///
94 | public Action OnDebug { get; set; }
95 |
96 | ///
97 | /// 成功启动后的回调函数
98 | ///
99 | public Action OnSuccess { get; set; }
100 |
101 |
102 | /////
103 | ///// 处理接收消息的回调函数
104 | /////
105 | //public Action OnRecMessage { get; set; }
106 |
107 | ///
108 | /// 处理消息回调函数
109 | ///
110 | public Action OnMessage { get; set; }
111 |
112 |
113 | ///
114 | /// 处理接收消息的回调函数
115 | ///
116 | public event EventHandler OnRecMessage;
117 |
118 |
119 |
120 | ///
121 | /// 客户端关闭的回调函数
122 | ///
123 | public event EventHandler OnClientClose;
124 |
125 |
126 | ///
127 | /// 添加一个用客户端后的回调函数
128 | ///
129 | public event EventHandler OnClientAdd;
130 |
131 |
132 |
133 | ///
134 | /// 异常输出的回调函数
135 | ///
136 | public Action OnError { get; set; }
137 |
138 |
139 |
140 |
141 | }
142 | }
143 |
--------------------------------------------------------------------------------
/ToolBox.Reflection/GenericUtil.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Dynamic;
4 | using System.Text;
5 |
6 | namespace ToolBox.Reflection
7 | {
8 | ///
9 | /// 通用工具类
10 | ///
11 | public class GenericUtil
12 | {
13 | public static bool IsDynamicType(Type type)
14 | {
15 | return type.Equals(typeof(ExpandoObject)) || type.Equals(typeof(object));
16 | }
17 |
18 | public static bool IsNullableType(Type theType)
19 | {
20 | return (theType.IsGenericType && theType.
21 | GetGenericTypeDefinition().Equals
22 | (typeof(Nullable<>)));
23 | }
24 |
25 | public static Type GetGenericType(object list)
26 | {
27 | return list.GetType().GetGenericArguments()[0];
28 | }
29 |
30 | public static bool IsTypeIgoreNullable(object value)
31 | {
32 | if (null == value) return false;
33 |
34 | Type type = value.GetType();
35 | if (type.IsGenericType)
36 | type = type.GetGenericArguments()[0];
37 |
38 | return type.Equals(typeof(T));
39 | }
40 |
41 | public static T CreateNew()
42 | {
43 | if (IsDynamicType(typeof(T)))
44 | return (T)(IDictionary)new ExpandoObject();
45 |
46 | return Activator.CreateInstance();
47 | }
48 |
49 | public static object GetValue(object item, string name)
50 | {
51 | if (IsDynamicType(item.GetType()))
52 | return ReflectionUtil.GetPropertyValueDynamic(item, name);
53 |
54 | return ReflectionUtil.GetPropertyValue(item, name);
55 | }
56 |
57 | public static void SetValue(object item, string name, object value)
58 | {
59 | Type type = item.GetType();
60 | if (IsDynamicType(type))
61 | ((IDictionary)item).Add(name, value);
62 |
63 | var property = type.GetProperty(name);
64 | property.SetValue(item, value, null);
65 | }
66 |
67 | public static Dictionary GetListProperties(dynamic list)
68 | {
69 | var type = GenericUtil.GetGenericType(list);
70 | var names = new Dictionary();
71 |
72 | if (GenericUtil.IsDynamicType(type))
73 | {
74 | if (list.Count > 0)
75 | foreach (var item in GenericUtil.GetDictionaryValues(list[0]))
76 | names.Add(item.Key, (item.Value ?? string.Empty).GetType());
77 | }
78 | else
79 | {
80 | foreach (var p in ReflectionUtil.GetProperties(type))
81 | names.Add(p.Value.Name, p.Value.PropertyType);
82 | }
83 |
84 | return names;
85 | }
86 |
87 | public static IDictionary GetDictionaryValues(object item)
88 | {
89 | if (IsDynamicType(item.GetType()))
90 | return item as IDictionary;
91 |
92 | var expando = (IDictionary)new ExpandoObject();
93 | var properties = ReflectionUtil.GetProperties(item.GetType());
94 | foreach (var p in properties)
95 | expando.Add(p.Value.Name, p.Value.GetValue(item, null));
96 | return expando;
97 | }
98 | }
99 | }
100 |
--------------------------------------------------------------------------------
/ToolBox.Cmd/CmdHelper.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Diagnostics;
4 | using System.Text;
5 |
6 | namespace ToolBox.Cmd
7 | {
8 | ///
9 | /// 执行命令
10 | ///
11 | public class CmdHelper
12 | {
13 | ///
14 | /// 执行cmd.exe命令
15 | ///
16 | ///命令文本
17 | /// 命令输出文本
18 | public static string ExeCommand(string commandText)
19 | {
20 | return ExeCommand(new string[] { commandText });
21 | }
22 | ///
23 | /// 执行多条cmd.exe命令
24 | ///
25 | ///命令文本数组
26 | /// 命令输出文本
27 | public static string ExeCommand(string[] commandTexts)
28 | {
29 | Process p = new Process();
30 | p.StartInfo.FileName = "cmd.exe";
31 | p.StartInfo.UseShellExecute = false;
32 | p.StartInfo.RedirectStandardInput = true;
33 | p.StartInfo.RedirectStandardOutput = true;
34 | p.StartInfo.RedirectStandardError = true;
35 | p.StartInfo.CreateNoWindow = true;
36 | string strOutput = null;
37 | try
38 | {
39 | p.Start();
40 | foreach (string item in commandTexts)
41 | {
42 | p.StandardInput.WriteLine(item);
43 | }
44 | p.StandardInput.WriteLine("exit");
45 | strOutput = p.StandardOutput.ReadToEnd();
46 | //strOutput = Encoding.UTF8.GetString(Encoding.Default.GetBytes(strOutput));
47 | p.WaitForExit();
48 | p.Close();
49 | }
50 | catch (Exception e)
51 | {
52 | strOutput = e.Message;
53 | }
54 | return strOutput;
55 | }
56 | ///
57 | /// 启动外部Windows应用程序,隐藏程序界面
58 | ///
59 | ///应用程序路径名称
60 | /// true表示成功,false表示失败
61 | public static bool StartApp(string appName)
62 | {
63 | return StartApp(appName, ProcessWindowStyle.Hidden);
64 | }
65 | ///
66 | /// 启动外部应用程序
67 | ///
68 | ///应用程序路径名称
69 | ///进程窗口模式
70 | /// true表示成功,false表示失败
71 | public static bool StartApp(string appName, ProcessWindowStyle style)
72 | {
73 | return StartApp(appName, null, style);
74 | }
75 | ///
76 | /// 启动外部应用程序,隐藏程序界面
77 | ///
78 | ///应用程序路径名称
79 | ///启动参数
80 | /// true表示成功,false表示失败
81 | public static bool StartApp(string appName, string arguments)
82 | {
83 | return StartApp(appName, arguments, ProcessWindowStyle.Hidden);
84 | }
85 | ///
86 | /// 启动外部应用程序
87 | ///
88 | ///应用程序路径名称
89 | ///启动参数
90 | ///进程窗口模式
91 | /// true表示成功,false表示失败
92 | public static bool StartApp(string appName, string arguments, ProcessWindowStyle style)
93 | {
94 | bool blnRst = false;
95 | Process p = new Process();
96 | p.StartInfo.FileName = appName;//exe,bat and so on
97 | p.StartInfo.WindowStyle = style;
98 | p.StartInfo.Arguments = arguments;
99 | try
100 | {
101 | p.Start();
102 | p.WaitForExit();
103 | p.Close();
104 | blnRst = true;
105 | }
106 | catch
107 | {
108 | }
109 | return blnRst;
110 | }
111 |
112 | }
113 | }
114 |
--------------------------------------------------------------------------------
/ToolBox.Security/DesHelper.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Security.Cryptography;
5 | using System.Text;
6 |
7 | namespace ToolBox.Security
8 | {
9 |
10 | ///
11 | /// DES加密/解密工具类。
12 | ///
13 | public static class DesHelper
14 | {
15 |
16 | public static string encryptKey = "km4250km";
17 |
18 | #region ========加密========
19 |
20 | ///
21 | /// DES【加密】字符串,使用缺省密钥。
22 | ///
23 | /// 字符串
24 | /// 返回string
25 | public static string Encrypt(this string text)
26 | {
27 | return Encrypt(text, encryptKey);
28 | }
29 |
30 | ///
31 | /// DES【加密】字符串,使用给定密钥。
32 | ///
33 | /// 字符串
34 | /// 密钥字符串
35 | /// 返回string
36 | public static string Encrypt(string text, string key)
37 | {
38 | try
39 | {
40 | byte[] rgbKey = Encoding.UTF8.GetBytes(key.Substring(0, 8));
41 | //rgbIV与rgbKey可以不一样,这里只是为了简便,读者可以自行修改
42 | byte[] rgbIV = rgbKey;// Encoding.UTF8.GetBytes(key.Substring(0, 8));
43 | byte[] inputByteArray = Encoding.UTF8.GetBytes(text);
44 | DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
45 | MemoryStream mStream = new MemoryStream();
46 | CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
47 | cStream.Write(inputByteArray, 0, inputByteArray.Length);
48 | cStream.FlushFinalBlock();
49 | return Convert.ToBase64String(mStream.ToArray());
50 | }
51 | catch
52 | {
53 | return text;
54 | }
55 |
56 | }
57 |
58 | #endregion
59 |
60 | #region ========解密========
61 |
62 |
63 | ///
64 | /// DES【解密】字符串,使用缺省密钥。
65 | ///
66 | /// 字符串
67 | /// 返回string
68 | public static string Decrypt(this string text)
69 | {
70 | return Decrypt(text, encryptKey);
71 |
72 | }
73 |
74 | ///
75 | /// DES【解密】字符串,使用给定密钥。
76 | ///
77 | /// 字符串
78 | /// 密钥字符串
79 | /// 返回string
80 | public static string Decrypt(string text, string key)
81 | {
82 | try
83 | {
84 | byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));
85 | byte[] keyIV = keyBytes;
86 | byte[] inputByteArray = Convert.FromBase64String(text);
87 | DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
88 | MemoryStream mStream = new MemoryStream();
89 | CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write);
90 | cStream.Write(inputByteArray, 0, inputByteArray.Length);
91 | cStream.FlushFinalBlock();
92 | return Encoding.UTF8.GetString(mStream.ToArray());
93 | }
94 | catch (Exception ex)
95 | {
96 | return "";
97 |
98 | }
99 |
100 |
101 | }
102 |
103 | #endregion
104 |
105 | }
106 | }
107 |
--------------------------------------------------------------------------------
/ToolBox.Framework.Test/ToolBox.Framework.Test.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {EA2AE0DE-3B56-4B8C-885C-668228872C55}
8 | Exe
9 | ToolBox.Framework.Test
10 | ToolBox.Framework.Test
11 | v4.6.1
12 | 512
13 | true
14 | true
15 |
16 |
17 | AnyCPU
18 | true
19 | full
20 | false
21 | bin\Debug\
22 | DEBUG;TRACE
23 | prompt
24 | 4
25 |
26 |
27 | AnyCPU
28 | pdbonly
29 | true
30 | bin\Release\
31 | TRACE
32 | prompt
33 | 4
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 | ..\packages\WebSocketSharp-NonPreRelease.1.0.0\lib\net35\websocket-sharp.dll
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 | {e337219d-c541-4d44-8c18-4076120355c6}
59 | ToolBox.Files
60 |
61 |
62 | {1581cfa6-8e1a-4a24-919a-08e04a8256f3}
63 | ToolBox.Log
64 |
65 |
66 | {417d3f4f-01c4-4467-a6a6-7ddf687166a6}
67 | ToolBox.Security
68 |
69 |
70 | {c2ff3905-8bb9-429b-9733-24d464b57024}
71 | ToolBox.Socket
72 |
73 |
74 |
75 |
76 |
77 |
78 |
--------------------------------------------------------------------------------
/ToolBox.Security/SymmetricHelper.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Security.Cryptography;
5 | using System.Text;
6 |
7 | namespace ToolBox.Security
8 | {
9 | ///
10 | /// 对称加密工具类
11 | ///
12 | public class SymmetricEncrypt
13 | {
14 | private SymmetricAlgorithm mobjCryptoService;
15 | private string Key;
16 | ///
17 | /// 对称加密类的构造函数
18 | ///
19 | public SymmetricEncrypt()
20 | {
21 | mobjCryptoService = new RijndaelManaged();
22 | Key = "Guz(%&hj7x89H$yuBI0456FtmaT5&fvHUFCy76*h%(HilJ$lhj!y6&(*jkP87jH7";
23 | }
24 |
25 | ///
26 | /// 带参数对称加密类的构造函数
27 | ///
28 | ///
29 | public SymmetricEncrypt(string key) {
30 |
31 | mobjCryptoService = new RijndaelManaged();
32 | this.Key = key;
33 | }
34 |
35 |
36 | ///
37 | /// 获得密钥
38 | ///
39 | /// 密钥
40 | private byte[] GetLegalKey()
41 | {
42 | string sTemp = Key;
43 | mobjCryptoService.GenerateKey();
44 | byte[] bytTemp = mobjCryptoService.Key;
45 | int KeyLength = bytTemp.Length;
46 | if (sTemp.Length > KeyLength)
47 | sTemp = sTemp.Substring(0, KeyLength);
48 | else if (sTemp.Length < KeyLength)
49 | sTemp = sTemp.PadRight(KeyLength, ' ');
50 | return ASCIIEncoding.ASCII.GetBytes(sTemp);
51 | }
52 | ///
53 | /// 获得初始向量IV
54 | ///
55 | /// 初试向量IV
56 | private byte[] GetLegalIV()
57 | {
58 | string sTemp = "E4ghj*Ghg7!rNIfb&95GUY86GfghUb#er57HBh(u%g6HJ($jhWk7&!hg4ui%$hjk";
59 | mobjCryptoService.GenerateIV();
60 | byte[] bytTemp = mobjCryptoService.IV;
61 | int IVLength = bytTemp.Length;
62 | if (sTemp.Length > IVLength)
63 | sTemp = sTemp.Substring(0, IVLength);
64 | else if (sTemp.Length < IVLength)
65 | sTemp = sTemp.PadRight(IVLength, ' ');
66 | return ASCIIEncoding.ASCII.GetBytes(sTemp);
67 | }
68 | ///
69 | /// 加密方法
70 | ///
71 | /// 待加密的串
72 | /// 经过加密的串
73 | public string Encrypto(string Source)
74 | {
75 | byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source);
76 | MemoryStream ms = new MemoryStream();
77 | mobjCryptoService.Key = GetLegalKey();
78 | mobjCryptoService.IV = GetLegalIV();
79 | ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();
80 | CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
81 | cs.Write(bytIn, 0, bytIn.Length);
82 | cs.FlushFinalBlock();
83 | ms.Close();
84 | byte[] bytOut = ms.ToArray();
85 | return Convert.ToBase64String(bytOut);
86 | }
87 | ///
88 | /// 解密方法
89 | ///
90 | /// 待解密的串
91 | /// 经过解密的串
92 | public string Decrypto(string Source)
93 | {
94 | byte[] bytIn = Convert.FromBase64String(Source);
95 | MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
96 | mobjCryptoService.Key = GetLegalKey();
97 | mobjCryptoService.IV = GetLegalIV();
98 | ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();
99 | CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
100 | StreamReader sr = new StreamReader(cs);
101 | return sr.ReadToEnd();
102 | }
103 |
104 |
105 |
106 | }
107 | }
108 |
--------------------------------------------------------------------------------
/ToolBox.Socket/UdpServer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 | using System.Net.Sockets;
5 | using System.Net;
6 | using System.Threading;
7 |
8 | namespace ToolBox.Socket
9 | {
10 | ///
11 | /// 消息参数
12 | ///
13 | public class MsgArgs : EventArgs {
14 |
15 | public string message { get; set; }
16 |
17 | public MsgArgs(string msg) {
18 |
19 | message = msg;
20 | }
21 |
22 | }
23 |
24 |
25 |
26 |
27 | ///
28 | /// udp服务器
29 | ///
30 | public class UdpServer
31 | {
32 | ///
33 | /// 套接字
34 | ///
35 | public System.Net.Sockets.Socket server;
36 |
37 | ///
38 | /// 错误输出
39 | ///
40 | public event EventHandler OnError;
41 |
42 | ///
43 | /// 接收信息输出
44 | ///
45 | public event EventHandler OnReciveMsg;
46 |
47 | ///
48 | /// 成功输出
49 | ///
50 | public event EventHandler OnSuccess;
51 |
52 |
53 | private string ip { get; set; }
54 |
55 | private int port { get; set; }
56 |
57 | ///
58 | /// 构造函数
59 | ///
60 | ///
61 | ///
62 | public UdpServer(string ip,int port) {
63 |
64 | this.ip=ip;
65 | this.port = port;
66 |
67 | }
68 |
69 |
70 | ///
71 | /// 开始udp连接
72 | ///
73 | ///
74 | ///
75 | public void Connect() {
76 |
77 | try
78 | {
79 | server = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
80 | server.Bind(new IPEndPoint(IPAddress.Parse(ip), port));//绑定端口号和IP
81 | Thread t2 = new Thread(ReciveMsg);
82 | t2.Start();
83 |
84 | OnSuccess?.Invoke(this, new MsgArgs("成功启动"));
85 | }
86 | catch (Exception ex)
87 | {
88 | OnError?.Invoke(this, new MsgArgs(ex.ToString()));
89 | }
90 |
91 |
92 | }
93 |
94 |
95 |
96 | ///
97 | /// 向特定ip的主机的端口发送数据报
98 | ///
99 | ///
100 | ///
101 | ///
102 | public void sendMsg(string msg, string ip, int port)
103 | {
104 |
105 | try
106 | {
107 | EndPoint point = new IPEndPoint(IPAddress.Parse(ip), port);
108 |
109 | server.SendTo(Encoding.UTF8.GetBytes(msg), point);
110 | }
111 | catch (Exception ex)
112 | {
113 |
114 | OnError?.Invoke(this, new MsgArgs(ex.ToString()));
115 | }
116 |
117 |
118 | }
119 |
120 |
121 | ///
122 | /// 接收到的数据
123 | ///
124 | public void ReciveMsg() {
125 |
126 |
127 | try
128 | {
129 | while (true)
130 | {
131 | EndPoint point = new IPEndPoint(IPAddress.Any, 0);//用来保存发送方的ip和端口号
132 |
133 | byte[] buffer = new byte[1024];
134 | int length = server.ReceiveFrom(buffer, ref point);//接收数据报
135 | string message = Encoding.UTF8.GetString(buffer, 0, length);
136 |
137 | // Console.WriteLine(point.ToString() + message);
138 |
139 | OnReciveMsg?.Invoke(this, new MsgArgs(point.ToString()+","+ message));
140 |
141 | }
142 | }
143 | catch (Exception ex)
144 | {
145 |
146 | OnError?.Invoke(this, new MsgArgs(ex.ToString()));
147 | }
148 |
149 |
150 | }
151 |
152 |
153 |
154 |
155 | }
156 |
157 |
158 |
159 | }
160 |
--------------------------------------------------------------------------------
/ToolBox.Socket/SocketTools.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Net.Sockets;
5 | using System.Text;
6 |
7 | namespace ToolBox.Socket
8 | {
9 |
10 | ///
11 | /// 套接字工具类
12 | ///
13 | public static class SocketTools
14 | {
15 |
16 | ///
17 | /// 得到二进制字节,已经添加了包头
18 | ///
19 | ///
20 | ///
21 | public static byte[] GetBytes(this string data)
22 | {
23 |
24 | byte[] dataBytes = Encoding.UTF8.GetBytes(data);
25 | int dataLength = dataBytes.Length;
26 | byte[] lengthBytes = BitConverter.GetBytes(dataLength);
27 | byte[] newBytes = lengthBytes.Concat(dataBytes).ToArray();
28 | return newBytes;
29 |
30 | }
31 |
32 |
33 | ///
34 | /// Get the binary byte, the header has been added
35 | ///
36 | ///
37 | ///
38 | ///
39 | public static byte[] GetBytes(this string data, bool IsEncryption = true) {
40 |
41 | if (IsEncryption)
42 | {
43 | return GetBytes(data.Encrypt());
44 | }
45 | else {
46 |
47 | return GetBytes(data);
48 | }
49 |
50 | }
51 |
52 |
53 | ///
54 | /// 字符串解密(Stringdecryption)
55 | ///
56 | ///
57 | ///
58 | public static string StringDecrypt(this string str, bool IsEncryption) {
59 |
60 | if (IsEncryption)
61 | {
62 | return str.Decrypt();
63 | }
64 | else {
65 |
66 | return str;
67 | }
68 |
69 | }
70 |
71 |
72 |
73 | ///
74 | /// 发送信息
75 | ///
76 | ///
77 | ///
78 | ///
79 | public static void SendMsg(System.Net.Sockets.Socket socket,string data,bool IsEncryption=true) {
80 |
81 | if (IsEncryption)
82 | {
83 | SendMsg(socket, data.Encrypt());
84 | }
85 | else {
86 |
87 | SendMsg(socket, data);
88 | }
89 |
90 |
91 | }
92 |
93 |
94 |
95 | ///
96 | /// 发送信息
97 | ///
98 | /// 套接字
99 | /// 要发送的信息
100 | private static void SendMsg(System.Net.Sockets.Socket socket , string data)
101 | {
102 |
103 | try
104 | {
105 | byte[] newBytes = GetBytes(data);
106 |
107 | if (socket.Connected)
108 | {
109 | socket.Send(newBytes);
110 | }
111 | else
112 | {
113 | Console.WriteLine("没有跟服务器连接~");
114 | }
115 | }
116 | catch (Exception ex)
117 | {
118 |
119 | Console.WriteLine("发送信息时的报错:" + ex);
120 | }
121 |
122 |
123 | }
124 |
125 |
126 | ///
127 | /// 是否连接了
128 | ///
129 | ///
130 | ///
131 | public static bool IsSocketConnected(this System.Net.Sockets.Socket socket)
132 | {
133 |
134 | bool part1 = socket.Poll(1000, SelectMode.SelectRead);
135 | bool part2 = (socket.Available == 0);
136 |
137 | if (part1 && part2)
138 | {
139 | return false;
140 | }
141 | else
142 | {
143 | return true;
144 | }
145 |
146 | }
147 |
148 |
149 | ///
150 | /// 得到时间便签
151 | ///
152 | ///
153 | public static long GetTimeStamp()
154 | {
155 |
156 | TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
157 | return Convert.ToInt64(ts.TotalSeconds);
158 | }
159 |
160 |
161 |
162 |
163 | }
164 | }
165 |
--------------------------------------------------------------------------------
/ToolBox.Common/Common.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Diagnostics;
4 | using System.Text;
5 |
6 | namespace ToolBox.Common
7 | {
8 | ///
9 | /// 常用公共类
10 | ///
11 | public class Common
12 | {
13 | #region Stopwatch计时器
14 | ///
15 | /// 计时器开始
16 | ///
17 | ///
18 | public static Stopwatch TimerStart()
19 | {
20 | Stopwatch watch = new Stopwatch();
21 | watch.Reset();
22 | watch.Start();
23 | return watch;
24 | }
25 | ///
26 | /// 计时器结束
27 | ///
28 | ///
29 | ///
30 | public static string TimerEnd(Stopwatch watch)
31 | {
32 | watch.Stop();
33 | double costtime = watch.ElapsedMilliseconds;
34 | return costtime.ToString();
35 | }
36 | #endregion
37 |
38 | #region 删除数组中的重复项
39 | ///
40 | /// 删除数组中的重复项
41 | ///
42 | ///
43 | ///
44 | public static string[] RemoveDup(string[] values)
45 | {
46 | List list = new List();
47 | for (int i = 0; i < values.Length; i++)//遍历数组成员
48 | {
49 | if (!list.Contains(values[i]))
50 | {
51 | list.Add(values[i]);
52 | };
53 | }
54 | return list.ToArray();
55 | }
56 | #endregion
57 |
58 | #region 自动生成编号
59 | ///
60 | /// 表示全局唯一标识符 (GUID)。
61 | ///
62 | ///
63 | public static string GuId()
64 | {
65 | return Guid.NewGuid().ToString();
66 | }
67 | ///
68 | /// 自动生成编号 201008251145409865
69 | ///
70 | ///
71 | public static string CreateNo()
72 | {
73 | Random random = new Random();
74 | string strRandom = random.Next(1000, 10000).ToString(); //生成编号
75 | string code = DateTime.Now.ToString("yyyyMMddHHmmss") + strRandom;//形如
76 | return code;
77 | }
78 | #endregion
79 |
80 | #region "全球唯一码GUID"
81 | ///
82 | /// 获取一个全球唯一码GUID字符串
83 | ///
84 | public static string GetGuid
85 | {
86 | get
87 | {
88 | return Guid.NewGuid().ToString().ToLower();
89 | }
90 | }
91 | #endregion
92 |
93 | #region 生成0-9随机数
94 | ///
95 | /// 生成0-9随机数
96 | ///
97 | /// 生成长度
98 | ///
99 | public static string RndNum(int codeNum)
100 | {
101 | StringBuilder sb = new StringBuilder(codeNum);
102 | Random rand = new Random();
103 | for (int i = 1; i < codeNum + 1; i++)
104 | {
105 | int t = rand.Next(9);
106 | sb.AppendFormat("{0}", t);
107 | }
108 | return sb.ToString();
109 |
110 | }
111 | #endregion
112 |
113 | #region 删除最后一个字符之后的字符
114 | ///
115 | /// 删除最后结尾的一个逗号
116 | ///
117 | public static string DelLastComma(string str)
118 | {
119 | return str.Substring(0, str.LastIndexOf(","));
120 | }
121 | ///
122 | /// 删除最后结尾的指定字符后的字符
123 | ///
124 | public static string DelLastChar(string str, string strchar)
125 | {
126 | return str.Substring(0, str.LastIndexOf(strchar));
127 | }
128 | ///
129 | /// 删除最后结尾的长度
130 | ///
131 | ///
132 | ///
133 | ///
134 | public static string DelLastLength(string str, int Length)
135 | {
136 | if (string.IsNullOrEmpty(str))
137 | return "";
138 | str = str.Substring(0, str.Length - Length);
139 | return str;
140 | }
141 | #endregion
142 | }
143 | }
144 |
--------------------------------------------------------------------------------
/ToolBox.Socket/SocketDesHelper.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Security.Cryptography;
5 | using System.Text;
6 |
7 | namespace ToolBox.Socket
8 | {
9 | ///
10 | /// socket des helper
11 | ///
12 | public static class SocketDesHelper
13 | {
14 |
15 | ///
16 | /// Set Encryption key
17 | ///
18 | private static string encryptKey { get; set; } = "socket123";
19 |
20 |
21 | ///
22 | /// the key is length must >=8
23 | ///
24 | ///
25 | ///
26 | public static bool SetEncryptKey(string key) {
27 |
28 | if (key.Length >= 8)
29 | {
30 | string result= "test".Encrypt();
31 |
32 | if (result.Equals("")) {
33 | return false;
34 | }
35 |
36 | if (result.Decrypt().Equals("")) {
37 |
38 | return false;
39 | }
40 |
41 |
42 | return true;
43 | }
44 | else {
45 |
46 | return false;
47 | }
48 |
49 |
50 | }
51 |
52 |
53 | #region ========加密========
54 |
55 | ///
56 | /// DES【加密】字符串,使用缺省密钥。
57 | ///
58 | /// 字符串
59 | /// 返回string
60 | public static string Encrypt(this string text)
61 | {
62 | return Encrypt(text, encryptKey);
63 | }
64 |
65 | ///
66 | /// DES【加密】字符串,使用给定密钥。
67 | ///
68 | /// 字符串
69 | /// 密钥字符串
70 | /// 返回string
71 | public static string Encrypt(string text, string key)
72 | {
73 | try
74 | {
75 | byte[] rgbKey = Encoding.UTF8.GetBytes(key.Substring(0, 8));
76 | byte[] rgbIV = rgbKey;// Encoding.UTF8.GetBytes(key.Substring(0, 8));
77 | byte[] inputByteArray = Encoding.UTF8.GetBytes(text);
78 | DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
79 | MemoryStream mStream = new MemoryStream();
80 | CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
81 | cStream.Write(inputByteArray, 0, inputByteArray.Length);
82 | cStream.FlushFinalBlock();
83 | return Convert.ToBase64String(mStream.ToArray());
84 | }
85 | catch
86 | {
87 | return "";
88 | }
89 |
90 | }
91 |
92 | #endregion
93 |
94 | #region ========解密========
95 |
96 |
97 | ///
98 | /// DES【解密】字符串,使用缺省密钥。
99 | ///
100 | /// 字符串
101 | /// 返回string
102 | public static string Decrypt(this string text)
103 | {
104 | return Decrypt(text, encryptKey);
105 |
106 | }
107 |
108 | ///
109 | /// DES【解密】字符串,使用给定密钥。
110 | ///
111 | /// 字符串
112 | /// 密钥字符串
113 | /// 返回string
114 | public static string Decrypt(string text, string key)
115 | {
116 | try
117 | {
118 | byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));
119 | byte[] keyIV = keyBytes;
120 | byte[] inputByteArray = Convert.FromBase64String(text);
121 | DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
122 | MemoryStream mStream = new MemoryStream();
123 | CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write);
124 | cStream.Write(inputByteArray, 0, inputByteArray.Length);
125 | cStream.FlushFinalBlock();
126 | return Encoding.UTF8.GetString(mStream.ToArray());
127 | }
128 | catch (Exception ex)
129 | {
130 | return "";
131 |
132 | }
133 |
134 |
135 | }
136 |
137 | #endregion
138 |
139 |
140 | }
141 | }
142 |
--------------------------------------------------------------------------------
/ToolBox.Common/ConverHelper.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace ToolBox.Common
6 | {
7 |
8 | ///
9 | /// 处理数据类型转换,数制转换、编码转换相关的类
10 | ///
11 | public class ConverHelper
12 | {
13 |
14 | #region 补足位数
15 | ///
16 | /// 指定字符串的固定长度,如果字符串小于固定长度,
17 | /// 则在字符串的前面补足零,可设置的固定长度最大为9位
18 | ///
19 | /// 原始字符串
20 | /// 字符串的固定长度
21 | public static string RepairZero(string text, int limitedLength)
22 | {
23 | //补足0的字符串
24 | string temp = "";
25 |
26 | //补足0
27 | for (int i = 0; i < limitedLength - text.Length; i++)
28 | {
29 | temp += "0";
30 | }
31 |
32 | //连接text
33 | temp += text;
34 |
35 | //返回补足0的字符串
36 | return temp;
37 | }
38 | #endregion
39 |
40 | #region 各进制数间转换
41 | ///
42 | /// 实现各进制数间的转换。ConvertBase("15",10,16)表示将十进制数15转换为16进制的数。
43 | ///
44 | /// 要转换的值,即原值
45 | /// 原值的进制,只能是2,8,10,16四个值。
46 | /// 要转换到的目标进制,只能是2,8,10,16四个值。
47 | public static string ConvertBase(string value, int from, int to)
48 | {
49 | try
50 | {
51 | int intValue = Convert.ToInt32(value, from); //先转成10进制
52 | string result = Convert.ToString(intValue, to); //再转成目标进制
53 | if (to == 2)
54 | {
55 | int resultLength = result.Length; //获取二进制的长度
56 | switch (resultLength)
57 | {
58 | case 7:
59 | result = "0" + result;
60 | break;
61 | case 6:
62 | result = "00" + result;
63 | break;
64 | case 5:
65 | result = "000" + result;
66 | break;
67 | case 4:
68 | result = "0000" + result;
69 | break;
70 | case 3:
71 | result = "00000" + result;
72 | break;
73 | }
74 | }
75 | return result;
76 | }
77 | catch
78 | {
79 |
80 | //LogHelper.WriteTraceLog(TraceLogLevel.Error, ex.Message);
81 | return "0";
82 | }
83 | }
84 | #endregion
85 |
86 | #region 使用指定字符集将string转换成byte[]
87 | ///
88 | /// 使用指定字符集将string转换成byte[]
89 | ///
90 | /// 要转换的字符串
91 | /// 字符编码
92 | public static byte[] StringToBytes(string text, Encoding encoding)
93 | {
94 | return encoding.GetBytes(text);
95 | }
96 | #endregion
97 |
98 | #region 使用指定字符集将byte[]转换成string
99 | ///
100 | /// 使用指定字符集将byte[]转换成string
101 | ///
102 | /// 要转换的字节数组
103 | /// 字符编码
104 | public static string BytesToString(byte[] bytes, Encoding encoding)
105 | {
106 | return encoding.GetString(bytes);
107 | }
108 | #endregion
109 |
110 | #region 将byte[]转换成int
111 | ///
112 | /// 将byte[]转换成int
113 | ///
114 | /// 需要转换成整数的byte数组
115 | public static int BytesToInt32(byte[] data)
116 | {
117 | //如果传入的字节数组长度小于4,则返回0
118 | if (data.Length < 4)
119 | {
120 | return 0;
121 | }
122 |
123 | //定义要返回的整数
124 | int num = 0;
125 |
126 | //如果传入的字节数组长度大于4,需要进行处理
127 | if (data.Length >= 4)
128 | {
129 | //创建一个临时缓冲区
130 | byte[] tempBuffer = new byte[4];
131 |
132 | //将传入的字节数组的前4个字节复制到临时缓冲区
133 | Buffer.BlockCopy(data, 0, tempBuffer, 0, 4);
134 |
135 | //将临时缓冲区的值转换成整数,并赋给num
136 | num = BitConverter.ToInt32(tempBuffer, 0);
137 | }
138 |
139 | //返回整数
140 | return num;
141 | }
142 | #endregion
143 |
144 | }
145 | }
146 |
--------------------------------------------------------------------------------
/ToolBox.String/RMBHelper.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace ToolBox.String
6 | {
7 | ///
8 | /// 转换人民币大小金额
9 | ///
10 | public static class RMBHelper
11 | {
12 | ///
13 | /// 转换人民币大小金额
14 | ///
15 | /// 金额
16 | /// 返回大写形式
17 | public static string CmycurD(decimal num)
18 | {
19 | string str1 = "零壹贰叁肆伍陆柒捌玖"; //0-9所对应的汉字
20 | string str2 = "万仟佰拾亿仟佰拾万仟佰拾元角分"; //数字位所对应的汉字
21 | string str3 = ""; //从原num值中取出的值
22 | string str4 = ""; //数字的字符串形式
23 | string str5 = ""; //人民币大写金额形式
24 | int i; //循环变量
25 | int j; //num的值乘以100的字符串长度
26 | string ch1 = ""; //数字的汉语读法
27 | string ch2 = ""; //数字位的汉字读法
28 | int nzero = 0; //用来计算连续的零值是几个
29 | int temp; //从原num值中取出的值
30 |
31 | num = Math.Round(Math.Abs(num), 2); //将num取绝对值并四舍五入取2位小数
32 | str4 = ((long)(num * 100)).ToString(); //将num乘100并转换成字符串形式
33 | j = str4.Length; //找出最高位
34 | if (j > 15) { return "溢出"; }
35 | str2 = str2.Substring(15 - j); //取出对应位数的str2的值。如:200.55,j为5所以str2=佰拾元角分
36 |
37 | //循环取出每一位需要转换的值
38 | for (i = 0; i < j; i++)
39 | {
40 | str3 = str4.Substring(i, 1); //取出需转换的某一位的值
41 | temp = Convert.ToInt32(str3); //转换为数字
42 | if (i != (j - 3) && i != (j - 7) && i != (j - 11) && i != (j - 15))
43 | {
44 | //当所取位数不为元、万、亿、万亿上的数字时
45 | if (str3 == "0")
46 | {
47 | ch1 = "";
48 | ch2 = "";
49 | nzero = nzero + 1;
50 | }
51 | else
52 | {
53 | if (str3 != "0" && nzero != 0)
54 | {
55 | ch1 = "零" + str1.Substring(temp * 1, 1);
56 | ch2 = str2.Substring(i, 1);
57 | nzero = 0;
58 | }
59 | else
60 | {
61 | ch1 = str1.Substring(temp * 1, 1);
62 | ch2 = str2.Substring(i, 1);
63 | nzero = 0;
64 | }
65 | }
66 | }
67 | else
68 | {
69 | //该位是万亿,亿,万,元位等关键位
70 | if (str3 != "0" && nzero != 0)
71 | {
72 | ch1 = "零" + str1.Substring(temp * 1, 1);
73 | ch2 = str2.Substring(i, 1);
74 | nzero = 0;
75 | }
76 | else
77 | {
78 | if (str3 != "0" && nzero == 0)
79 | {
80 | ch1 = str1.Substring(temp * 1, 1);
81 | ch2 = str2.Substring(i, 1);
82 | nzero = 0;
83 | }
84 | else
85 | {
86 | if (str3 == "0" && nzero >= 3)
87 | {
88 | ch1 = "";
89 | ch2 = "";
90 | nzero = nzero + 1;
91 | }
92 | else
93 | {
94 | if (j >= 11)
95 | {
96 | ch1 = "";
97 | nzero = nzero + 1;
98 | }
99 | else
100 | {
101 | ch1 = "";
102 | ch2 = str2.Substring(i, 1);
103 | nzero = nzero + 1;
104 | }
105 | }
106 | }
107 | }
108 | }
109 | if (i == (j - 11) || i == (j - 3))
110 | {
111 | //如果该位是亿位或元位,则必须写上
112 | ch2 = str2.Substring(i, 1);
113 | }
114 | str5 = str5 + ch1 + ch2;
115 |
116 | if (i == j - 1 && str3 == "0")
117 | {
118 | //最后一位(分)为0时,加上“整”
119 | str5 = str5 + '整';
120 | }
121 | }
122 | if (num == 0)
123 | {
124 | str5 = "零元整";
125 | }
126 | return str5;
127 | }
128 |
129 | /**/
130 | ///
131 | /// 一个重载,将字符串先转换成数字在调用CmycurD(decimal num)
132 | ///
133 | /// 用户输入的金额,字符串形式未转成decimal
134 | ///
135 | public static string CmycurD(string numstr)
136 | {
137 | try
138 | {
139 | decimal num = Convert.ToDecimal(numstr);
140 | return CmycurD(num);
141 | }
142 | catch
143 | {
144 | return "非数字形式!";
145 | }
146 | }
147 | }
148 | }
149 |
--------------------------------------------------------------------------------
/ToolBox.Reflection/ReflectionUtil.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections;
3 | using System.Collections.Concurrent;
4 | using System.Collections.Generic;
5 | using System.Linq.Expressions;
6 | using System.Reflection;
7 | using System.Text;
8 |
9 | namespace ToolBox.Reflection
10 | {
11 | ///
12 | /// 反射工具类
13 | ///
14 | public static class ReflectionUtil
15 | {
16 | private static readonly ConcurrentDictionary> _cachedProperties = new ConcurrentDictionary>();
17 |
18 | public static string GetPropertyNameFromExpression(Expression> expression)
19 | {
20 | string propertyPath = null;
21 | if (expression.Body is UnaryExpression)
22 | {
23 | var unaryExpression = (UnaryExpression)expression.Body;
24 | if (unaryExpression.NodeType == ExpressionType.Convert)
25 | propertyPath = unaryExpression.Operand.ToString();
26 | }
27 |
28 | if (propertyPath == null)
29 | propertyPath = expression.Body.ToString();
30 |
31 | propertyPath = propertyPath.Replace(expression.Parameters[0] + ".", string.Empty);
32 |
33 | return propertyPath;
34 | }
35 |
36 | public static List GetPropertyNamesFromExpressions(Expression>[] expressions)
37 | {
38 | var propertyNames = new List();
39 | foreach (var expression in expressions)
40 | {
41 | var propertyName = GetPropertyNameFromExpression(expression);
42 | propertyNames.Add(propertyName);
43 | }
44 | return propertyNames;
45 | }
46 |
47 | ///
48 | /// 获取对象属性值
49 | ///
50 | /// 将返回其属性值的对象。
51 | /// 对象的PropertyInfo信息(发现属性 (Property) 的属性 (Attribute) 并提供对属性 (Property) 元数据的访问)
52 | /// object
53 | public static object GetPropertyValue(object item, PropertyInfo property)
54 | {
55 | var value = property.GetValue(item, null);
56 |
57 | return value;
58 | }
59 |
60 | ///
61 | /// 获取对象属性值
62 | ///
63 | /// 将返回其属性值的对象。
64 | /// 属性名
65 | /// object
66 | public static object GetPropertyValue(object item, string propertyName)
67 | {
68 | PropertyInfo property;
69 | foreach (var part in propertyName.Split('.'))
70 | {
71 | if (item == null)
72 | return null;
73 |
74 | var type = item.GetType();
75 |
76 | property = type.GetProperty(part);
77 | if (property == null)
78 | return null;
79 |
80 | item = GetPropertyValue(item, property);
81 | }
82 | return item;
83 | }
84 |
85 | public static object GetPropertyValueDynamic(object item, string name)
86 | {
87 | var dictionary = (IDictionary)item;
88 |
89 | return dictionary[name];
90 | }
91 |
92 | public static Dictionary GetProperties(Type type)
93 | {
94 | var properties = _cachedProperties.GetOrAdd(type, BuildPropertyDictionary);
95 |
96 | return properties;
97 | }
98 |
99 | private static Dictionary BuildPropertyDictionary(Type type)
100 | {
101 | var result = new Dictionary();
102 |
103 | var properties = type.GetProperties();
104 | foreach (var property in properties)
105 | {
106 | result.Add(property.Name.ToLower(), property);
107 | }
108 | return result;
109 | }
110 |
111 | public static bool IsList(object item)
112 | {
113 | if (item is ICollection)
114 | return true;
115 |
116 | return false;
117 | }
118 |
119 | public static bool IsNullable(PropertyInfo property)
120 | {
121 | if (property.PropertyType.IsGenericType &&
122 | property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
123 | return true;
124 |
125 | return false;
126 | }
127 |
128 | ///
129 | /// Includes a work around for getting the actual type of a Nullable type.
130 | ///
131 | public static Type GetPropertyType(PropertyInfo property)
132 | {
133 | if (IsNullable(property))
134 | return property.PropertyType.GetGenericArguments()[0];
135 |
136 | return property.PropertyType;
137 | }
138 |
139 | public static object GetDefault(Type type)
140 | {
141 | if (type.IsValueType)
142 | return Activator.CreateInstance(type);
143 | return null;
144 | }
145 |
146 | public static bool IsBasicClrType(Type type)
147 | {
148 | if (type.IsEnum
149 | || type.IsPrimitive
150 | || type.IsValueType
151 | || type == typeof(string)
152 | || type == typeof(DateTime))
153 | return true;
154 |
155 | return false;
156 | }
157 | }
158 | }
159 |
--------------------------------------------------------------------------------
/ToolBox.Http/CookieHelper.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Text.RegularExpressions;
6 |
7 | namespace ToolBox.Http
8 | {
9 |
10 | ///
11 | /// cookite工具量
12 | ///
13 | public class CookieHelper
14 | {
15 |
16 | ///
17 | /// 获取简化的cookite
18 | ///
19 | ///
20 | ///
21 | public static string GetSmallCookie(IEnumerable cookies) {
22 |
23 | string[] strs;
24 | StringBuilder sb = new StringBuilder(128);
25 | HashSet hash = new HashSet();
26 |
27 | foreach (var item in cookies)
28 | {
29 | strs = item.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
30 |
31 | foreach (var sub in strs)
32 | {
33 | if (!Regex.IsMatch(sub, "expires=|max-ag=|path=|domain=|HttpOnly", RegexOptions.IgnoreCase)) {
34 |
35 | hash.Add(sub.Trim());
36 | }
37 | }
38 |
39 |
40 | }
41 |
42 | return string.Join(";", hash);
43 |
44 | }
45 |
46 |
47 | ///
48 | /// 合并更新cookite
49 | ///
50 | /// oldCookie
51 | /// newCookie
52 | ///
53 | public static string UpdateMergeCookie(string oldCookie, string newCookie) {
54 |
55 | if (string.IsNullOrWhiteSpace(oldCookie)) return newCookie;
56 | if (string.IsNullOrWhiteSpace(newCookie)) return oldCookie;
57 |
58 | List nCookies = newCookie.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList();
59 | string[] oCookies = newCookie.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
60 | HashSet hash = new HashSet();
61 |
62 | string[] temps;
63 | foreach (var item in oCookies)
64 | {
65 | temps = item.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
66 | if (temps.Length < 2) continue;
67 | hash.Add(temps[0]);
68 | }
69 | foreach (var item in oCookies)
70 | {
71 | temps = item.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
72 | if (temps.Length < 2) continue;
73 |
74 | if (hash.Add(temps[0])) nCookies.Add(item);
75 | }
76 | return string.Join(";", nCookies);
77 |
78 |
79 |
80 | }
81 |
82 |
83 | ///
84 | /// 查询是否有cookname值
85 | ///
86 | ///
87 | ///
88 | ///
89 | public static bool HasCookie(string cookie, string cook_name)
90 | {
91 | if (string.IsNullOrWhiteSpace(cookie)) return false;
92 | string[] list_New = cookie.ToString().Split(';');
93 |
94 | foreach (string item in list_New)
95 | {
96 | //排除重复项
97 | string[] tmp = item.Trim().Split('=');
98 |
99 | if (tmp[0] == cook_name)
100 | {
101 | return true;
102 | }
103 | }
104 |
105 | return false;
106 | }
107 |
108 |
109 | ///
110 | /// 删除指定cookite
111 | ///
112 | ///
113 | ///
114 | ///
115 | public static string CookieDelete(string cookie, string cook_name)
116 | {
117 | if (string.IsNullOrWhiteSpace(cookie)) return "";
118 |
119 | string[] list_New = cookie.ToString().Split(';');
120 |
121 | string new_cookie = cookie;
122 |
123 | foreach (string item in list_New)
124 | {
125 | //排除重复项
126 | string[] tmp = item.Trim().Split('=');
127 | if (tmp.Length == 2)
128 | {
129 | if (tmp[0].Equals(cook_name))
130 | {
131 | new_cookie = new_cookie.Replace(item + ';', "");
132 | }
133 |
134 | }
135 |
136 |
137 | }
138 |
139 | return new_cookie;
140 |
141 | }
142 |
143 |
144 | ///
145 | /// 获取指定cookei值
146 | ///
147 | ///
148 | ///
149 | public static string GetCookValue(string cookie, string cook_name)
150 | {
151 | if (string.IsNullOrWhiteSpace(cookie)) return "";
152 |
153 | string[] list_New = cookie.ToString().Split(';');
154 |
155 | string new_cookie = cookie;
156 |
157 | foreach (string item in list_New)
158 | {
159 | //排除重复项
160 | string[] tmp = item.Trim().Split('=');
161 | if (tmp.Length == 2)
162 | {
163 | if (tmp[0].Equals(cook_name))
164 | {
165 | return tmp[1];
166 | }
167 |
168 | }
169 |
170 |
171 | }
172 |
173 | return "";
174 |
175 | }
176 |
177 |
178 | }
179 | }
180 |
--------------------------------------------------------------------------------
/ToolBox.Log/LogUtil.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Text;
5 | using System.Threading;
6 | using System.Threading.Tasks;
7 |
8 | namespace ToolBox.Log
9 | {
10 | ///
11 | /// 一个超级简单的日志帮助类
12 | ///
13 | public static class LogUtil
14 | {
15 |
16 | private static string LogPath { get; set; }
17 | private static List LogQueue { get; set; }
18 |
19 | ///
20 | /// 设置日志文件夹路径,true是成功,flase是失败(默认软件运行目录下log文件夹中写入)
21 | ///
22 | ///
23 | ///
24 | public static bool SetDirectoryPath(string directoryPath)
25 | {
26 | try
27 | {
28 | if (Directory.Exists(directoryPath) && !File.Exists(directoryPath))
29 | {
30 |
31 | Console.WriteLine(Path.GetFullPath(directoryPath));
32 |
33 | LogPath = Path.GetFullPath(directoryPath);
34 |
35 | return true;
36 |
37 | }
38 | else if (!File.Exists(directoryPath))
39 | {
40 |
41 | string path = Path.Combine(directoryPath);
42 |
43 | Directory.CreateDirectory(path);
44 |
45 | LogPath = path;
46 |
47 | return true;
48 | }
49 | else
50 | {
51 | return false;
52 | }
53 | }
54 | catch (Exception ex)
55 | {
56 | ex.Message.WriteErrorLog();
57 | return false;
58 | }
59 | }
60 |
61 | ///
62 | /// 不指定目录默认当前目录
63 | ///
64 | static LogUtil()
65 | {
66 | LogQueue = new List();
67 | LogPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"logs\");
68 | Start();
69 | }
70 |
71 | ///
72 | /// 循环检测队列
73 | ///
74 | private static void Start()
75 | {
76 | Task.Run(() =>
77 | {
78 | while (true)
79 | {
80 | if (LogQueue.Count > 0)
81 | {
82 | try
83 | {
84 | using (StreamWriter sw = new StreamWriter(LogQueue[0].Path, true, Encoding.UTF8))
85 | {
86 | sw.Write(LogQueue[0].Msg);
87 | LogQueue.Remove(LogQueue[0]);
88 | sw.Close();
89 | sw.Dispose();
90 | }
91 | }
92 | catch (Exception ex)
93 | {
94 | WriteMessage(LogType.Error, ex.Message + "----WriteMessage:" + LogQueue[0].Msg);
95 | LogQueue.Remove(LogQueue[0]);
96 | }
97 | Thread.Sleep(2);
98 | }
99 | else
100 | {
101 | Thread.Sleep(500);
102 | }
103 | }
104 | });
105 | }
106 |
107 | ///
108 | /// 输出调试信息
109 | ///
110 | ///
111 | public static void Debug(string message)
112 | {
113 | WriteMessage(LogType.Debug, message);
114 | }
115 |
116 | ///
117 | /// 输出普通信息
118 | ///
119 | ///
120 | public static void Info(string message)
121 | {
122 | WriteMessage(LogType.Info, message);
123 | }
124 |
125 | ///
126 | /// 输出错误信息
127 | ///
128 | ///
129 | public static void Error(string message)
130 | {
131 | WriteMessage(LogType.Error, message);
132 | }
133 |
134 | ///
135 | /// 扩展方法 输出调试信息
136 | ///
137 | ///
138 | public static void WriteDebugLog(this string message)
139 | {
140 | WriteMessage(LogType.Debug, message);
141 | }
142 |
143 | ///
144 | /// 扩展方法 输出普通信息
145 | ///
146 | ///
147 | public static void WriteInfoLog(this string message)
148 | {
149 | WriteMessage(LogType.Info, message);
150 | }
151 |
152 | ///
153 | /// 扩展方法 输出错误信息
154 | ///
155 | ///
156 | public static void WriteErrorLog(this string message)
157 | {
158 | WriteMessage(LogType.Error, message);
159 | }
160 |
161 | private static void WriteMessage(LogType logType, string message)
162 | {
163 | try
164 | {
165 | if (!Directory.Exists(LogPath))
166 | {
167 | Directory.CreateDirectory(LogPath);
168 | }
169 |
170 | var tLogPath = Path.Combine(LogPath, DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
171 |
172 |
173 | // Console.WriteLine(tLogPath);
174 |
175 | if (!File.Exists(tLogPath))
176 | {
177 | using (File.Create(tLogPath))
178 | {
179 |
180 | }
181 | }
182 |
183 | //添加队列
184 | LogQueue.Add(new QueueModel { Msg = $"{DateTime.Now}----【{logType.ToString()}】:{message}\r\n", Path = tLogPath });
185 | }
186 | catch (Exception ex)
187 | {
188 | WriteMessage(LogType.Error, ex.Message);
189 | }
190 | }
191 | }
192 | }
193 |
--------------------------------------------------------------------------------
/ToolBox.Http/HttpItem.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Net;
4 | using System.Net.Http;
5 | using System.Security.Cryptography.X509Certificates;
6 | using System.Text;
7 |
8 | namespace ToolBox.Http
9 | {
10 | ///
11 | /// http请求类
12 | ///
13 | public class HttpItem
14 | {
15 |
16 | ///
17 | /// 处理程序使用的自动解压缩方法 Gzip Deflate none=不使用压缩 默认为none
18 | ///
19 | public DecompressionMethods Decompression = DecompressionMethods.None;
20 |
21 | ///
22 | /// Authorization 认证 Convert.ToBase64String(Encoding.UTF8.GetBytes($"{Username}:{Password}"))
23 | ///
24 | public string Authorization { get; set; }
25 |
26 | ///
27 | /// 请求URL必须填写
28 | ///
29 | public string URL { get; set; }
30 |
31 | ///
32 | /// 请求方式
33 | ///
34 | public HttpMethod Method = HttpMethod.Get;
35 |
36 | ///
37 | /// 请求时间
38 | ///
39 | public int Timeout { get; set; } = 100000;
40 |
41 | ///
42 | /// 默认写入post数据超时时间
43 | ///
44 | public int ReadWriteTimeout { get; set; } = 30000;
45 |
46 | ///
47 | /// 设置Host的标头信息
48 | ///
49 | public string Host { get; set; }
50 |
51 | ///
52 | /// 获取或设置一个值,该值指示是否与 Internet 资源建立持久性连接默认为true。
53 | ///
54 | public Boolean KeepAlive { get; set; } = true;
55 |
56 | ///
57 | /// 请求标头值 默认为text/html, application/xhtml+xml, */*
58 | ///
59 | public string Accept { get; set; } = "*/*";
60 |
61 |
62 | ///
63 | /// 请求返回类型默认 text/html
64 | ///
65 | public string ContentType { get; set; } = "application/x-www-form-urlencoded";
66 |
67 |
68 | ///
69 | /// 客户端访问信息默认Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
70 | ///
71 | public string UserAgent { get; set; }= "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
72 |
73 |
74 | ///
75 | /// 返回数据编码默认为NUll 一般为utf-8,gbk,gb2312
76 | ///
77 | public Encoding Encoding { get; set; } = Encoding.UTF8;
78 |
79 |
80 | ///
81 | /// Post的数据类型,默认为string
82 | ///
83 | public PostDataType postDataType { get; set; } = PostDataType.String;
84 |
85 |
86 | ///
87 | /// Post请求时要发送的字符串Post数据
88 | ///
89 | public string Postdata { get; set; }
90 |
91 |
92 | ///
93 | /// Post请求时要发送的Byte类型的Post数据
94 | ///
95 | public byte[] PostdataByte { get; set; }
96 |
97 |
98 | ///
99 | /// Cookie对象集合
100 | ///
101 | private CookieContainer CookieContainer { get; set; }
102 |
103 | ///
104 | /// 请求时的Cookie
105 | ///
106 | public string Cookie { get; set; }
107 |
108 | ///
109 | /// 来源地址,上次访问地址
110 | ///
111 | public string Referer { get; set; }
112 |
113 | ///
114 | /// 证书绝对路径
115 | ///
116 | public string CerPath { get; set; }
117 |
118 | ///
119 | /// 设置代理对象,不想使用IE默认配置就设置为Null,而且不要设置ProxyIp
120 | ///
121 | public WebProxy WebProxy { get; set; }
122 |
123 | ///
124 | /// 是否设置为全文小写,默认为不转化
125 | ///
126 | public Boolean IsToLower { get; set; } = false;
127 |
128 | ///
129 | /// 支持跳转页面,查询结果将是跳转后的页面,默认是跳转
130 | ///
131 | public Boolean Allowautoredirect { get; set; } = true;
132 |
133 | ///
134 | /// 最大连接数
135 | ///
136 | public int Connectionlimit { get; set; } = 1024;
137 |
138 | ///
139 | /// 代理Proxy 服务器用户名
140 | ///
141 | public string ProxyUserName { get; set; }
142 |
143 |
144 | ///
145 | /// 代理 服务器密码
146 | ///
147 | public string ProxyPwd { get; set; }
148 |
149 | ///
150 | /// 代理 服务IP,如果要使用IE代理就设置为ieproxy
151 | ///
152 | public string ProxyIp { get; set; }
153 |
154 | ///
155 | /// 设置返回类型String和Byte
156 | ///
157 | public ResultType ResultType { get; set; }= ResultType.String;
158 |
159 | ///
160 | /// header对象
161 | ///
162 | public WebHeaderCollection Header { get; set; } = new WebHeaderCollection();
163 |
164 | ///
165 | /// 获取或设置用于请求的 HTTP 版本。返回结果:用于请求的 HTTP 版本。默认为 System.Net.HttpVersion.Version11。
166 | ///
167 | public Version ProtocolVersion { get; set; }
168 |
169 | ///
170 | /// 获取或设置一个 System.Boolean 值,该值确定是否使用 100-Continue 行为。如果 POST 请求需要 100-Continue 响应,则为 true;否则为 false。默认值为 true。
171 | ///
172 | public Boolean Expect100Continue { get; set; } = false;
173 |
174 |
175 | ///
176 | /// 设置509证书集合
177 | ///
178 | public X509CertificateCollection ClentCertificates { get; set; }
179 |
180 |
181 | ///
182 | /// 设置或获取Post参数编码,默认的为Default编码
183 | ///
184 | public Encoding PostEncoding { get; set; }
185 |
186 | ///
187 | /// Cookie返回类型,默认的是只返回字符串类型
188 | ///
189 | public ResultCookieType ResultCookieType { get; set; } = ResultCookieType.String;
190 |
191 | ///
192 | /// 获取或设置请求的身份验证信息。
193 | ///
194 | public ICredentials ICredentials { get; set; }= CredentialCache.DefaultCredentials;
195 |
196 |
197 | ///
198 | /// 设置请求将跟随的重定向的最大数目
199 | ///
200 | public int MaximumAutomaticRedirections { get; set; }
201 |
202 | ///
203 | /// 获取和设置IfModifiedSince,默认为当前日期和时间
204 | ///
205 | public DateTime? IfModifiedSince { get; set; } = null;
206 |
207 | ///
208 | /// 是否自动合并并更新Cookie 默认为true
209 | ///
210 | public bool AutoFixCookie { get; set; } = true;
211 |
212 |
213 | ///
214 | /// 是否自动处理无效Cookie 默认为true
215 | ///
216 | public bool AutoHanderCookie { get; set; } = true;
217 |
218 |
219 |
220 | }
221 | }
222 |
--------------------------------------------------------------------------------
/ToolBox.Object/ObjectHelper.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Reflection;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace ToolBox.Object
9 | {
10 | ///
11 | /// Object工具类
12 | ///
13 | public class ObjectHelper
14 | {
15 |
16 | ///
17 | /// 从指定对象创建指定类型的新对象
18 | ///
19 | /// 新对象类型
20 | /// 源对象
21 | /// 指定类型的新对象
22 | /// 不拷贝的属性集合
23 | /// 是否拷贝有Virtual访问修饰符的属性
24 | ///
25 | public static T CreateObject(object obj, string[] notFilds, bool isCopyVirtual) where T : class
26 | {
27 | Type type = typeof(T);
28 | object obj2 = Assembly.GetAssembly(type).CreateInstance(type.FullName);
29 | PropertyCopy(obj, obj2, notFilds, isCopyVirtual);
30 | return obj2 as T;
31 | }
32 |
33 | ///
34 | /// 拷贝并创建对象
35 | ///
36 | ///
37 | /// 不拷贝的属性集合
38 | ///
39 | public static T CreateObject(object obj, string[] notFilds) where T : class
40 | {
41 | return CreateObject(obj, notFilds, false);
42 | }
43 | ///
44 | /// 拷贝并创建对象
45 | ///
46 | ///
47 | /// 是否拷贝有Virtual访问修饰符的属性
48 | ///
49 | public static T CreateObject(object obj, bool isCopyVirtual) where T : class
50 | {
51 | return CreateObject(obj, null, isCopyVirtual);
52 | }
53 | ///
54 | /// 拷贝并创建对象
55 | ///
56 | ///
57 | ///
58 | public static T CreateObject(object obj) where T : class
59 | {
60 | return CreateObject(obj, null);
61 | }
62 |
63 |
64 | ///
65 | /// 拷贝并创建对象
66 | ///
67 | ///
68 | /// 不拷贝的属性集合
69 | /// 是否拷贝有Virtual访问修饰符的属性
70 | ///
71 | public static object CreateObject(object obj, string[] notFilds, bool isCopyVirtual)
72 | {
73 | Type type = obj.GetType();
74 | object obj2 = Assembly.GetAssembly(type).CreateInstance(type.FullName);
75 | PropertyCopy(obj, obj2, notFilds, isCopyVirtual);
76 | return obj2;
77 | }
78 | ///
79 | /// 拷贝并创建对象
80 | ///
81 | ///
82 | /// 不拷贝的属性集合
83 | ///
84 | public static object CreateObject(object obj, string[] notFilds)
85 | {
86 | return CreateObject(obj, notFilds, false);
87 | }
88 | ///
89 | /// 拷贝并创建对象
90 | ///
91 | ///
92 | /// 是否拷贝有Virtual访问修饰符的属性
93 | ///
94 | public static object CreateObject(object obj, bool isCopyVirtual)
95 | {
96 | return CreateObject(obj, null, isCopyVirtual);
97 | }
98 | ///
99 | /// 拷贝并创建对象
100 | ///
101 | ///
102 | ///
103 | public static object CreateObject(object obj)
104 | {
105 | return CreateObject(obj, null);
106 | }
107 |
108 |
109 | ///
110 | /// 对象属性拷贝(全匹配拷贝)
111 | ///
112 | /// 源对象()
113 | /// 目标对象(被赋值对象)
114 | /// 不赋值的字段
115 | /// 目标对象
116 | /// 是否拷贝有Virtual访问修饰符的属性
117 | ///
118 | public static object PropertyCopy(object obj1, object obj2, string[] notFilds, bool isCopyVirtual)
119 | {
120 | Type souType = obj1.GetType();
121 | Type tarType = obj2.GetType();
122 | PropertyInfo[] pis = souType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
123 | if (null != pis)
124 | {
125 | foreach (PropertyInfo pi in pis)
126 | {
127 | string propertyName = pi.Name;
128 | if (notFilds != null && notFilds.Count(i => i == propertyName) > 0)
129 | continue;
130 | PropertyInfo pit = tarType.GetProperty(propertyName);
131 | if (pit != null && pi.CanWrite)
132 | {
133 | if (!isCopyVirtual && pit.GetMethod.IsVirtual) continue;
134 | Type ptype = pit.PropertyType;
135 | object value = pi.GetValue(obj1, null);
136 | if (value != null && pit.CanWrite && types.FirstOrDefault(i => i.Equals(ptype)) != null)
137 | {
138 | if (ptype == typeof(DateTime) && ((DateTime)value).Year == 1)
139 | {
140 | continue;
141 | }
142 | pit.SetValue(obj2, value, null);
143 | }
144 | }
145 | }
146 | }
147 | return obj2;
148 | }
149 |
150 | public static object PropertyCopy(object obj1, object obj2, string[] notFilds)
151 | {
152 | return PropertyCopy(obj1, obj2, notFilds, false);
153 | }
154 | ///
155 | /// 对象属性拷贝(全匹配拷贝)
156 | ///
157 | /// 源对象
158 | /// 目标对象(被赋值对象)
159 | /// 目标对象
160 | public static object PropertyCopy(object obj1, object obj2)
161 | {
162 | return PropertyCopy(obj1, obj2, null, false);
163 | }
164 |
165 |
166 | private static Type[] types = new Type[] {
167 | typeof(Int16?),typeof(Int32?),typeof(Int64?),
168 | typeof(Int16),typeof(Int32),typeof(Int64),
169 | typeof(string),typeof(float),typeof(double),
170 | typeof(char),typeof(bool),typeof(decimal),
171 | typeof(float?),typeof(double?),
172 | typeof(char?),typeof(bool?),typeof(decimal?),
173 | typeof(DateTime),typeof(DateTime?),typeof(Byte),typeof(object)
174 | };
175 |
176 | }
177 | }
178 |
--------------------------------------------------------------------------------
/ToolBox.Process/ProcessHelper.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Diagnostics;
4 | using System.Linq;
5 | using System.Runtime.InteropServices;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace ToolBox.Process
10 | {
11 | public static class ProcessHelper
12 | {
13 | ///
14 | /// 根据进程名结束进程 不需要后缀 多个 相同进程名 会被一起结束
15 | ///
16 | ///
17 | public static void KillProcessByName(string ProssName)
18 | {
19 | string newName = ProssName.Replace(".exe", "");
20 | try
21 | {
22 |
23 | Process[] pp = Process.GetProcessesByName(newName);
24 |
25 | for (int i = 0; i < pp.Length; i++)
26 |
27 | {
28 | if (pp[i].ProcessName == newName)
29 | {
30 | pp[i].Kill();
31 |
32 | }
33 |
34 | }
35 |
36 | }
37 |
38 | catch (Exception ex)
39 |
40 | {
41 | Console.WriteLine(ex.Message);
42 |
43 | }
44 |
45 | }
46 |
47 | ///
48 | /// 根据进程名和进程PID,关闭指定进程 - 进程名不需要带后缀
49 | ///
50 | /// 进程名
51 | /// 需要关闭的进程PID
52 | public static void CloseProcessByProcessId(string ProssName, int ClosePid)
53 | {
54 | Process[] pp = Process.GetProcessesByName(ProssName);
55 | for (int i = 0; i < pp.Length; i++)
56 | {
57 | if (pp[i].Id == ClosePid)
58 | {
59 | pp[i].Kill();
60 | }
61 | }
62 | }
63 |
64 | ///
65 | /// 根据进程名获得进程Process对象的集合 - 不需要带上进程后缀
66 | ///
67 | /// 进程名
68 | /// 进程Process 对象集合
69 | /// 找不到返回 false
70 | public static bool GetProcessPorEx_ByProssName(string ProssName, ref List Pro)
71 | {
72 | bool finded = false;
73 | Process[] pp = Process.GetProcessesByName(ProssName);
74 | for (int i = 0; i < pp.Length; i++)
75 | {
76 | if (pp[i].ProcessName == ProssName)
77 | {
78 | finded = true;
79 | Pro.Add(pp[i]);
80 | }
81 | }
82 | if (finded)
83 | {
84 | return true;
85 | }
86 | return false;
87 | }
88 |
89 |
90 |
91 |
92 | [DllImport("user32", EntryPoint = "GetWindowThreadProcessId")]
93 | private static extern int GetWindowThreadProcessId(IntPtr hwnd, out int pid);
94 |
95 | ///
96 | /// 根据窗口句柄获得进程PID和线程PID
97 | ///
98 | /// 句柄
99 | /// 返回 进程PID
100 | /// 方法的返回值,线程PID,进程PID和线程PID这两个概念不同
101 | public static int GetPidByHwnd(int hwnd, out int pid)
102 | {
103 | pid = 0;
104 | return GetWindowThreadProcessId((IntPtr)hwnd, out pid);
105 | }
106 |
107 | ///
108 | /// 根据窗口标题获得进程Process对象
109 | ///
110 | /// 窗口标题
111 | /// 进程Process 对象
112 | /// 找不到返回 false
113 | public static bool GetProcessPor_ByTitle(string Title, out Process Pro)
114 | {
115 | Pro = null;
116 | Process[] arrayProcess = Process.GetProcesses();
117 | foreach (Process p in arrayProcess)
118 | {
119 | if (p.MainWindowTitle.IndexOf(Title) != -1)
120 | {
121 | Pro = p;
122 | return true;
123 | }
124 | }
125 | return false;
126 | }
127 |
128 |
129 | ///
130 | /// 根据窗口标题查找窗口进程PID-返回List
131 | ///
132 | /// 窗口标题
133 | /// List
134 | public static List FindPidEx_ByTitle(string Title)
135 | {
136 | List list = new List();
137 | Process[] arrayProcess = Process.GetProcesses();
138 | foreach (Process p in arrayProcess)
139 | {
140 | if (p.MainWindowTitle.IndexOf(Title) != -1)
141 | {
142 | list.Add(p.Id);
143 |
144 | }
145 | }
146 | return list;
147 | }
148 |
149 |
150 |
151 | ///
152 | /// 根据进程名获得进程PID - 不需要带上进程后缀
153 | ///
154 | /// 进城名
155 | /// 进城PID 找不到返回 0
156 | public static int GetProcessPid(string ProssName)
157 | {
158 | Process[] pp = Process.GetProcessesByName(ProssName);
159 | for (int i = 0; i < pp.Length; i++)
160 | {
161 | if (pp[i].ProcessName == ProssName)
162 | {
163 | return pp[i].Id;
164 | }
165 | }
166 | return 0;
167 | }
168 |
169 |
170 | ///
171 | /// 运行一个指定文件或者程序
172 | ///
173 | /// 文件路径
174 | /// 失败返回false
175 | public static bool Runapp(string Path)
176 | {
177 | try
178 | {
179 | Process pro = new Process();
180 | pro.StartInfo.FileName = @Path;
181 | pro.Start();
182 | }
183 | catch (System.Exception ex)
184 | {
185 | Console.WriteLine(ex.Message);
186 | return false;
187 | }
188 | return true;
189 | }
190 |
191 |
192 | ///
193 | /// 运行一个指定文件或者程序可以带上参数
194 | ///
195 | /// 文件路径
196 | /// 附带参数
197 | /// 失败返回false
198 | private static bool Runapp_ByFlag(string Path, string Flag)
199 | {
200 | try
201 | {
202 | Process pro = new Process();
203 | pro.StartInfo.FileName = @Path;
204 | pro.StartInfo.Arguments = Flag;
205 | pro.Start();
206 | }
207 | catch (System.Exception ex)
208 | {
209 | Console.WriteLine(ex.Message);
210 | return false;
211 | }
212 | return true;
213 | }
214 |
215 |
216 |
217 | ///
218 | /// 通过句柄获得进程路径
219 | ///
220 | /// 句柄
221 | /// 返回 进程路径 找不到返回""
222 | public static string GetAppRunPath_ByHandle(int hwnd)
223 | {
224 | string path = "";
225 | Process[] ps = Process.GetProcesses();
226 | foreach (Process p in ps)
227 | {
228 | if ((int)p.MainWindowHandle == hwnd)
229 | {
230 | path = p.MainModule.FileName.ToString();
231 | }
232 | }
233 | return path;
234 | }
235 |
236 |
237 |
238 | ///
239 | /// 通过进程名获得进程路径 不需要后缀
240 | ///
241 | /// 句柄
242 | /// 返回 进程路径 找不到返回""
243 | public static string GetAppRunPath_ByName(string prossName)
244 | {
245 | string path = "";
246 | Process[] ps = Process.GetProcesses();
247 | foreach (Process p in ps)
248 | {
249 | if (p.ProcessName == prossName)
250 | {
251 | path = p.MainModule.FileName.ToString();
252 | return path;
253 | }
254 | }
255 |
256 |
257 | return "";
258 | }
259 |
260 |
261 | }
262 | }
263 |
--------------------------------------------------------------------------------
/ToolBox.DateTime/ToolBox.DateTimeTool.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | ToolBox.DateTimeTool
5 |
6 |
7 |
8 |
9 | 获取本日开始时间(0点0分0秒)
10 |
11 |
12 |
13 |
14 |
15 |
16 | 获取本日结束时间(23点59分59秒)
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 | 北京时间转换成unix时间戳(10位/秒)
80 |
81 |
82 |
83 |
84 |
85 |
86 | 格林威治时间转换成unix时间戳(10位/秒)
87 |
88 |
89 |
90 |
91 |
92 |
93 | 北京时间转换成unix时间戳(13位/毫秒)
94 |
95 |
96 |
97 |
98 |
99 |
100 | 格林威治时间转换成unix时间戳(13位/毫秒)
101 |
102 |
103 |
104 |
105 |
106 |
107 | 10位unix时间戳转换成北京时间
108 |
109 |
110 |
111 |
112 |
113 |
114 | 10位unix时间戳转换成格林威治
115 |
116 |
117 |
118 |
119 |
120 |
121 | 13位unix时间戳转换成北京时间
122 |
123 |
124 |
125 |
126 |
127 |
128 | 13位unix时间戳转换成格林威治
129 |
130 |
131 |
132 |
133 |
134 |
135 | 当前日期所在月份第一个指定星期几的日期
136 |
137 | 给定日期
138 | 星期几
139 | 所对应的日期
140 |
141 |
142 |
143 | 当前日期所在月份最后1个指定星期几的日期
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 | 公历闰年的精确计算方法:(按一回归年365天5小时48分45.5秒)
205 | 普通年能被4整除而不能被100整除的为闰年。 (如2004年就是闰年,1900年不是闰年)
206 | 世纪年能被400整除而不能被3200整除的为闰年。 (如2000年是闰年,3200年不是闰年)
207 | 对于数值很大的年份能整除3200,但同时又能整除172800则又是闰年。(如172800年是闰年,86400年不是闰年)
208 | 公元前闰年规则如下:
209 | 非整百年:年数除4余数为1是闰年,即公元前1、5、9……年;
210 | 整百年:年数除400余数为1是闰年,年数除3200余数为1,不是闰年,年数除172800余1又为闰年,即公元前401、801……年。
211 |
212 |
213 |
214 |
215 |
216 |
217 | 获取当前年天数
218 |
219 |
220 |
221 |
222 |
223 |
224 | 获取当前年天数
225 |
226 |
227 |
228 |
229 |
230 |
231 |
--------------------------------------------------------------------------------
/ToolBox/ToolBox.DateTime/ToolBox.DateTimeTool.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | ToolBox.DateTimeTool
5 |
6 |
7 |
8 |
9 | 获取本日开始时间(0点0分0秒)
10 |
11 |
12 |
13 |
14 |
15 |
16 | 获取本日结束时间(23点59分59秒999毫秒)
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 | 北京时间转换成unix时间戳(10位/秒)
80 |
81 |
82 |
83 |
84 |
85 |
86 | 格林威治时间转换成unix时间戳(10位/秒)
87 |
88 |
89 |
90 |
91 |
92 |
93 | 北京时间转换成unix时间戳(13位/毫秒)
94 |
95 |
96 |
97 |
98 |
99 |
100 | 格林威治时间转换成unix时间戳(13位/毫秒)
101 |
102 |
103 |
104 |
105 |
106 |
107 | 10位unix时间戳转换成北京时间
108 |
109 |
110 |
111 |
112 |
113 |
114 | 10位unix时间戳转换成格林威治
115 |
116 |
117 |
118 |
119 |
120 |
121 | 13位unix时间戳转换成北京时间
122 |
123 |
124 |
125 |
126 |
127 |
128 | 13位unix时间戳转换成格林威治
129 |
130 |
131 |
132 |
133 |
134 |
135 | 当前日期所在月份第一个指定星期几的日期
136 |
137 | 给定日期
138 | 星期几
139 | 所对应的日期
140 |
141 |
142 |
143 | 当前日期所在月份最后1个指定星期几的日期
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 | 公历闰年的精确计算方法:(按一回归年365天5小时48分45.5秒)
205 | 普通年能被4整除而不能被100整除的为闰年。 (如2004年就是闰年,1900年不是闰年)
206 | 世纪年能被400整除而不能被3200整除的为闰年。 (如2000年是闰年,3200年不是闰年)
207 | 对于数值很大的年份能整除3200,但同时又能整除172800则又是闰年。(如172800年是闰年,86400年不是闰年)
208 | 公元前闰年规则如下:
209 | 非整百年:年数除4余数为1是闰年,即公元前1、5、9……年;
210 | 整百年:年数除400余数为1是闰年,年数除3200余数为1,不是闰年,年数除172800余1又为闰年,即公元前401、801……年。
211 |
212 |
213 |
214 |
215 |
216 |
217 | 获取当前年天数
218 |
219 |
220 |
221 |
222 |
223 |
224 | 获取当前年天数
225 |
226 |
227 |
228 |
229 |
230 |
231 |
--------------------------------------------------------------------------------
/ToolBox.Socket/TcpClient.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Net;
5 | using System.Net.Sockets;
6 | using System.Text;
7 | using System.Threading;
8 | using System.Threading.Tasks;
9 | using System.Timers;
10 |
11 | namespace ToolBox.Socket
12 | {
13 | ///
14 | /// TCP客户端
15 | ///
16 | public class TcpClient
17 | {
18 | ///
19 | /// 客户端套接字
20 | ///
21 | public System.Net.Sockets.Socket mySocket;
22 |
23 | ///
24 | /// 接收线程
25 | ///
26 | public Thread recThread;
27 |
28 | ///
29 | /// 本地ip
30 | ///
31 | public string EndPointIp;
32 |
33 |
34 | private bool IsReceive = false;
35 |
36 |
37 | ///
38 | /// 心跳检查间隔(Heartbeat check interval)
39 | ///
40 | public int HeartbeatCheckInterval { get; set; } = 3000;
41 |
42 |
43 | ///
44 | /// (是否打开des加密,默认是打开)Whether to open des encryption, open by default
45 | ///
46 | public bool IsOpenDesEnc { get; set; } = true;
47 |
48 | ///
49 | /// 开始连接服务器
50 | ///
51 | ///
52 | ///
53 | public void StartConnect(int port, string ip = "127.0.0.1")
54 | {
55 |
56 | try
57 | {
58 | mySocket = new System.Net.Sockets.Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
59 | IPAddress address = IPAddress.Parse(ip);
60 | IPEndPoint endPoint = new IPEndPoint(address, port);
61 |
62 | //开始异步连接
63 | mySocket.BeginConnect(endPoint, asyncResult =>
64 | {
65 |
66 | try
67 | {
68 | mySocket.EndConnect(asyncResult); //结束异步连接
69 | // localEndPointIp = mySocket.LocalEndPoint.ToString(); //得到ip地址
70 |
71 | OnSuccess?.Invoke(this); //连接成功的回调
72 |
73 | recThread = new Thread(RecMsg);
74 | recThread.IsBackground = true;
75 | recThread.Start(mySocket);
76 |
77 |
78 | Task.Run(() =>
79 | {
80 | while (true) {
81 |
82 | if (mySocket != null && IsReceive)
83 | {
84 | string ss = EndPointIp;
85 | SendMsg("hear," + ss);
86 |
87 | }
88 | Thread.Sleep(HeartbeatCheckInterval);
89 |
90 | }
91 |
92 | });
93 |
94 |
95 | }
96 | catch (Exception ex)
97 | {
98 | OnError?.Invoke(ex);
99 | }
100 |
101 | }, null);
102 |
103 | }
104 | catch (Exception ex)
105 | {
106 |
107 | OnError?.Invoke(ex); //报错的回调
108 | }
109 |
110 |
111 | }
112 |
113 |
114 |
115 | ///
116 | /// Set key,the key is length must >=8
117 | ///
118 | ///
119 | ///
120 | public void SetEncryptKey(string key)
121 | {
122 |
123 | if (!SocketDesHelper.SetEncryptKey(key))
124 | {
125 |
126 | throw new Exception("The key is set incorrectly, the length is greater than or equal to 8, and cannot contain Chinese.");
127 |
128 | }
129 |
130 | }
131 |
132 |
133 |
134 |
135 | ///
136 | /// 接收信息的线程
137 | ///
138 | private void RecMsg(object socket)
139 | {
140 | int headSize = 4;
141 | byte[] surplusBuffer = null;
142 |
143 | System.Net.Sockets.Socket mySocket = socket as System.Net.Sockets.Socket ;
144 |
145 | while (true)
146 | {
147 |
148 | int count = -1;
149 |
150 | try
151 | {
152 | byte[] vs = new byte[1024];
153 | count = mySocket.Receive(vs);
154 | int bytesRead = vs.Length;
155 |
156 | if (bytesRead > 0)
157 | {
158 | surplusBuffer = vs;
159 | }
160 | else
161 | {
162 | surplusBuffer = surplusBuffer.Concat(vs).ToArray();
163 | }
164 |
165 | int haveRead = 0;
166 | int totalLen = surplusBuffer.Length;
167 |
168 | while (haveRead <= totalLen)
169 | {
170 |
171 | if (totalLen - haveRead < headSize)
172 | {
173 | byte[] byteSub = new byte[totalLen - haveRead];
174 | Buffer.BlockCopy(surplusBuffer, haveRead, byteSub, 0, totalLen - haveRead);
175 | surplusBuffer = byteSub;
176 | totalLen = 0;
177 | break;
178 | }
179 |
180 | //如果是够一个完整包,读取包头的数据
181 | byte[] headByte = new byte[headSize];
182 | Buffer.BlockCopy(surplusBuffer, haveRead, headByte, 0, headSize);
183 |
184 | int bodySize = BitConverter.ToInt32(headByte, 0);
185 |
186 | if (bodySize == 0)
187 | {
188 |
189 | surplusBuffer = null;
190 | totalLen = 0;
191 | break;
192 |
193 | }
194 |
195 |
196 | if (haveRead + headSize + bodySize > totalLen)
197 | {
198 |
199 | byte[] byteSub = new byte[totalLen - haveRead];
200 | Buffer.BlockCopy(surplusBuffer, haveRead, byteSub, 0, totalLen - haveRead);
201 | surplusBuffer = byteSub;
202 | // Console.WriteLine("不够一个包,拆出来保存");
203 | break;
204 |
205 | }
206 | else
207 | {
208 |
209 | string strc = Encoding.UTF8.GetString(surplusBuffer, haveRead + headSize, bodySize).StringDecrypt(IsOpenDesEnc);
210 |
211 | if (IsReceive == false)
212 | {
213 | string[] ss = strc.Split(',');
214 | if (ss.Count()== 2 && ss[0].ToString().Equals("YouIP")) {
215 | EndPointIp = ss[1].ToString().Trim();
216 | }
217 | IsReceive = true;
218 | }
219 | else {
220 |
221 | // Console.WriteLine("------------结果:" + strc);
222 | OnRecMessage?.Invoke(strc, this);
223 |
224 | }
225 |
226 | haveRead = haveRead + headSize + bodySize;
227 |
228 | if (headSize + bodySize == bytesRead)
229 | {
230 | surplusBuffer = null;
231 | totalLen = 0;
232 | }
233 | }
234 |
235 | }
236 |
237 | }
238 | catch (Exception ex)
239 | {
240 | // Console.WriteLine("接收的线程组错误信息:" + ex);
241 |
242 | OnError?.Invoke(ex);
243 | break;
244 |
245 | }
246 |
247 | }
248 |
249 | }
250 |
251 | ///
252 | /// 关闭
253 | ///
254 | public void Close()
255 | {
256 |
257 | if (mySocket != null && recThread != null)
258 | {
259 | mySocket.Close();
260 | recThread = null;
261 | }
262 |
263 | }
264 |
265 |
266 | ///
267 | /// 发送消息
268 | ///
269 | ///
270 | public void SendMsg(string msg)
271 | {
272 |
273 | if (mySocket.Connected)
274 | {
275 | mySocket.Send(SocketTools.GetBytes(msg,IsOpenDesEnc));
276 | }
277 | else
278 | {
279 | OnMessage?.Invoke("Not connected to the server~");
280 |
281 | }
282 | }
283 |
284 |
285 |
286 | ///
287 | /// 连接成功
288 | ///
289 | public Action OnSuccess { get; set; } //连接成功
290 |
291 |
292 | ///
293 | /// 接收消息
294 | ///
295 | public Action OnRecMessage { get; set; } //接收消息
296 |
297 |
298 | ///
299 | /// 错误处理
300 | ///
301 | public Action OnError { get; set; }
302 |
303 | ///
304 | /// 接收信息
305 | ///
306 | public Action OnMessage { get; set; }
307 |
308 |
309 |
310 |
311 | }
312 | }
313 |
--------------------------------------------------------------------------------
/ToolBox.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.29709.97
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ToolBox.DateTimeTool", "ToolBox.DateTime\ToolBox.DateTimeTool.csproj", "{A25954E2-BC9C-411E-A911-96ABA6CB6F47}"
7 | EndProject
8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ToolBox.Cmd", "ToolBox.Cmd\ToolBox.Cmd.csproj", "{621AF054-7FEB-4B7C-B617-D04113BE53F6}"
9 | EndProject
10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ToolBox.Common", "ToolBox.Common\ToolBox.Common.csproj", "{2F2CBF1C-084B-44D5-A94E-E1CDA37DAF29}"
11 | EndProject
12 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ToolBox.Files", "ToolBox.Files\ToolBox.Files.csproj", "{E337219D-C541-4D44-8C18-4076120355C6}"
13 | EndProject
14 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ToolBox.Ftp", "ToolBox.Ftp\ToolBox.Ftp.csproj", "{B15B0DA7-2A94-49F2-AFD2-EE01F3DC114D}"
15 | EndProject
16 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ToolBox.Http", "ToolBox.Http\ToolBox.Http.csproj", "{2DBA542C-4F60-404C-8644-40DCCA633250}"
17 | EndProject
18 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ToolBox.Json", "ToolBox.Json\ToolBox.Json.csproj", "{B94A3D4B-F8B9-4018-AFEA-41FDB62888FA}"
19 | EndProject
20 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ToolBox.Object", "ToolBox.Object\ToolBox.Object.csproj", "{F90AE6A1-3CE8-4761-9854-2AD8642CFC0C}"
21 | EndProject
22 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ToolBox.Process", "ToolBox.Process\ToolBox.Process.csproj", "{2C2DFBD3-8D03-4110-8748-721533077EC7}"
23 | EndProject
24 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ToolBox.Reflection", "ToolBox.Reflection\ToolBox.Reflection.csproj", "{331E760C-6309-4608-8874-C5946EC94578}"
25 | EndProject
26 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ToolBox.Security", "ToolBox.Security\ToolBox.Security.csproj", "{417D3F4F-01C4-4467-A6A6-7DDF687166A6}"
27 | EndProject
28 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ToolBox.Socket", "ToolBox.Socket\ToolBox.Socket.csproj", "{C2FF3905-8BB9-429B-9733-24D464B57024}"
29 | EndProject
30 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ToolBox.String", "ToolBox.String\ToolBox.String.csproj", "{5DD9799F-118D-41BE-A968-DF021424E16A}"
31 | EndProject
32 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ToolBox.Test", "ToolBox.Test\ToolBox.Test.csproj", "{964C0B7C-B347-4036-B0A9-9FD6AD1D0050}"
33 | EndProject
34 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "测试项目", "测试项目", "{45A09EC5-7EA9-4B71-91EC-7540BFD38827}"
35 | EndProject
36 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ToolBox.Framework.Test", "ToolBox.Framework.Test\ToolBox.Framework.Test.csproj", "{EA2AE0DE-3B56-4B8C-885C-668228872C55}"
37 | EndProject
38 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ToolBox.Log", "ToolBox.Log\ToolBox.Log.csproj", "{1581CFA6-8E1A-4A24-919A-08E04A8256F3}"
39 | EndProject
40 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ToolBox.PhoneModelParse", "ToolBox.Phone\ToolBox.PhoneModelParse.csproj", "{74549AC8-4C5C-421C-8701-DD7E56F1E861}"
41 | EndProject
42 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ToolBox.UserAgentParse", "ToolBox.UserAgentParse\ToolBox.UserAgentParse.csproj", "{874D7BF3-1E4E-408D-A7C5-BBBE0A029CB6}"
43 | EndProject
44 | Global
45 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
46 | Debug|Any CPU = Debug|Any CPU
47 | Release|Any CPU = Release|Any CPU
48 | EndGlobalSection
49 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
50 | {A25954E2-BC9C-411E-A911-96ABA6CB6F47}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
51 | {A25954E2-BC9C-411E-A911-96ABA6CB6F47}.Debug|Any CPU.Build.0 = Debug|Any CPU
52 | {A25954E2-BC9C-411E-A911-96ABA6CB6F47}.Release|Any CPU.ActiveCfg = Release|Any CPU
53 | {A25954E2-BC9C-411E-A911-96ABA6CB6F47}.Release|Any CPU.Build.0 = Release|Any CPU
54 | {621AF054-7FEB-4B7C-B617-D04113BE53F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
55 | {621AF054-7FEB-4B7C-B617-D04113BE53F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
56 | {621AF054-7FEB-4B7C-B617-D04113BE53F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
57 | {621AF054-7FEB-4B7C-B617-D04113BE53F6}.Release|Any CPU.Build.0 = Release|Any CPU
58 | {2F2CBF1C-084B-44D5-A94E-E1CDA37DAF29}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
59 | {2F2CBF1C-084B-44D5-A94E-E1CDA37DAF29}.Debug|Any CPU.Build.0 = Debug|Any CPU
60 | {2F2CBF1C-084B-44D5-A94E-E1CDA37DAF29}.Release|Any CPU.ActiveCfg = Release|Any CPU
61 | {2F2CBF1C-084B-44D5-A94E-E1CDA37DAF29}.Release|Any CPU.Build.0 = Release|Any CPU
62 | {E337219D-C541-4D44-8C18-4076120355C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
63 | {E337219D-C541-4D44-8C18-4076120355C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
64 | {E337219D-C541-4D44-8C18-4076120355C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
65 | {E337219D-C541-4D44-8C18-4076120355C6}.Release|Any CPU.Build.0 = Release|Any CPU
66 | {B15B0DA7-2A94-49F2-AFD2-EE01F3DC114D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
67 | {B15B0DA7-2A94-49F2-AFD2-EE01F3DC114D}.Debug|Any CPU.Build.0 = Debug|Any CPU
68 | {B15B0DA7-2A94-49F2-AFD2-EE01F3DC114D}.Release|Any CPU.ActiveCfg = Release|Any CPU
69 | {B15B0DA7-2A94-49F2-AFD2-EE01F3DC114D}.Release|Any CPU.Build.0 = Release|Any CPU
70 | {2DBA542C-4F60-404C-8644-40DCCA633250}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
71 | {2DBA542C-4F60-404C-8644-40DCCA633250}.Debug|Any CPU.Build.0 = Debug|Any CPU
72 | {2DBA542C-4F60-404C-8644-40DCCA633250}.Release|Any CPU.ActiveCfg = Release|Any CPU
73 | {2DBA542C-4F60-404C-8644-40DCCA633250}.Release|Any CPU.Build.0 = Release|Any CPU
74 | {B94A3D4B-F8B9-4018-AFEA-41FDB62888FA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
75 | {B94A3D4B-F8B9-4018-AFEA-41FDB62888FA}.Debug|Any CPU.Build.0 = Debug|Any CPU
76 | {B94A3D4B-F8B9-4018-AFEA-41FDB62888FA}.Release|Any CPU.ActiveCfg = Release|Any CPU
77 | {B94A3D4B-F8B9-4018-AFEA-41FDB62888FA}.Release|Any CPU.Build.0 = Release|Any CPU
78 | {F90AE6A1-3CE8-4761-9854-2AD8642CFC0C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
79 | {F90AE6A1-3CE8-4761-9854-2AD8642CFC0C}.Debug|Any CPU.Build.0 = Debug|Any CPU
80 | {F90AE6A1-3CE8-4761-9854-2AD8642CFC0C}.Release|Any CPU.ActiveCfg = Release|Any CPU
81 | {F90AE6A1-3CE8-4761-9854-2AD8642CFC0C}.Release|Any CPU.Build.0 = Release|Any CPU
82 | {2C2DFBD3-8D03-4110-8748-721533077EC7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
83 | {2C2DFBD3-8D03-4110-8748-721533077EC7}.Debug|Any CPU.Build.0 = Debug|Any CPU
84 | {2C2DFBD3-8D03-4110-8748-721533077EC7}.Release|Any CPU.ActiveCfg = Release|Any CPU
85 | {2C2DFBD3-8D03-4110-8748-721533077EC7}.Release|Any CPU.Build.0 = Release|Any CPU
86 | {331E760C-6309-4608-8874-C5946EC94578}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
87 | {331E760C-6309-4608-8874-C5946EC94578}.Debug|Any CPU.Build.0 = Debug|Any CPU
88 | {331E760C-6309-4608-8874-C5946EC94578}.Release|Any CPU.ActiveCfg = Release|Any CPU
89 | {331E760C-6309-4608-8874-C5946EC94578}.Release|Any CPU.Build.0 = Release|Any CPU
90 | {417D3F4F-01C4-4467-A6A6-7DDF687166A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
91 | {417D3F4F-01C4-4467-A6A6-7DDF687166A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
92 | {417D3F4F-01C4-4467-A6A6-7DDF687166A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
93 | {417D3F4F-01C4-4467-A6A6-7DDF687166A6}.Release|Any CPU.Build.0 = Release|Any CPU
94 | {C2FF3905-8BB9-429B-9733-24D464B57024}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
95 | {C2FF3905-8BB9-429B-9733-24D464B57024}.Debug|Any CPU.Build.0 = Debug|Any CPU
96 | {C2FF3905-8BB9-429B-9733-24D464B57024}.Release|Any CPU.ActiveCfg = Release|Any CPU
97 | {C2FF3905-8BB9-429B-9733-24D464B57024}.Release|Any CPU.Build.0 = Release|Any CPU
98 | {5DD9799F-118D-41BE-A968-DF021424E16A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
99 | {5DD9799F-118D-41BE-A968-DF021424E16A}.Debug|Any CPU.Build.0 = Debug|Any CPU
100 | {5DD9799F-118D-41BE-A968-DF021424E16A}.Release|Any CPU.ActiveCfg = Release|Any CPU
101 | {5DD9799F-118D-41BE-A968-DF021424E16A}.Release|Any CPU.Build.0 = Release|Any CPU
102 | {964C0B7C-B347-4036-B0A9-9FD6AD1D0050}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
103 | {964C0B7C-B347-4036-B0A9-9FD6AD1D0050}.Debug|Any CPU.Build.0 = Debug|Any CPU
104 | {964C0B7C-B347-4036-B0A9-9FD6AD1D0050}.Release|Any CPU.ActiveCfg = Release|Any CPU
105 | {964C0B7C-B347-4036-B0A9-9FD6AD1D0050}.Release|Any CPU.Build.0 = Release|Any CPU
106 | {EA2AE0DE-3B56-4B8C-885C-668228872C55}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
107 | {EA2AE0DE-3B56-4B8C-885C-668228872C55}.Debug|Any CPU.Build.0 = Debug|Any CPU
108 | {EA2AE0DE-3B56-4B8C-885C-668228872C55}.Release|Any CPU.ActiveCfg = Release|Any CPU
109 | {EA2AE0DE-3B56-4B8C-885C-668228872C55}.Release|Any CPU.Build.0 = Release|Any CPU
110 | {1581CFA6-8E1A-4A24-919A-08E04A8256F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
111 | {1581CFA6-8E1A-4A24-919A-08E04A8256F3}.Debug|Any CPU.Build.0 = Debug|Any CPU
112 | {1581CFA6-8E1A-4A24-919A-08E04A8256F3}.Release|Any CPU.ActiveCfg = Release|Any CPU
113 | {1581CFA6-8E1A-4A24-919A-08E04A8256F3}.Release|Any CPU.Build.0 = Release|Any CPU
114 | {74549AC8-4C5C-421C-8701-DD7E56F1E861}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
115 | {74549AC8-4C5C-421C-8701-DD7E56F1E861}.Debug|Any CPU.Build.0 = Debug|Any CPU
116 | {74549AC8-4C5C-421C-8701-DD7E56F1E861}.Release|Any CPU.ActiveCfg = Release|Any CPU
117 | {74549AC8-4C5C-421C-8701-DD7E56F1E861}.Release|Any CPU.Build.0 = Release|Any CPU
118 | {874D7BF3-1E4E-408D-A7C5-BBBE0A029CB6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
119 | {874D7BF3-1E4E-408D-A7C5-BBBE0A029CB6}.Debug|Any CPU.Build.0 = Debug|Any CPU
120 | {874D7BF3-1E4E-408D-A7C5-BBBE0A029CB6}.Release|Any CPU.ActiveCfg = Release|Any CPU
121 | {874D7BF3-1E4E-408D-A7C5-BBBE0A029CB6}.Release|Any CPU.Build.0 = Release|Any CPU
122 | EndGlobalSection
123 | GlobalSection(SolutionProperties) = preSolution
124 | HideSolutionNode = FALSE
125 | EndGlobalSection
126 | GlobalSection(NestedProjects) = preSolution
127 | {964C0B7C-B347-4036-B0A9-9FD6AD1D0050} = {45A09EC5-7EA9-4B71-91EC-7540BFD38827}
128 | {EA2AE0DE-3B56-4B8C-885C-668228872C55} = {45A09EC5-7EA9-4B71-91EC-7540BFD38827}
129 | EndGlobalSection
130 | GlobalSection(ExtensibilityGlobals) = postSolution
131 | SolutionGuid = {69F874AA-9512-48AD-8294-812FDC60C849}
132 | EndGlobalSection
133 | EndGlobal
134 |
--------------------------------------------------------------------------------
/ToolBox.UserAgentParse/UaUnit.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text.RegularExpressions;
4 | using ToolBox.PhoneModelParse;
5 |
6 | namespace ToolBox.UserAgentParse
7 | {
8 | public class UaUnit
9 | {
10 | private string UaStr { get; set; }
11 |
12 | public UaUnit(string ua)
13 | {
14 | UaStr = ua;
15 | }
16 |
17 | public UaInfo Parse()
18 | {
19 | if (string.IsNullOrEmpty(UaStr)) return null;
20 | var result = new UaInfo();
21 | var systemInfo = GetSystemInfo();
22 | result.SystemName = systemInfo.Item1;
23 | result.SystemVersion = systemInfo.Item2;
24 | result.Platform = GetPlatform();
25 | result.BrowserKernel = GetBrowserKernel();
26 | if (result.Platform == "Mobile")
27 | {
28 | var phoneModelInfo = GetPhoneModel();
29 | result.PhoneModelCode = phoneModelInfo.Item1;
30 | result.PhoneModelName = phoneModelInfo.Item2;
31 | }
32 | var browserInfo = GetBrowserName();
33 | result.BrowserName = browserInfo.Item1;
34 | result.BrowserVersion = browserInfo.Item2;
35 | return result;
36 | }
37 |
38 | ///
39 | /// 获得手机型号
40 | ///
41 | ///
42 | private (string, string) GetPhoneModel()
43 | {
44 | var str1 = string.Empty;
45 | var str2 = string.Empty;
46 | if (UaStr.Contains("Android"))
47 | {
48 | var jiexi1 = Regex.Split(UaStr, "Build", RegexOptions.IgnoreCase);
49 | var jiexi2 = jiexi1[0].Split(';');
50 | str1 = jiexi2[jiexi2.Length - 1].Trim();
51 | }
52 | if (UaStr.Contains("iPhone"))
53 | {
54 | str1 = "iPhone";
55 | }
56 | return (str1, new PhoneModelNumberTool().ModelNumberToName(str1));
57 | }
58 |
59 | ///
60 | /// 获得浏览器名称
61 | ///
62 | ///
63 | private (string, string) GetBrowserName()
64 | {
65 | if (UaStr.Contains("LBBROWSER")) return ("猎豹浏览器", GetVersion("LBBROWSER"));
66 | if (UaStr.Contains("Chromium")) return ("Chromium", GetVersion("Chromium"));
67 | if (UaStr.Contains("Opera")) return ("欧朋浏览器", GetVersion("Opera"));
68 | if (UaStr.Contains("OPR")) return ("欧朋浏览器", GetVersion("OPR"));
69 | if (UaStr.Contains("Firefox")) return ("火狐浏览器", GetVersion("Firefox"));
70 | if (UaStr.Contains("UCBrowser")) return ("UC浏览器", GetVersion("UCBrowser"));
71 | if (UaStr.Contains("SeaMonkey")) return ("SeaMonkey", GetVersion("SeaMonkey"));
72 | if (UaStr.Contains("MicroMessenger")) return ("微信浏览器", GetVersion("MicroMessenger"));
73 | if (UaStr.Contains("QQBrowser")) return ("QQ浏览器", GetVersion("QQBrowser"));
74 | if (UaStr.Contains("MiuiBrowser")) return ("小米浏览器", GetVersion("MiuiBrowser"));
75 | if (UaStr.Contains("baiduboxapp")) return ("手机百度APP浏览器", GetVersion("baiduboxapp"));
76 | if (UaStr.Contains("360SE")) return ("360浏览器", GetVersion("360SE"));
77 | if (UaStr.Contains("360EE")) return ("360浏览器", GetVersion("360EE"));
78 | if (UaStr.Contains("weibo")) return ("新浪微博浏览器", GetVersion("__weibo__"));
79 | if (UaStr.Contains("NokiaBrowser")) return ("诺基亚浏览器", GetVersion("NokiaBrowser"));
80 | if (UaStr.Contains("Edge")) return ("Edge浏览器", GetVersion("Edge"));
81 | if (UaStr.Contains("MSIE")) return ("IE浏览器", GetVersion("MSIE"));
82 | if (UaStr.Contains("Trident") || UaStr.Contains("WOW64")) return ("IE浏览器", "11");
83 | if (UaStr.Contains("Chrome")) return ("谷歌浏览器", GetVersion("Chrome"));
84 | if (UaStr.Contains("CrMo")) return ("谷歌浏览器", GetVersion("CrMo"));
85 | if (UaStr.Contains("curl")) return ("curl", GetVersion("curl"));
86 | if (UaStr.Contains("Safari")) return ("Safari浏览器", GetVersion("Safari"));
87 | return ("", "");
88 | }
89 |
90 | ///
91 | /// 获得系统名称
92 | ///
93 | ///
94 | private (string, string) GetSystemInfo()
95 | {
96 | if (UaStr.Contains("Android")) return ("Android OS", GetVersion("Android"));
97 | if (UaStr.Contains("iPad")) return ("iPad OS", GetVersion("OS"));
98 | if (UaStr.Contains("iPhone")) return ("iPhone OS", GetVersion("OS"));
99 | if (UaStr.Contains("BB") && UaStr.Contains("Mobile")) return ("Black Berry", GetVersion("BB"));
100 | if (UaStr.Contains("RIM Tablet")) return ("Black Berry", GetVersion("OS"));
101 | if (UaStr.Contains("BlackBerry")) return ("Black Berry", GetVersion("BlackBerry"));
102 | if (UaStr.Contains("Windows Phone")) return ("Windows Phone", GetVersion("Windows Phone"));
103 | if (UaStr.Contains("Mac OS X")) return ("Mac OS X", GetVersion("Mac OS X"));
104 | if (UaStr.Contains("MacOS")) return ("Mac OS", "");
105 | if (UaStr.Contains("Windows NT 5.1") && !UaStr.Contains("Windows NT 5.2")) return ("Windows", "XP");
106 | if (UaStr.Contains("Windows NT 6.0")) return ("Windows", "Vista");
107 | if (UaStr.Contains("Windows NT 6.1")) return ("Windows", "7");
108 | if (UaStr.Contains("Windows NT 6.2")) return ("Windows", "8");
109 | if (UaStr.Contains("Windows NT 6.3")) return ("Windows", "8.1");
110 | if (UaStr.Contains("Windows NT 10")) return ("Windows", "10");
111 | if (UaStr.Contains("CentOS")) return ("Linux CentOS", GetVersion("CentOS"));
112 | if (UaStr.Contains("Red Hat")) return ("Linux Red Hat", GetVersion("Red Hat"));
113 | if (UaStr.Contains("SUSE")) return ("Linux SUSE", GetVersion("SUSE"));
114 | if (UaStr.Contains("Debian")) return ("Linux Debian", GetVersion("Debian"));
115 | if (UaStr.Contains("Arch Linux")) return ("Arch Linux", GetVersion("Arch Linux"));
116 | if (UaStr.Contains("Fedora")) return ("Linux Fedora", GetVersion("Fedora"));
117 | if (UaStr.Contains("Ubuntu")) return ("Linux Ubuntu", GetVersion("Ubuntu"));
118 | if (UaStr.Contains("Linux")) return ("Linux", GetVersion("Linux"));
119 | if (UaStr.Contains("curl")) return ("curl", GetVersion("curl"));
120 | return ("Unknow", "Unknow");
121 | }
122 |
123 | ///
124 | /// 获得版本号
125 | ///
126 | ///
127 | private string GetVersion(string value)
128 | {
129 | string text = UaStr.Substring(UaStr.IndexOf(value) + value.Length).TrimStart('-').TrimStart()
130 | .TrimStart('/')
131 | .TrimStart();
132 | string text2 = string.Empty;
133 | string text3 = text;
134 | for (int i = 0; i < text3.Length; i++)
135 | {
136 | char c = text3[i];
137 | bool flag = false;
138 | int result = 0;
139 | if (int.TryParse(c.ToString(), out result))
140 | {
141 | text2 += c.ToString();
142 | flag = true;
143 | }
144 | if (c == '.' || c == '_')
145 | {
146 | text2 += ".";
147 | flag = true;
148 | }
149 | if (!flag)
150 | {
151 | break;
152 | }
153 | }
154 | return text2;
155 | }
156 |
157 | ///
158 | /// 获得浏览器内核
159 | ///
160 | ///
161 | private string GetBrowserKernel()
162 | {
163 | if (UaStr.Contains("Presto")) return "Presto";
164 | if (UaStr.Contains("WebKit")) return "Webkit";
165 | if (UaStr.Contains("Blink")) return "Blink";
166 | if (UaStr.Contains("Trident")) return "Trident";
167 | if (UaStr.Contains("Gecko")) return "Gecko";
168 | return "";
169 | }
170 |
171 | ///
172 | /// 获得平台
173 | ///
174 | ///
175 | private string GetPlatform()
176 | {
177 | if (UaStr.Contains("Android") || UaStr.Contains("iPad") || UaStr.Contains("iPhone") || UaStr.Contains("Kindle Fire") || UaStr.Contains("Black Berry") || UaStr.Contains("Windows Phone"))
178 | {
179 | return "Mobile";
180 | }
181 | if (UaStr.Contains("Mac") || UaStr.Contains("Windows") || UaStr.Contains("Linux"))
182 | {
183 | return "Desktop";
184 | }
185 | return "Unknown";
186 | }
187 |
188 | }
189 | public class UaInfo
190 | {
191 | ///
192 | /// 系统名称
193 | ///
194 | public string SystemName { get; set; }
195 |
196 | ///
197 | /// 系统版本号
198 | ///
199 | public string SystemVersion { get; set; }
200 |
201 | ///
202 | /// 浏览器名称
203 | ///
204 | public string BrowserName { get; set; }
205 |
206 | ///
207 | /// 浏览器版本号
208 | ///
209 | public string BrowserVersion { get; set; }
210 |
211 | ///
212 | /// 浏览器内核
213 | ///
214 | public string BrowserKernel { get; set; }
215 |
216 | ///
217 | /// 手机型号代码
218 | ///
219 | public string PhoneModelCode { get; set; }
220 |
221 | ///
222 | /// 手机型号名称
223 | ///
224 | public string PhoneModelName { get; set; }
225 |
226 | ///
227 | /// 平台
228 | ///
229 | public string Platform { get; set; }
230 |
231 | }
232 |
233 | }
234 |
--------------------------------------------------------------------------------
/ToolBox.Files/ToolBox.Files.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | ToolBox.Files
5 |
6 |
7 |
8 |
9 | 删除指定目录的所有文件
10 |
11 | 操作目录
12 | 默认为false 如果为true,包含对子目录的操作
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 | 如果为true,覆盖同名文件,否则不覆盖
69 | 如果为true,包含目录,否则不包含
70 |
71 |
72 |
73 | 移动指定目录的所有文件
74 |
75 | 原始目录
76 | 目标目录
77 | 如果为true,覆盖同名文件,否则不覆盖
78 | 如果为true,包含目录,否则不包含
79 |
80 |
81 |
82 | 获取指定目录及子目录中所有文件列表
83 |
84 | 指定目录的绝对路径
85 | 模式字符串,"*"代表0或N个字符,"?"代表1个字符。
86 | 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。
87 | 是否搜索子目录
88 |
89 |
90 |
91 | 检测指定目录中是否存在指定的文件,若要搜索子目录请使用重载方法.
92 |
93 | 指定目录的绝对路径
94 | 模式字符串,"*"代表0或N个字符,"?"代表1个字符。
95 | 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。
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 | 获取文件的md5值
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 | 以UTF8编码格式读取文本文件
176 |
177 | 文件路径及文件名
178 |
179 |
180 |
181 |
182 | 以GB2312编码格式读取文本文件
183 |
184 | 文件路径及文件名
185 |
186 |
187 |
188 |
189 | 获得文件名,不包含扩展名
190 |
191 | 文件的绝对路径
192 |
193 |
194 |
195 | 将内容写入文本文件(如果文件path存在就打开,不存在就新建)
196 |
197 | 文件路径
198 | 要写入的内容
199 | 是否追加写入,true追加,flase覆盖 默认追加
200 |
201 |
202 |
203 | 将源文件的内容复制到目标文件中
204 |
205 | 源文件的绝对路径
206 | 目标文件的绝对路径
207 | 是否覆盖,true 覆盖,flase 不覆盖,默认true
208 |
209 |
210 |
211 | 清空文件内容
212 |
213 | 文件的绝对路径
214 |
215 |
216 |
217 | 写入数据流到文件
218 |
219 | 数据
220 | 目标文件
221 |
222 |
223 |
224 | 获取文件大小并以B,KB,GB,TB方式表示
225 |
226 | 文件(FileInfo类型)
227 |
228 |
229 |
230 |
231 | 获取文件大小并以B,KB,GB,TB方式表示
232 |
233 | 文件的具体路径
234 |
235 |
236 |
237 |
238 |
--------------------------------------------------------------------------------
/ToolBox.Object/ObjectConversion.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Text.RegularExpressions;
6 | using System.Threading.Tasks;
7 |
8 | namespace ToolBox.Object
9 | {
10 | public class ObjectConversion
11 | {
12 | ///
13 | /// 判断对象是否为Int32类型的数字
14 | ///
15 | ///
16 | ///
17 | public static bool IsNumeric(object expression)
18 | {
19 | if (expression != null)
20 | return IsNumeric(expression.ToString());
21 |
22 | return false;
23 |
24 | }
25 |
26 | ///
27 | /// 判断对象是否为Int32类型的数字
28 | ///
29 | ///
30 | ///
31 | public static bool IsNumeric(string expression)
32 | {
33 | if (expression != null)
34 | {
35 | string str = expression;
36 | if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*[.]?[0-9]*$"))
37 | {
38 | if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1'))
39 | return true;
40 | }
41 | }
42 | return false;
43 | }
44 |
45 | ///
46 | /// 是否为Double类型
47 | ///
48 | ///
49 | ///
50 | public static bool IsDouble(object expression)
51 | {
52 | if (expression != null)
53 | return Regex.IsMatch(expression.ToString(), @"^([0-9])[0-9]*(\.\w*)?$");
54 |
55 | return false;
56 | }
57 |
58 | ///
59 | /// 将字符串转换为数组
60 | ///
61 | /// 字符串
62 | /// 字符串数组
63 | public static string[] GetStrArray(string str)
64 | {
65 | return str.Split(new char[',']);
66 | }
67 |
68 | ///
69 | /// 将数组转换为字符串
70 | ///
71 | /// List
72 | /// 分隔符
73 | /// String
74 | public static string GetArrayStr(List list, string speater)
75 | {
76 | StringBuilder sb = new StringBuilder();
77 | for (int i = 0; i < list.Count; i++)
78 | {
79 | if (i == list.Count - 1)
80 | {
81 | sb.Append(list[i]);
82 | }
83 | else
84 | {
85 | sb.Append(list[i]);
86 | sb.Append(speater);
87 | }
88 | }
89 | return sb.ToString();
90 | }
91 |
92 | ///
93 | /// object型转换为bool型
94 | ///
95 | /// 要转换的字符串
96 | /// 缺省值
97 | /// 转换后的bool类型结果
98 | public static bool StrToBool(object expression, bool defValue)
99 | {
100 | if (expression != null)
101 | return StrToBool(expression, defValue);
102 |
103 | return defValue;
104 | }
105 |
106 | ///
107 | /// string型转换为bool型
108 | ///
109 | /// 要转换的字符串
110 | /// 缺省值
111 | /// 转换后的bool类型结果
112 | public static bool StrToBool(string expression, bool defValue)
113 | {
114 | if (expression != null)
115 | {
116 | if (string.Compare(expression, "true", true) == 0)
117 | return true;
118 | else if (string.Compare(expression, "false", true) == 0)
119 | return false;
120 | }
121 | return defValue;
122 | }
123 |
124 | ///
125 | /// 将对象转换为Int32类型
126 | ///
127 | /// 要转换的字符串
128 | /// 缺省值
129 | /// 转换后的int类型结果
130 | public static int ObjToInt(object expression, int defValue)
131 | {
132 | if (expression != null)
133 | return StrToInt(expression.ToString(), defValue);
134 |
135 | return defValue;
136 | }
137 |
138 | ///
139 | /// 将字符串转换为Int32类型
140 | ///
141 | /// 要转换的字符串
142 | /// 缺省值
143 | /// 转换后的int类型结果
144 | public static int StrToInt(string expression, int defValue)
145 | {
146 | if (string.IsNullOrEmpty(expression) || expression.Trim().Length >= 11 || !Regex.IsMatch(expression.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
147 | return defValue;
148 |
149 | int rv;
150 | if (Int32.TryParse(expression, out rv))
151 | return rv;
152 |
153 | return Convert.ToInt32(StrToFloat(expression, defValue));
154 | }
155 |
156 | ///
157 | /// Object型转换为decimal型
158 | ///
159 | /// 要转换的字符串
160 | /// 缺省值
161 | /// 转换后的decimal类型结果
162 | public static decimal ObjToDecimal(object expression, decimal defValue)
163 | {
164 | if (expression != null)
165 | return StrToDecimal(expression.ToString(), defValue);
166 |
167 | return defValue;
168 | }
169 |
170 | ///
171 | /// string型转换为decimal型
172 | ///
173 | /// 要转换的字符串
174 | /// 缺省值
175 | /// 转换后的decimal类型结果
176 | public static decimal StrToDecimal(string expression, decimal defValue)
177 | {
178 | if ((expression == null) || (expression.Length > 10))
179 | return defValue;
180 |
181 | decimal intValue = defValue;
182 | if (expression != null)
183 | {
184 | bool IsDecimal = Regex.IsMatch(expression, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
185 | if (IsDecimal)
186 | decimal.TryParse(expression, out intValue);
187 | }
188 | return intValue;
189 | }
190 |
191 | ///
192 | /// Object型转换为float型
193 | ///
194 | /// 要转换的字符串
195 | /// 缺省值
196 | /// 转换后的int类型结果
197 | public static float ObjToFloat(object expression, float defValue)
198 | {
199 | if (expression != null)
200 | return StrToFloat(expression.ToString(), defValue);
201 |
202 | return defValue;
203 | }
204 |
205 | ///
206 | /// string型转换为float型
207 | ///
208 | /// 要转换的字符串
209 | /// 缺省值
210 | /// 转换后的int类型结果
211 | public static float StrToFloat(string expression, float defValue)
212 | {
213 | if ((expression == null) || (expression.Length > 10))
214 | return defValue;
215 |
216 | float intValue = defValue;
217 | if (expression != null)
218 | {
219 | bool IsFloat = Regex.IsMatch(expression, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
220 | if (IsFloat)
221 | float.TryParse(expression, out intValue);
222 | }
223 | return intValue;
224 | }
225 |
226 | ///
227 | /// 将对象转换为日期时间类型
228 | ///
229 | /// 要转换的字符串
230 | /// 缺省值
231 | /// 转换后的int类型结果
232 | public static DateTime StrToDateTime(string str, DateTime defValue)
233 | {
234 | if (!string.IsNullOrEmpty(str))
235 | {
236 | DateTime dateTime;
237 | if (DateTime.TryParse(str, out dateTime))
238 | return dateTime;
239 | }
240 | return defValue;
241 | }
242 |
243 | ///
244 | /// 将对象转换为日期时间类型
245 | ///
246 | /// 要转换的字符串
247 | /// 转换后的int类型结果
248 | public static DateTime StrToDateTime(string str)
249 | {
250 | return StrToDateTime(str, DateTime.Now);
251 | }
252 |
253 | ///
254 | /// 将对象转换为日期时间类型
255 | ///
256 | /// 要转换的对象
257 | /// 转换后的int类型结果
258 | public static DateTime ObjectToDateTime(object obj)
259 | {
260 | return StrToDateTime(obj.ToString());
261 | }
262 |
263 | ///
264 | /// 将对象转换为日期时间类型
265 | ///
266 | /// 要转换的对象
267 | /// 缺省值
268 | /// 转换后的int类型结果
269 | public static DateTime ObjectToDateTime(object obj, DateTime defValue)
270 | {
271 | return StrToDateTime(obj.ToString(), defValue);
272 | }
273 |
274 | ///
275 | /// 将对象转换为字符串 如果为null 则返回空
276 | ///
277 | /// 要转换的对象
278 | /// 转换后的string类型结果
279 | public static string ObjectToStr(object obj)
280 | {
281 | if (obj == null)
282 | return "";
283 | return obj.ToString().Trim();
284 | }
285 |
286 |
287 | }
288 | }
289 |
--------------------------------------------------------------------------------