0

I have a question and don't quite understand. I've read the article Reading/writing an INI file And I have applied it to my project. but I cannot read the data in my .int file.

This is the code I use

var MyIni = new IniFile("config.ini");
                string id = MyIni.Read("id").ToString();
                string url = MyIni.Read("url").ToString();
                string token = MyIni.Read("token").ToString();

This is the class I use "IniFile.cs"

using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;

// Change this to match your program's normal namespace
namespace MyProg
{
class IniFile   // revision 11
{
    string Path;
    string EXE = Assembly.GetExecutingAssembly().GetName().Name;

    [DllImport("kernel32", CharSet = CharSet.Unicode)]
    static extern long WritePrivateProfileString(string Section, string Key, string Value, string FilePath);

    [DllImport("kernel32", CharSet = CharSet.Unicode)]
    static extern int GetPrivateProfileString(string Section, string Key, string Default, StringBuilder RetVal, int Size, string FilePath);

    public IniFile(string IniPath = null)
    {
        Path = new FileInfo(IniPath ?? EXE + ".ini").FullName;
    }

    public string Read(string Key, string Section = null)
    {
        var RetVal = new StringBuilder(255);
        GetPrivateProfileString(Section ?? EXE, Key, "", RetVal, 255, Path);
        return RetVal.ToString();
    }

    public void Write(string Key, string Value, string Section = null)
    {
        WritePrivateProfileString(Section ?? EXE, Key, Value, Path);
    }

    public void DeleteKey(string Key, string Section = null)
    {
        Write(Key, null, Section ?? EXE);
    }

    public void DeleteSection(string Section = null)
    {
        Write(null, null, Section ?? EXE);
    }

    public bool KeyExists(string Key, string Section = null)
    {
        return Read(Key, Section).Length > 0;
    }
}

}

this is the information in my "IniFile.ini"

id=5eb3c344a9e9486ebb3450cd 
url=https:https://demo.cti.com/
token=FO_KO7XTNe6tamWL9PlFG7L5gbGObl4z

I put my file "IniFile.ini" in the "debug" folder of my project. Hope everyone help me, what was wrong with me? thanks so much!

ASh
  • 34,632
  • 9
  • 60
  • 82
CongSyIT
  • 61
  • 6

1 Answers1

1

Getting NULL with section parameter of GetPrivateProfileString doesn't means getting values from sectionless key of INI file.(https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getprivateprofilestring)

So, you can make your .ini like

[config]
id=5eb3c344a9e9486ebb3450cd 
url=https:https://demo.cti.com/
token=FO_KO7XTNe6tamWL9PlFG7L5gbGObl4z

Then

string id = MyIni.Read("id", "config").ToString();
string url = MyIni.Read("url", "config").ToString();
string token = MyIni.Read("token", "config").ToString();

and it will works well.

Point is, GetPrivateProfileString cannot handle the sectionless INI file. You may have to other library to handle with that.

Klaus Gütter
  • 11,151
  • 6
  • 31
  • 36
Ryu
  • 271
  • 1
  • 12