|
To delete use following method
To delete key :-
iniFile.IniWriteValue("section", "key", null);
To delete section :-
iniFile.IniWriteValue("section", null, null);
|
|
|
|
|
https://gist.github.com/anonymous/7a2efb7291eb6d68c6c8
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
class IniFile
{
public string path;
[DllImport("KERNEL32.DLL", EntryPoint = "GetPrivateProfileSectionW",
SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern int GetPrivateProfileSection(string lpAppName, string lpReturnedString, int nSize, string lpFileName);
[DllImport("KERNEL32.DLL", EntryPoint = "GetPrivateProfileSectionNamesW",
SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern int GetPrivateProfileSectionNames(string lpReturnedString, int nSize, string lpFileName);
[DllImport("KERNEL32.DLL", EntryPoint = "GetPrivateProfileStringW",
SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, string lpReturnString, int nSize, string lpFilename);
[DllImport("KERNEL32.DLL", EntryPoint = "WritePrivateProfileStringW",
SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern int WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFilename);
[DllImport("KERNEL32.DLL", EntryPoint = "WritePrivateProfileSectionW",
SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern int WritePrivateProfileSection(string lpAppName, string lpString, string lpFilename);
public IniFile(string INIPath)
{
path = INIPath;
}
public void WriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.path);
}
public void WriteAllSection(string section, string keyvalues)
{
WritePrivateProfileSection(section, keyvalues, path);
}
public string ReadValue(string section, string Key)
{
string returnString = new string(' ', 1024);
GetPrivateProfileString(section, Key, null, returnString, 1024, this.path);
return returnString;
}
public Dictionary<string, string> GetKeyValuesPair(string section)
{
List<string> tlist = GetKeyValues(section);
Dictionary<string, string> tdic = new Dictionary<string, string>();
if (tlist == null || !tlist.Any()) return tdic;
foreach (var str in tlist)
{
if (string.IsNullOrEmpty(str)) continue;
var vl = str.Split('=');
tdic.Add(vl[0], vl[1]);
}
return tdic;
}
public List<string> GetKeyValues(string section)
{
string returnString = new string(' ', 65536);
GetPrivateProfileSection(section, returnString, 65536, this.path);
List<string> result = new List<string>(returnString.Split('\0'));
result.RemoveRange(result.Count - 2, 2);
return result;
}
public List<string> GetSectionsA()
{
string returnString = new string(' ', 65536);
GetPrivateProfileSectionNames(returnString, 65536, this.path);
List<string> result = new List<string>(returnString.Split('\0'));
result.RemoveRange(result.Count - 2, 2);
return result;
}
public List<string> GetSectionsB()
{
string returnString = new string(' ', 65536);
GetPrivateProfileString(null, null, null, returnString, 65536, this.path);
List<string> result = new List<string>(returnString.Split('\0'));
result.RemoveRange(result.Count - 2, 2);
return result;
}
public List<string> GetKeysOnly(string section)
{
string returnString = new string(' ', 32768);
GetPrivateProfileString(section, null, null, returnString, 32768, this.path);
List<string> result = new List<string>(returnString.Split('\0'));
result.RemoveRange(result.Count - 2, 2);
return result;
}
}
https://gist.github.com/anonymous/7a2efb7291eb6d68c6c8
|
|
|
|
|
The only change I would suggest on top of your changes is make the things that should return a bool actually return a bool instead of a int. For example "GetPrivateProfileStringW"
|
|
|
|
|
|
Nice article. Thanks for sharing
|
|
|
|
|
It very simple and useful
|
|
|
|
|
Thank you, man, easy and fast solution!
|
|
|
|
|
Suppose I call the constructor like this
IniFile ifh = new IniFile("Foo.ini");
and then I call
string val = ifh.IniReadValue("Section1", "Key1"));
then (despite the fact that the Key really has a value in Section1)
val is not set to the right value.
And yet File.Exists("Foo.ini") returns true.
But if I replace Foo.ini by its absolute path it works.
So I modified the code of the constructor to this
public IniFile(string INIPath)
{
path = Path.GetFullPath(INIPath);
}
Now things work well.
Go figure.
Update:
I would look at another project that is more like the current, but more up to date and more powerful.
modified 17-Feb-15 15:13pm.
|
|
|
|
|
|
Hi, I puted following code in my application,,for both read and write from Ini file.
[DllImport("Kernel32")]
private static extern long WritePrivateProfileStringW(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 static void iniWriteValue(string Section, string Key, string Value)
{
Console.WriteLine(WritePrivateProfileStringW(Section, Key, Value, Inipath));
}
public static string iniReadValue(string Section,string Key)
{
StringBuilder temp=new StringBuilder(150);
int i = GetPrivateProfileString(Section, Key, "", temp, 150, Inipath);
return temp.ToString();
}
but I cann't able to write values in my Ini file, which is located in C:\\Windows folder.
|
|
|
|
|
Devendra lodha wrote: but I cann't able to write values in my Ini file
what do you mean ? do you get a error ? what happens ?
I'd also ask why your ini file is in a Windows folder ? do you not think it might be permission related ?
|
|
|
|
|
No I did'n get any error,
If I put the file for Ex. D:\abc.ini then it is easily writable,
but I want to do this in C:\Windows folder.
when I execute code with C:\Windows\abc.ini file then nothing is happen means not any error occur, but file also not updated.
|
|
|
|
|
that pretty well proves its the permissions on c:\windows that are the issue - the only way I know for you to be able to run your program and modify a file there is run the program as Administrator
|
|
|
|
|
Hello Sir,
I used this code in my application, Thank for this.
I have one doubt WritePrivateProfileString always returns 4294967297 whether it write value successfully or not.
so I want to ask that is there any way to know that WritePrivateProfileString() writing values or not.
I want to show a message if value is not written.
|
|
|
|
|
|
Simple and effective. Thanks.
|
|
|
|
|
This is much simpler than other INI readers I have encountered.
In my program, I have this:
bool myBool;
IniFile myIni = new IniFile((AppDomain.CurrentDomain.BaseDirectory) + "Planet_001.ini");
int CurrentSeed;
myBool = int.TryParse(myIni.IniReadValue("Init", "CurrentSeed"), out CurrentSeed);
if (myBool == false)
{
CurrentSeed = 0;
myIni.IniWriteValue("Init", "CurrentSeed", " 0");
myBool = true;
}
This tests the value in the INI, and if absent or of the wrong type, will substitute a default value, and write that value to the INI file as well. It also will create an INI file if none exists.
|
|
|
|
|
Replace DLLImport("kernel32")] for [DLLImport("KERNEL32.DLL")]
But, I Approve.
PT_BR:
Substitua a linha [DLLImport("kernel32")] por [DLLImport("KERNEL32.DLL")]
Mas, Eu Aprovo
|
|
|
|
|
|
Thank you for posting this! I am not able to append an existing file, it just overwrites. Any suggestions, anyone?
|
|
|
|
|
Thanks.. Helped me at the right time
|
|
|
|
|
|
my long time issue of saving config solved
|
|
|
|
|
for me it dont work on Windows 8 the Program don't create the ini file
|
|
|
|
|
i am using windows 7. please check the file path . use absolute path like @c:\temp.ini. if you put only file name then it's not creating ini file.
|
|
|
|
|