Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
how can i save a point[] to file for next using
Posted

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.
 
Share this answer
 
v3
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:
C#
PointConverter con = new PointConverter();
string str = con.ConvertToString(new Point(30, 40));
Point p = (Point)con.ConvertFromString(str);
MessageBox.Show(p.ToString());
 
Share this answer
 
Google for 'serialize c#' or 'serialize array c#' and you'll find plenty of examples...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900