Saving and Restoring Application Settings

Hi,

Does someone has an example for me:
How to read and write application-settings to ini-file.

Greetings,

Peter Kiers
LVL 1
peterkiersAsked:
Who is Participating?
 
Carl TawnSystems and Integration DeveloperCommented:
That would be:

string settingsFile = string.Concat(System.IO.Path.GetFileNameWithoutExtension(Application.ExecutablePath), ".ini");

Open in new window

0
 
Carl TawnSystems and Integration DeveloperCommented:
Do you specifically want to use an ini file, or do you just want to store settings? If it's the latter then you can use the built-in settings feature:

     http://msdn.microsoft.com/en-us/library/aa730869(VS.80).aspx
0
 
peterkiersAuthor Commented:
Use a ini file
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
peterkiersAuthor Commented:
This is a delphi line:

SettingsFile := ChangeFileExt(Application.ExeName, '.ini');

can someone translate this to C#

Peter
0
 
peterkiersAuthor Commented:
Oke, now how to read and write to ini-file

Greetings,

Peter
0
 
mkobrinCommented:
I use a third party component called AMS Profile for my config settings. Very easy to use.

http://www.codeproject.com/KB/cs/readwritexmlini.aspx
0
 
peterkiersAuthor Commented:
I don't want to use third party components.

P
0
 
Carl TawnSystems and Integration DeveloperCommented:
Assuming you are storing them as name/value pairs, you can load all of your settings into a Dictionary:

Dictionary<string, string> configSettings = new Dictionary<string, string>();

string[] lines = File.ReadAllLines("Your_Config_File");
foreach (string line in lines)
{
       string[] arr = line.Split(new char[] { '=' });
       configSettings.Add(arr[0], arr[1]);
}

Open in new window

0
 
Éric MoreauSenior .Net ConsultantCommented:
0
 
peterkiersAuthor Commented:
Do you have an example in C#?

P
0
 
peterkiersAuthor Commented:
Oke, Can someone tell me how to declare this:

[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string strSection, string strKeys,string myVal,string theFile);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string strSection, string strKeys, string myDef, StringBuilder theReturnedValue, int varSize, string theFile);
0
 
peterkiersAuthor Commented:
I have this but I get an error:

The type or namespace name 'DllImport' could not be found (are you missing a using directive or an assembly reference?)      

P.
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;

namespace iniFile
{
    public partial class Form1 : Form
    {

    public string path;

    [DllImport("kernel32")]
    private static extern long WritePrivateProfileString(string section,
      string key,string val,string filePath);

    [DllImport("kernel32")]
    private static extern int GetPrivateProfileString(string section,
      string key,string def, StringBuilder retVal,
      int size,string filePath);


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string settingsFile = string.Concat(System.IO.Path.GetFileNameWithoutExtension(Application.ExecutablePath), ".ini");
         //   LoadSettings();
        }


    }
}

Open in new window

0
 
Éric MoreauSenior .Net ConsultantCommented:
0
 
peterkiersAuthor Commented:
Yeh, I have allready found that too. Could you please look at my last post
with the declaration of the two methods which are necessary to access an INI File.
0
 
peterkiersAuthor Commented:
I quess nobody knows the answer to my problem.
0
 
peterkiersAuthor Commented:
This was the answer: using System.Runtime.InteropServices;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.