?
Solved

Saving and Restoring Application Settings

Posted on 2010-11-17
16
Medium Priority
?
304 Views
Last Modified: 2013-12-17
Hi,

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

Greetings,

Peter Kiers
0
Comment
Question by:peterkiers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 3
  • 2
  • +1
16 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34153605
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
 
LVL 1

Author Comment

by:peterkiers
ID: 34153634
Use a ini file
0
 
LVL 1

Author Comment

by:peterkiers
ID: 34153707
This is a delphi line:

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

can someone translate this to C#

Peter
0
How to Create Failover DNS Record Sets in Route 53

Route 53 has the ability to easily configure DNS record sets specifically for failover scenarios. These failover record sets can be configured to failover to full-blown deployments in other regions or to a static HTML page that informs your customers of the issue.

 
LVL 52

Accepted Solution

by:
Carl Tawn earned 1600 total points
ID: 34153733
That would be:

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

Open in new window

0
 
LVL 1

Author Comment

by:peterkiers
ID: 34153752
Oke, now how to read and write to ini-file

Greetings,

Peter
0
 
LVL 7

Assisted Solution

by:mkobrin
mkobrin earned 200 total points
ID: 34153763
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
 
LVL 1

Author Comment

by:peterkiers
ID: 34153781
I don't want to use third party components.

P
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34153812
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
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 200 total points
ID: 34153824
0
 
LVL 1

Author Comment

by:peterkiers
ID: 34153838
Do you have an example in C#?

P
0
 
LVL 1

Author Comment

by:peterkiers
ID: 34153854
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
 
LVL 1

Author Comment

by:peterkiers
ID: 34153915
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
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 34153930
0
 
LVL 1

Author Comment

by:peterkiers
ID: 34153948
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
 
LVL 1

Author Comment

by:peterkiers
ID: 34154058
I quess nobody knows the answer to my problem.
0
 
LVL 1

Author Comment

by:peterkiers
ID: 34154072
This was the answer: using System.Runtime.InteropServices;
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question