hi,
I want to access ini file in my application , which asked for the centre ID b4 login & starts, it asked this only for one tym when it start at first tym & never asked this question again.
The second reason to use ini file is , i want to pass a string of database connection from the ini file to my connection.dll file, bcz the connections details can be change in future, so i cnt hard coded it in my application.
Can any one tell me how to use this from ini file.
actually i m new to these file concepts , so i hv no idea for that. so pls provide me some steps or code 4 that.
Thanx in advance

Recommended Answers

All 7 Replies

You could also look into settings to store application data.

Use "custom-made" C# class for accessing ini files, with functions similar to GetPrivateProfileString and WritePrivateProfileString in C++. if you want i will sent you code.

I am using simple txt file to read connection string, and parse string later in application.

Can you clarify something please; is an INI file format required or just the way you assumed it would be done? ie, do the INI files already exist and you have to implement them, or will you be creating the files specifically for your application?

If its the later then i would definitely recommend using the built in Settings i linked to above. When you create tableadapters, Visual Studio stores the connection string there by default.

Whilst rolling your own solution can allow a higher level of customisation and control, i really think the built in system does what you need :)

hello milosz, pls. send me the code.
thanks a lot

You're shooting yourself in the foot if you want to use ini files. Traditionally those files are stored in the same directory as the application and with windows Vista and later versions of windows you have something called virtualized folders. That means when you write to C:\Program Files\YourApp\YourFile.ini the write will succeed but it really wraps the file to C:\Documents and Settings\...\. When you go to access it you will see the same thing. When another user logs in then the .ini file won't be there.

Don't use .ini files.

hello milosz, pls. send me the code.
thanks a lot

create IniFile.cs and copy/paste code below. Change namespace.

using System;
using System.Collections.Generic;
//using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace YourNameSpace
{
	class IniFile
	{
		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 IniFile(string INIPath)
		{
			path = INIPath;			
		}

		public long IniWriteValue(string Section, string Key, string Value)
		{
			long ret = WritePrivateProfileString(Section, Key, Value, this.path);			

			return ret;
		}

		public string IniReadValue(string Section, string Key)
		{
			StringBuilder temp = new StringBuilder(255);
			int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
			return temp.ToString();
		}
	}
}

Use ini class in your code like this:

IniFile ini = new IniFile(INIfile);

string tmp = ini.IniReadValue("SectionName", "KeyName");

INIfile could be name of ini file if ini is located in same directory as your c# application(relative path) or absolute path.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.