1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
namespace Misc
{
//字符串转换
public class StringConverter
{
private const string blank = @"\s*";
private const string num = @"\d+";
public static Size ToSize(string s)
{
string RegexText = @"\{" + blank + "Width" + blank + "=" + blank + num + blank + "," + blank +
"Height" + blank + "=" + blank + num + blank + @"\}";
Regex regex = new Regex(RegexText);
Match match = regex.Match(s);
if (match.Success)
{
s = match.Value;
regex = new Regex(num);
MatchCollection matches = regex.Matches(s);
string[] ss = new string[matches.Count];
for (int i = 0; i < ss.Length; i++)
{
ss[i] = matches[i].Value;
}
Size size = new Size(int.Parse(ss[0]), int.Parse(ss[1]));
return size;
}
else
{
return Size.Empty;
}
}
public static Rectangle ToRectangle(string s)
{
string RegexText = @"\{" +
blank + "X" + blank + "=" + blank + num + blank + "," +
blank + "Y" + blank + "=" + blank + num + blank + "," +
blank + "Width" + blank + "=" + blank + num + blank + "," +
blank + "Height" + blank + "=" + blank + num + blank + @"\}";
Regex regex = new Regex(RegexText);
Match match = regex.Match(s);
if (match.Success)
{
s = match.Value;
regex = new Regex(num);
MatchCollection matches = regex.Matches(s);
string[] ss = new string[matches.Count];
for (int i = 0; i < ss.Length; i++)
{
ss[i] = matches[i].Value;
}
Rectangle rect = new Rectangle(
int.Parse(ss[0]),
int.Parse(ss[1]),
int.Parse(ss[2]),
int.Parse(ss[3]));
return rect;
}
else
{
return Rectangle.Empty;
}
}
// 摘要:
// 将主机名或 IP 地址解析为 IPEndPoint 实例。
public static IPEndPoint ToIPEndPoint(string hostNameOrAddress)
{
string[] s = hostNameOrAddress.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
if (s.Length == 1)
{
IPAddress ip;
int port = 20006;
try
{
ip = IPAddress.Parse(s[0]);
}
catch
{
try
{
IPAddress[] ips = System.Net.Dns.GetHostEntry(s[0]).AddressList;
if (ips.Length == 1)
ip = ips[0];
else
ip = IPAddress.Parse("127.0.0.1");
}
catch
{
return null;
}
}
return new IPEndPoint(ip, port);
}
else if (s.Length == 2)
{
IPAddress ip;
int port;
try
{
port = int.Parse(s[1]);
}
catch
{
return null;
}
try
{
ip = IPAddress.Parse(s[0]);
}
catch
{
try {
IPAddress[] ips = System.Net.Dns.GetHostEntry(s[0]).AddressList;
if (ips.Length == 1)
ip = ips[0];
else
ip = IPAddress.Parse("127.0.0.1");
}
catch{
return null;
}
}
return new IPEndPoint(ip, port);
}
return null;
}
public static string IPEndPoint2DirectoryName(IPEndPoint ipep)
{
return ipep.Address.ToString() + "_" + ipep.Port.ToString();
}
}
}