How can i add a List Type of int to the options file i have and use format string?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have two functions GetKey and SetKey and i added after this two functions another two functions GetListKey and SetListKey both type of List string.<br/>
<br/>
Now after that i want to add another two functions of Lists type of int GetListIntKey and SetListIntKey but i have a problem since GetKey and SetKey are type of string.
<br/>
<br/>
<br/>
Then what i need to do is when im getting the list in the SetListIntKey i want it to be written in my Options_DB file formmated to string like this:<br/>
<br/>
122,231 233,244 and so on what the list im getting is coordinates of points.<br/>
So i want to write it formatted string to the options_db file and with the GetKey to read the formatted string back so i will have a list with the coordinates when read it back.<br/>
<br/>
<br/>
This is the Options File code:<br/>
<br/>

<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))
{

}
else
{
FileStream fs=File.Create(settings);

fs.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<String> GetListKey(string Key)
{
string j;
List<String> t;
t=new List<String>();
int i;
for (i = 0; ; i++)
{
j=GetKey(Key + i.ToString());
if (j == null)
{
break;
}
else
{
t.Add(j);
}
}
if (t.Count == 0)
return null;
else
return t;
}

public void SetListKey(string Key, List<String> Value)
{
int i;
for (i = 0; i < Value.Count; i++)
{
string indexed_key;
indexed_key = string.Format("{0}{1}", Key, i);
// indexed_key = Key + i.ToString();
SetKey(indexed_key, Value);
}
}

public List<int> GetListIntKey(int keys)
{
int j;
List<int> t;
t = new List<int>();
int i;
for (i = 0; ; i++)
{
j = GetKey(keys + i);
if (j == null)
{
break;
}
else
{
t.Add(j);
}
}
if (t.Count == 0)
return null;
else
return t;
}

public void SetListIntKey(int keys, List<int> Values)
{
int i;
for (i = 0; i < Values.Count; i++)
{
string indexed_key;
indexed_key = string.Format("{0}{1}", keys, i);
// indexed_key = Key + i.ToString();
SetKey(indexed_key, Values.ToString());
}
}
/*---------------------------------
* P R I V A T E M E T H O D S
* -------------------------------*/

}





}
[/code]
<br/>
<br/>
<br/>
And this is an example of the Options_DB ( Data Base ) file as im using it today:<br/>
<br/>

<pre class="prettyprint using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DannyGeneral;
using System.IO;
using System.Windows.Forms;

namespace mws
{

static class Options_DB
{
static decimal radarImagesDec;
static decimal satelliteImagesDec;
static string selectedpath;
static string selected_section;
static DateTime emailSentTime;
static string emailTime;
static decimal num1;
static string num1_numricupdown;
static bool begin_download_on_start;
public static List<string> temp = new List<string>();
static string images_full_screen_size_directory;
static List<String> Radar_Sites;
static int voices_volume;
static string remote_image_on_server;
static string satellite_dir;
static string sf;
static string radar_set_time;
static string satellite_set_time;
static string settings_dir;
static string settings_file;
static OptionsFile setting_file;
static bool voices_on_off;
static string path_settings;
static string path_exe;
static string temp_radar_download_directory;
static string temp_satellite_download_directory;

static Options_DB()
{
// --- O P E N N I N G S E T T I N G F I L E
//t = voices_volume_trackbar.ToString();
path_exe = Path.GetDirectoryName(Application.LocalUserAppDataPath);
temp_radar_download_directory = path_exe+"\radar_temp_directory";
temp_satellite_download_directory = path_exe+"\satellite_temp_directory";
if (Directory.Exists(temp_radar_download_directory))
{
}
else
{
Directory.CreateDirectory(temp_radar_download_directory);
}
if (Directory.Exists(temp_satellite_download_directory))
{
}
else
{
Directory.CreateDirectory(temp_satellite_download_directory);
}
path_settings = Path.GetDirectoryName(Application.LocalUserAppDataPath);
settings_file = "\settings.txt";
settings_dir = path_settings + @"settings";
setting_file = new OptionsFile(settings_dir + settings_file);
// ---- L O A D I N G A L L K E Y S
string val;
val = setting_file.GetKey("Voices");
if (val == null)
{
voices_on_off = false; // SETTING DEFAULT!!!!
}
else
{
bool b;
bool.TryParse(val.Trim(), out b);
voices_on_off = b;
}

//*** All functions Get and Set ***\

public static bool Get_Voices_On_Off()
{
return voices_on_off;
}

public static void Set_Voices_On_Off(bool voices)
{
voices_on_off = voices;
setting_file.SetKey("Voices", voices_on_off.ToString());
}[/code]
<br/>
<br/>
<br/>
In the Options_Db i should see the formmated list int.<br/>
So if in my program in runtime code i will make SetListIntKey(Points) i will see the list of Points coordinates formmated the key to the left and the formmated coordinates on the right like:<br/>
<br/>
Points_Key = 122,233 44,122 121,33 and so on.<br/>
And when i make GetListIntKey in the runtime code so for example:<br/>
<br/>
List<int> test = GetListIntKey()<br/>
So in the test List i will see in index 0 122,233 and ini ndex 1 44,122<br/>
In fact i have two Lists for the points coordinates Point_X and Point_Y so maybe in the SetListIntKey i need to use two Lists and combine them ?<br/>
<br/>
<br/><hr class="sig danieli

View the full article
 
Back
Top