Answer the question
In order to leave comments, you need to log in
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();
}
}
}
Answer the question
In order to leave comments, you need to log in
Linux does not have kernel32. Accordingly, the program will not be able to import this library.
Use Mono to cross compile c#.
On Linux, either parse ini files yourself, or use ready-made solutions like libconfig or this
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 "";
}
}
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 questionAsk a Question
731 491 924 answers to any question