using INI(setting) file in c#

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 26
Reputation: kool.net is an unknown quantity at this point 
Solved Threads: 0
kool.net kool.net is offline Offline
Light Poster

using INI(setting) file in c#

 
0
  #1
21 Days Ago
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 901
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 142
DdoubleD DdoubleD is offline Offline
Posting Shark
Join Date: Aug 2009
Posts: 331
Reputation: Ryshad has a spectacular aura about Ryshad has a spectacular aura about 
Solved Threads: 61
Ryshad's Avatar
Ryshad Ryshad is offline Offline
Posting Whiz
 
0
  #3
20 Days Ago
You could also look into settings to store application data.
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.

"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 7
Reputation: milosz is an unknown quantity at this point 
Solved Threads: 0
milosz milosz is offline Offline
Newbie Poster
 
0
  #4
20 Days Ago
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 331
Reputation: Ryshad has a spectacular aura about Ryshad has a spectacular aura about 
Solved Threads: 61
Ryshad's Avatar
Ryshad Ryshad is offline Offline
Posting Whiz
 
0
  #5
20 Days Ago
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
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.

"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 26
Reputation: kool.net is an unknown quantity at this point 
Solved Threads: 0
kool.net kool.net is offline Offline
Light Poster
 
0
  #6
19 Days Ago
hello milosz, pls. send me the code.
thanks a lot
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #7
19 Days Ago
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.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 7
Reputation: milosz is an unknown quantity at this point 
Solved Threads: 0
milosz milosz is offline Offline
Newbie Poster
 
0
  #8
19 Days Ago
Originally Posted by kool.net View Post
hello milosz, pls. send me the code.
thanks a lot
create IniFile.cs and copy/paste code below. Change namespace.
  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:
  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.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC