L
L
LiptonOlolo2016-03-30 19:52:41
C++ / C#
LiptonOlolo, 2016-03-30 19:52:41

how to read ini file on linux

Good evening. The title speaks for itself.
There is this code to read:

using System.Text;
using System.Runtime.InteropServices;

namespace iniRead
{
    public class iniReader
    {
        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 iniReader(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)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
            return temp.ToString();
        }
    }
}

But on linux systems, it refuses to read files and cannot find some kind of library, like, and even if it is placed next to the program, the result is zero.
So how do you read ini on linux systems?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
G
GavriKos, 2016-03-30
@GavriKos

Linux does not have kernel32. Accordingly, the program will not be able to import this library.
Use Mono to cross compile c#.

A
Alexander Ananiev, 2016-03-30
@SaNNy32

On Linux, either parse ini files yourself, or use ready-made solutions like libconfig or this

L
LiptonOlolo, 2016-04-03
@LiptonOlolo

I didn’t want to do my own thing until the end, I thought there was a finished one, well, here is the code for shit code to read

public class IniReader_Linux
    {
        string ini_path = "";
        public void Path(string path)
        {
            ini_path = path;
        }
        public string Read(string Case, string Key)
        {
            StreamReader sr = new StreamReader(ini_path);
            string[] lines = sr.ReadToEnd().Replace("\r", String.Empty).Split('\n');

            for (int i = 0; i < lines.Length; i++)
                if (lines[i].IndexOf("#") == -1)
                    if (String.Format("[{0}]", Case) == lines[i])
                        for (int j = 0; j < lines.Length; j++)
                            if (lines[j].Split('=')[0] == Key)
                                return lines[j].Split('=')[1];

            return "";
        }
    }

Call:
IniReader_Linux ir = new IniReader_Linux(AppDomain.CurrentDomain.BaseDirectory + "Config.ini");

string ip = ir.Read("Settings", "ip");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question