Why wen i click on Save button twice in a row its adding new text to the file and dosent update the

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have in Form1 a click event for saving:

<pre class="prettyprint private void saveBaseFileToolStripMenuItem_Click(object sender, EventArgs e)
{
string path_exe = Path.GetDirectoryName(Application.LocalUserAppDataPath);
wireObject1.Save(path_exe,Lock,pictureBox1);
}[/code]
Now in WireObject class i have a Save function:

<pre class="prettyprint public void Save(string path , bool Locked , PictureBox pb)
{
string fn;

fn = path + "\" + "DATABASE" + "\" + wo_name + "\" + wo_name + ".txt";
OptionsFile setting_file = new OptionsFile(fn);
setting_file.SetKey("File Name", fn);
setting_file.SetKey("Version", version);
setting_file.SetKey("picturebox.Width", pb.Width.ToString());
setting_file.SetKey("picturebox.Height", pb.Height.ToString());
setting_file.SetKey("Button Lock", Locked.ToString());
setting_file.SetListFloatKey("Coordinates_X", woc.Point_X);
setting_file.SetListFloatKey("Coordinates_Y", woc.Point_Y);
setting_file.SetListIntKey("ConnectionStart", connectionStart);
setting_file.SetListIntKey("ConnectionEnd", connectionEnd);
}[/code]

