I am fairly new to C# and I am currently writing a program and this program should be able to write to an Ini File, that is why I used this class
public 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 void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.path);
}
public string IniReadValue(string Section, string Key, string defaultvalue)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
if (temp.ToString() == "")
{
return defaultvalue;
}
return temp.ToString();
}
}
To write and read IniFiles. I understood that you need to call that class from an Instance, that is exactly what I did:
IniFile InstanceIniFile = new IniFile(IniPath);
To understand my programm, I have three files, one Program.cs which contains the class, one FirstStart.cs which uses the InstanceIniFile and everything works fine, the problem is now that I also want to read and write Ini Files with the MainForm.cs but whereever I put the IniFile InstanceIniFile = new IniFile(IniPath);
I always get this error:
Error 1 A field initializer cannot reference the non-static field, method, or property 'InetTimeManager.MainForm.IniPath' D:\Extras\Programmieren\C#\InetTimeManager\InetTimeManager\MainForm.cs 20 47 InetTimeManager
, IniPath is always the same variable in each file. I also made sure that it exists, in case you are wondering, IniPath is just this:
string IniPath = Path.Combine(Directory.GetCurrentDirectory(), "config.ini");
So I am not really sure what I am doing wrong and I would really appreciate your help. Thank you very much Fliwatt