简体   繁体   中英

I can not reading file .ini with C#

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!

Getting NULL with section parameter of GetPrivateProfileString doesn't means getting values from sectionless key of INI file.( https://docs.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM