0

This is the code I have in a new class:

public static void Save(List<Point> points, int imageWidth, int imageHeight, double originalFactor, double currentFactor)
{
   string appDirectory = Path.GetDirectoryName(Application.LocalUserAppDataPath);
   string textFile = "LightningsDistance.txt";
   string path = "LightningsDistance";
   string lightningsDistancePath = Path.Combine(appDirectory,path);
   string file = Path.Combine(lightningsDistancePath,textFile);
   if (!Directory.Exists(lightningsDistancePath))
   {
      Directory.CreateDirectory(lightningsDistancePath);
   }
   if (!File.Exists(file))
   {
      File.Create(file);
   }
}

The format of the text file should be something like:

Image Width:             Image Height:        

Original Factor:         Current Factor:

Points Coordinates:

In the Points Coordinates it should be like this:

Coordinate 1: X = 21, Y = 25
Coordinate 2: X = 210, Y = 344

And so on. The List points is like this for example in index 0 I see: X = 21 Y = 35

In Form1 I have a click button where I call this save method :

private void button2_Click(object sender, EventArgs e)
{
   CloudEnteringAlert.Save(picturebox1PointsCoordinates, img.Width, img.Height, factor, currentfactor);
}

I want that when I click the button it will write to the text file and if I clicked once and draw more points and clicked again on the button so it will add (append) the new information to the text file on the same format as above and not to overwrite on the exist file.

Then I have another click button that call a Load method. The Load method is not getting so far anything. Just in the new class:

public static void Load()
{

}

I need that when i click on Load on any time it will read the information from the text file i saved and will assign for now only the Points coordinates back to the List points.

Zong
  • 6,160
  • 5
  • 32
  • 46
user3072062
  • 93
  • 1
  • 9
  • 1
    Have you looked at [writing to a file](http://stackoverflow.com/questions/8854984/write-text-file-c-sharp-streamwriter)? Maybe [serializing/deserializing](http://stackoverflow.com/questions/6115721/how-to-save-restore-serializable-object-to-from-file)? – Zong Dec 09 '13 at 18:14
  • And what's your question? – Pierre-Luc Pineault Dec 09 '13 at 18:20
  • Check stream reader and stream writter clases thats your answer. – bto.rdz Dec 09 '13 at 18:22
  • it is common to write data not forms to sequential files. It is more common to write the data as: 100,200,50,40 etc. Writing to structured format and reading that back across more than one record will require extra code. – NoChance Dec 09 '13 at 18:44

1 Answers1

1

File.AppendAllText(--) is all you need for this purpose.Its syntax is similar to;

File.AppendAllText(string path,string contents,Encoding encoding);

So all you need is a call to this function with appropriate parameters,like this;

File.AppendAllText(textFile,"Something to write...");

If you run this statement 5 times with a proper path the resulting file would contain five lines of text with each line having "Something to write..."

Hope that was the solution to your problem.

devavx
  • 1,035
  • 9
  • 22