0

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

Fliwatt
  • 9
  • 1
  • you didn't list `MainForm` where the property for `IniPath` would be defined, but it appears you defined a local variable, not a Public Property – Claies Feb 23 '15 at 23:28
  • So what -in code- am I supposed to do then? – Fliwatt Feb 24 '15 at 00:09
  • You can't do that in an initializer. Create your class member `IniFile InstanceIniFile;` and then initialize it in the constructor `this.InstanceIniFile = new IniFile(IniPath);` – itsme86 Feb 24 '15 at 00:22
  • possible duplicate of [A field initializer cannot reference the non-static field, method, or property](http://stackoverflow.com/questions/8366964/a-field-initializer-cannot-reference-the-non-static-field-method-or-property) – Peter Duniho Feb 24 '15 at 04:48
  • Sorry that I don't really get it, I have the feeling that I am messing everything up here, this is what I did: `string IniPath = Path.Combine(Directory.GetCurrentDirectory(), "config.ini"); InstanceIniFile = new IniFile(IniPath); int Hours = InstanceIniFile.IniReadValue("main", "timeHours", null); static System.Windows.Forms.Timer NetworkSpeedTimer = new System.Windows.Forms.Timer(); public IniFile InstanceIniFile() { this.InstanceIniFile = new IniFile(IniPath); }` And now I am getting new errors, I think I did it wrong – Fliwatt Feb 24 '15 at 17:01

0 Answers0