944,149 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 1862
  • C# RSS
Nov 6th, 2009
0

using INI(setting) file in c#

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
kool.net is offline Offline
28 posts
since Oct 2009
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Nov 6th, 2009
0
Re: using INI(setting) file in c#
You could also look into settings to store application data.
Reputation Points: 512
Solved Threads: 246
Nearly a Posting Virtuoso
Ryshad is offline Offline
1,260 posts
since Aug 2009
Nov 6th, 2009
0
Re: using INI(setting) file in c#
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
milosz is offline Offline
11 posts
since Oct 2009
Nov 6th, 2009
0
Re: using INI(setting) file in c#
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
Reputation Points: 512
Solved Threads: 246
Nearly a Posting Virtuoso
Ryshad is offline Offline
1,260 posts
since Aug 2009
Nov 7th, 2009
0
Re: using INI(setting) file in c#
hello milosz, pls. send me the code.
thanks a lot
Reputation Points: 10
Solved Threads: 0
Light Poster
kool.net is offline Offline
28 posts
since Oct 2009
Nov 7th, 2009
0
Re: using INI(setting) file in c#
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.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Nov 7th, 2009
0
Re: using INI(setting) file in c#
Click to Expand / Collapse  Quote originally posted by kool.net ...
hello milosz, pls. send me the code.
thanks a lot
create IniFile.cs and copy/paste code below. Change namespace.
C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. //using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6.  
  7. namespace YourNameSpace
  8. {
  9. class IniFile
  10. {
  11. public string path;
  12.  
  13. [DllImport("kernel32")]
  14. private static extern long WritePrivateProfileString(string section,
  15. string key, string val, string filePath);
  16.  
  17. [DllImport("kernel32")]
  18. private static extern int GetPrivateProfileString(string section,
  19. string key, string def, StringBuilder retVal,
  20. int size, string filePath);
  21.  
  22. public IniFile(string INIPath)
  23. {
  24. path = INIPath;
  25. }
  26.  
  27. public long IniWriteValue(string Section, string Key, string Value)
  28. {
  29. long ret = WritePrivateProfileString(Section, Key, Value, this.path);
  30.  
  31. return ret;
  32. }
  33.  
  34. public string IniReadValue(string Section, string Key)
  35. {
  36. StringBuilder temp = new StringBuilder(255);
  37. int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
  38. return temp.ToString();
  39. }
  40. }
  41. }

Use ini class in your code like this:
C# Syntax (Toggle Plain Text)
  1. IniFile ini = new IniFile(INIfile);
  2.  
  3. 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
milosz is offline Offline
11 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: click events for rutime generated controls
Next Thread in C# Forum Timeline: lines in a text file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC