Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8193967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:39:35+00:00 2026-06-07T04:39:35+00:00

I have in the options file two functions GetKey and SetKey. I set a

  • 0

I have in the options file two functions GetKey and SetKey.

I set a key then in the settings_file.txt it will look like:

text = hello where text is the key then = and hello is the value for the current key.

Now i need to add another two functions the first one is type of List that get a string and return a List

And a another function that get a Key and a List.

So this is the first two functions allready working GetKey and SetKey:

/*----------------------------------------------------------------
 * 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;

    }

After this two functions i did:

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

    public void SetListFloatKey(string key, List<float> Values)
    {
        int i;
        for (i = 0; i < Values.Count; i++)
        {
            string indexed_key;
            indexed_key = string.Format("{0}{1}", key, i);
            //  indexed_key = Key + i.ToString();
            SetKey(indexed_key, Values[i].ToString());
        }
    }

But they are not good.

The last one the SetListFloatKey when i put a List in it the result in the text file settings_file.txt is for exmaple:

coordinates01 = 123

coordinates02 = 144

coordinates03 = 145

For every cell/index in the List i get its making a key. What i need is that the List i get will have one key the format in the text file should be like this:

coordinates = 123,144,145……and so on one key and then all the values from the List i get.

Then in the GetListFloatKey i need re format the values according to the key for example coordinates and return a List with the values in index 0 123 in 1 144 in 2 145 and so on….

The qustion if the function the way im doing them are good in the way im using in both GetKey and SetKey ? And how do i format and re format the values ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:39:38+00:00Added an answer on June 7, 2026 at 4:39 am

    At the moment you are calling SetKey within SetListFloatKey for every item in the list. Instead, you need to build a string and call it once, along the lines of (basic testing done):

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

    Note I am getting lazy here – the last item will have a comma after it. Then when loading the list:

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

    However, given you are reading and writing an options file, you might want to investigate options around serializing your objects to and from files.

    EDIT There are a few ways you can get rid of the extra comma. One way is to not put it in in the first place…

      string sep = "";
      foreach (float value in Values)
      {
        sb.AppendFormat("{0}{1}", sep, value);
        if (sep == "") sep = ",";
      }
    

    …and another is to exclude it in the call to SetKey…

      foreach (float value in Values)
      {
        sb.AppendFormat(",{0}", value);
      }
      SetKey(key, sb.ToString().Substring(1));
    

    ..note that in both of these cases I moved the comma to the start to make life easier. Alternatively, you could store the numbers in an array and use Array.Join.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to parse a large XML file and I have two options: Perl
I have a file that essentially a list of XPaths like so: /Options/File[1]/Settings[1]/Type[1] /Options/File[1]/Settings[1]/Path[1]
Lets say i have two inline functions in my header file: inline int foo()
I have an options_menu.xml file like this: <?xml version=1.0 encoding=utf-8?> <menu xmlns:android=http://schemas.android.com/apk/res/android> <item android:id=@+id/search
Im including the popular file jquery.form.js. I have the following: var options = {
I have writen options to a <select> using something like Id.innerHTML = <option value='foo'>Foo</option>;
I have an options object in my CS class, and I'd like to keep
I have added options in application.rb: config.autoload_paths += %W(#{config.root}/lib) config.autoload_paths += Dir[#{config.root}/lib/**/] and lib\functions.rb:
I have a select menu with two options that are settings for my website.
i have two php files where one is executed through url and that file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.