|
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.
|
|
|
|
|
The Closing event is obsolete in the .NET Framework version 2.0; use the FormClosing event in example instead.
|
|
|
|
|
this is awesome..very comprehensive...thanks...
and easy to use..Cheers!
|
|
|
|
|
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;
public class IniFile
{
[DllImport("KERNEL32.DLL", EntryPoint = "GetPrivateProfileStringW",
SetLastError = true,
CharSet = CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
private static extern int GetPrivateProfileString(
string lpSection,
string lpKey,
string lpDefault,
StringBuilder 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 lpSection,
string lpKey,
string lpValue,
string lpFileName);
private string _path = "";
public string Path {
get
{
return _path;
}
set
{
if (!File.Exists(value))
File.WriteAllText(value, "", Encoding.Unicode);
_path = value;
}
}
public IniFile(string INIPath)
{
this.Path = INIPath;
}
public void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.Path);
}
public string IniReadValue(string Section, string Key)
{
const int MAX_CHARS = 1023;
StringBuilder result = new StringBuilder(MAX_CHARS);
GetPrivateProfileString(Section, Key, "", result, MAX_CHARS, this.Path);
return result.ToString();
}
}
|
|
|
|
|
Clear structured an easy to use!
|
|
|
|
|
Thankx Dear...!!! It helps me lot...!!!
But i hav one query....if i want to use <> this for defining Section i.e.
how to read this test.ini file?
<info>
<mdbpath>=<d:\fff\xyz.mdb>
|
|
|
|
|
|
My piece of code in VB.Net
<Runtime.InteropServices.DllImport("kernel32.dll", CharSet:=Runtime.InteropServices.CharSet.Ansi, SetLastError:=True)> _
Private Function WritePrivateProfileString(Section As String, Key As String, Value As String, FilePath As String) As Long
End Function
<Runtime.InteropServices.DllImport("kernel32.dll", CharSet:=Runtime.InteropServices.CharSet.Ansi, SetLastError:=True)> _
Private Function GetPrivateProfileString(Section As String, Key As String, DefaultValue As String, Result As System.Text.StringBuilder, Size As Integer, FilePath As String) As Long
End Function
Public Function IniFileModifyValue(ByVal FilePath As String, ByVal Section As String, ByVal Key As String, ByVal Value As String) As Boolean
Dim ret As Long
Try
ret = WritePrivateProfileString(Section, Key, Value, FilePath)
If ret = 0 Then Return False
Catch ex As Exception
Return False
End Try
Return True
End Function
Public Function IniFileReadSingleValue(FilePath As String, Section As String, Key As String, DefaultValue As String) As String
Dim ret As Long
Dim res As String = DefaultValue
Dim sb As New System.Text.StringBuilder(255)
If IO.File.Exists(FilePath) = True Then
ret = GetPrivateProfileString(Section, Key, DefaultValue, sb, sb.Capacity, FilePath)
If ret > 0 Then
ret = Global.System.Math.Min(ret, sb.Capacity)
ret = Global.System.Math.Min(ret, sb.Length)
res = sb.ToString(0, ret)
End If
End If
Return res
End Function
|
|
|
|
|
|
It solved my problem and useful article..
|
|
|
|
|