This is the WireObject class constructor:
<pre class="prettyprint class WireObject
{
public string lockObject;
private int idx;
public WireObjectCoordinates woc;
private List<int> connectionStart = new List<int>();
private List<int> connectionEnd = new List<int>();

private const string version = "01.00";

string wo_name;


public WireObject( string name )
{
wo_name = name;
woc = new WireObjectCoordinates();
}[/code]
Once in Form1 i enter any textBox then click a button its making an instance of the WireObject class with the text from the textBox:

<pre class="prettyprint wireObject1 = new WireObject(textBox3.Text);[/code]
For example i entered the textBox3 a name like Human so now the variable in WireObject name will be Human
Then im doing an instance for WireObjectCoordinates class wich just make an instance for some Lists:

WireObjectCoordinates have two constructors in this case im using the first one :

<pre class="prettyprint using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AnimationEditor
{
class WireObjectCoordinates
{
public List<float> Point_X = new List<float>();
public List<float> Point_Y = new List<float>();

public WireObjectCoordinates()
{
}

public WireObjectCoordinates(WireObjectCoordinates w)
{
Point_X.AddRange(w.Point_X);
Point_Y.AddRange(w.Point_Y);
}[/code]
Then im doing a Save clicked the button WireObject class got a name and now after clicked the button and when its got a name im doing the Save:
This is the Save function in WireObject class:

<pre class="prettyprint public void Save(string path , bool Locked , PictureBox pb)
{
string fn;

fn = path + "\" + "DATABASE" + "\" + wo_name + "\" + wo_name + ".txt";
OptionsFile setting_file = new OptionsFile(fn);
setting_file.SetKey("File Name", fn);
setting_file.SetKey("Version", version);
setting_file.SetKey("picturebox.Width", pb.Width.ToString());
setting_file.SetKey("picturebox.Height", pb.Height.ToString());
setting_file.SetKey("Button Lock", Locked.ToString());
setting_file.SetListFloatKey("Coordinates_X", woc.Point_X);
setting_file.SetListFloatKey("Coordinates_Y", woc.Point_Y);
setting_file.SetListIntKey("ConnectionStart", connectionStart);
setting_file.SetListIntKey("ConnectionEnd", connectionEnd);
}[/code]
<br/>



First time i click and save its creating a new file text file this the content:

<pre class="prettyprint File Name = C:UsersChocoladeAppDataLocalAnimationEditorAnimationEditorDATABASEhelphelp.txt
Version = 01.00
picturebox.Width = 656
picturebox.Height = 422
Button Lock = False
Coordinates_X = 332,331,275,328,318,335,329,323,308,318,331,340,309
Coordinates_Y = 208,209,225,208,203,212,210,207,244,206,203,274,299[/code]
Now im doing some changes i didnt wuit the program just added more points for example or even didnt change anything but lets say i added some points.
Now i click and make save again.
Now the content of the text file is like that:

<pre class="prettyprint File Name = C:UsersChocoladeAppDataLocalAnimationEditorAnimationEditorDATABASEhelphelp.txt
Version = 01.00
picturebox.Width = 656
picturebox.Height = 422
Button Lock = False
Coordinates_X = 332,331,275,328,318,335,329,323,308,318,331,340,309
Coordinates_Y = 208,209,225,208,203,212,210,207,244,206,203,274,299
File Name = C:UsersChocoladeAppDataLocalAnimationEditorAnimationEditorDATABASEhelphelp.txt
Version = 01.00
picturebox.Width = 656
picturebox.Height = 422
Button Lock = True
ConnectionStart = 2
ConnectionEnd = 8
Coordinates_X=332,331,275,328,318,335,329,323,308,318,331,340,309,322,317,325,319,333,334
Coordinates_Y=208,209,225,208,203,212,210,207,244,206,203,274,299,210,208,213,210,207,219[/code]
Its adding everything again including the coordinates of the new points.
But what it should do is just updating the value/s of the keys i changed. If i added more points so the file suppose to look like this:


<pre class="prettyprint File Name = C:UsersChocoladeAppDataLocalAnimationEditorAnimationEditorDATABASEhelphelp.txt
Version = 01.00
picturebox.Width = 656
picturebox.Height = 422
Button Lock = False
Coordinates_X=332,331,275,328,318,335,329,323,308,318,331,340,309,322,317,325,319,333,334
Coordinates_Y=208,209,225,208,203,212,210,207,244,206,203,274,299,210,208,213,210,207,219[/code]
Im not sure where the problem is.

This is the code of the OptionsFile where im creating the setting_file and also Get and Set the key and values:

<pre class="prettyprint /*----------------------------------------------------------------
* Module Name : OptionsFile
* Description : Saves and retrievs application options
* Author : Danny
* Date : 10/02/2010
* Revision : 1.00
* --------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Configuration;


/*
* Introduction :
*
* This module helps in saving application options
*
*
* Typical file could look like this:
* user_color=Red
* time_left=30
*
*
*
*
*
* */

namespace DannyGeneral
{
class OptionsFile
{
/*----------------------------------------
* P R I V A T E V A R I A B L E S
* ---------------------------------------*/


/*---------------------------------
* P U B L I C M E T H O D S
* -------------------------------*/
string path_exe;
string temp_settings_file;
string temp_settings_dir;
string Options_File;
StreamWriter sw;
StreamReader sr;

/*----------------------------------------------------------
* Function : OptionsFile
* Description : Constructor
* Parameters : file_name is the name of the file to use
* Return : none
* --------------------------------------------------------*/
public OptionsFile(string settings)
{
if (!File.Exists(settings))
{
if (!Directory.Exists(Path.GetDirectoryName(settings)))
{
Directory.CreateDirectory(Path.GetDirectoryName(settings));
}
File.Create(settings).Close();
}
path_exe = Path.GetDirectoryName(Application.LocalUserAppDataPath);
Options_File = settings;
}

/*----------------------------------------------------------
* Function : GetKey
* Description : gets the value of the key.
* Parameters : key
* Return : value of the key if key exist, null if not exist
* --------------------------------------------------------*/
public string GetKey(string key)
{

// string value_of_each_key;
string key_of_each_line;
string line;
int index;
string key_value;
key_value = null;

sr = new StreamReader(Options_File);
while (null != (line = sr.ReadLine()))
{


index = line.IndexOf("=");


// value_of_each_key = line.Substring(index+1);



if (index >= 1)
{
key_of_each_line = line.Substring(0, index);
if (key_of_each_line == key)
{
key_value = line.Substring(key.Length + 1);
}

}
else
{
}


}
sr.Close();
return key_value;
}


/*----------------------------------------------------------
* Function : SetKey
* Description : sets a value to the specified key
* Parameters : key and a value
* Return : none
* --------------------------------------------------------*/
public void SetKey(string key , string value)
{
bool key_was_found_inside_the_loop;
string value_of_each_key;
string key_of_each_line ;
string line;
int index;
key_was_found_inside_the_loop = false;

temp_settings_file = "\temp_settings_file.txt";
temp_settings_dir = path_exe + @"temp_settings";
if (!Directory.Exists(temp_settings_dir))
{
Directory.CreateDirectory(temp_settings_dir);
}

sw = new StreamWriter(temp_settings_dir+temp_settings_file);
sr = new StreamReader(Options_File);
while (null != (line = sr.ReadLine()))
{

index = line.IndexOf("=");
key_of_each_line = line.Substring(0, index);
value_of_each_key = line.Substring( index + 1);
// key_value = line.Substring(0,value.Length);
if (key_of_each_line == key)
{
sw.WriteLine(key + " = " + value);
key_was_found_inside_the_loop = true;

}
else
{
sw.WriteLine(key_of_each_line+"="+value_of_each_key);
}

}

if (!key_was_found_inside_the_loop)
{
sw.WriteLine(key + "=" + value);
}
sr.Close();
sw.Close();
File.Delete(Options_File);
File.Move(temp_settings_dir + temp_settings_file, Options_File);
return;

}



public List<float> GetListFloatKey(string keys)
{
List<float> result = new List<float>();
string s = GetKey(keys);
if (s != null)
{
string[] items = s.Split(new char[] { , });
float f;
foreach (string item in items)
{
if (float.TryParse(item, out f))
result.Add(f);
}
return result;
}
else
{
return result;
}
}


public void SetListFloatKey(string key, List<float> Values)
{
StringBuilder sb = new StringBuilder();
foreach (float value in Values)
{
sb.AppendFormat(",{0}", value);
}
if (Values.Count == 0)
{
}
else
{
SetKey(key, sb.ToString().Substring(1));
}
}




public List<int> GetListIntKey(string keys)
{
List<int> t = new List<int>();
t = (GetListFloatKey(keys).ConvertAll(x => (int)x));
return t;
;

}

public void SetListIntKey(string key, List<int> Values)
{
List<float> t = new List<float>();
for (int i = 0; i < Values.Count(); i++)
{
t.Add(Values);
}
SetListFloatKey(key, t);
}
/*---------------------------------
* P R I V A T E M E T H O D S
* -------------------------------*/

}





}
[/code]

In other program i didnt have any problems like that when using the OptionsFile but then i didnt have the other functions GetListFloatKey and SetListFloatKey i had only GetKey and SetKey.

And yet im not sure where the problem is.






<hr class="sig danieli

View the full article
 
Back
Top