保存点数组 [英] save point array
本文介绍了保存点数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用
推荐答案
private static void SavePointArray(string FileName, Point[] PointArray)
{
StreamWriter writer = File.CreateText(FileName);
foreach (Point point in PointArray)
writer.WriteLine(point.ToString());
writer.Dispose();
}
private static readonly char[] RemoveChars = new char[] { ''{'', ''}'', ''='', ''X'', ''Y'', '','' };
private static Point[] LoadPointArray(string FileName)
{
StreamReader reader = File.OpenText(FileName);
List<Point> result = new List<Point>();
string line;
string[] chunks;
int x, y;
while (!reader.EndOfStream)
{
line = reader.ReadLine();
chunks = line.Split(RemoveChars, StringSplitOptions.RemoveEmptyEntries);
int.TryParse(chunks[0], out x);
int.TryParse(chunks[1], out y);
result.Add(new Point(x, y));
}
reader.Dispose();
return result.ToArray();
}
将点[]保存到文件中以供下一步使用
您还应该进行一些检查,以确保一切正常.
You should also do some checking to make sure everything is ok.
要添加到上一个答案,您可以使用PointConverter对System.Drawing.Point进行序列化和反序列化(也称为解析).班级.这比上一个答案中介绍的方法更干净.例如:
To add to the previous answer, you can serialize and deserialize (aka, parse) a System.Drawing.Point with the PointConverter class. This is cleaner than the method presented in the previous answer. For example:
PointConverter con = new PointConverter();
string str = con.ConvertToString(new Point(30, 40));
Point p = (Point)con.ConvertFromString(str);
MessageBox.Show(p.ToString());
Google的序列化c#"或序列化数组c#",您会发现很多例子...
Google for ''serialize c#'' or ''serialize array c#'' and you''ll find plenty of examples...
这篇关于保存点数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文