Home > Net >  Read the content of the ini file, get in the middle of the night or read not to come out, for help
Read the content of the ini file, get in the middle of the night or read not to come out, for help

Time:12-25

My ini file, the path is in the root directory:
[registration code]
Sn=123456789

[device]
Millet 3=123456789
Millet 5=987654321


The class file content
 
The namespace myfun
{
The class file
{
//write INI statement
[DllImport (" kernel32 ")]
Private static extern long WritePrivateProfileString (string section, the string key, string val, string filePath);

//read INI statement
[DllImport (" kernel32 ")]
Private static extern int GetPrivateProfileString (string section, the string key, string def, StringBuilder retVal, int the size, the string filePath);

//statement in the constructor of variable
Public string ini_path;

//the constructor
The public file ()
{
Enclosing ini_path=@ "configuration files. Ini";
}

//read ini file content
Public string read (string section, the string key)
{
The StringBuilder MSG=new StringBuilder (512);
GetPrivateProfileString (section, key, "Null", MSG, 512, enclosing ini_path);
Return MSG. The ToString ();
}
}
}


The following is a button in winform method
 
Private void button2_Click (object sender, EventArgs e)
{
Myfun. File ini=new myfun. The file ();
Console. WriteLine (ini. Read (" equipment ", "millet 3"));
}


Got no idea, don't know where is wrong, but on the Internet to see all the code is written, so I am very depressed now

CodePudding user response:

As long as the execution, just return to the "Null", always can't read what I want

CodePudding user response:

This way, you haven't made sure that determine GetPrivateProfileString example the function parameters, you can get success

CodePudding user response:

Does not support Chinese, you should change the key - millet 3 to English

CodePudding user response:

If you want to read Chinese, set to Unicode, UTF8 encoding, otherwise unable to support

CodePudding user response:

DllImport set inside the charset

CodePudding user response:

And don't look at your code, I suggest that, rather than directly using json serialization deserialized to direct, to assemble with. Net's own configuration, why should such a roundabout way?

CodePudding user response:

Enclosing ini_path=@ "configuration files. Ini";
The INI file to the file name only, you don't give the directory name? At least adding a AppDomain. CurrentDomain. BaseDirectory also ok,

CodePudding user response:

reference SMYZ_RJY reply: 3/f
does not support Chinese, you should change the key - millet 3 to English ok

I changed my profile, has built a data, as follows:
[aaa]
BBB=123

But still can't read

CodePudding user response:

reference 4 floor SMYZ_RJY response:
if you want to read Chinese, set to Unicode, UTF8 encoding, otherwise unable to support


I now changed to pure English all can't read, don't know where is wrong

CodePudding user response:

Above a few colligate can
The configuration file save as Unicode
Code change
[DllImport (" kernel32, "CharSet=CharSet. Unicode)]
Enclosing ini_path=AppDomain. CurrentDomain. BaseDirectory + "configuration files. Ini";

CodePudding user response:

The
reference 7 floor dugupiaoyun response:
enclosing ini_path=@ "configuration files. Ini";
The INI file to the file name only, you don't give the directory name? At least, adding a AppDomain. CurrentDomain. BaseDirectory also line,


Root directory as so to write directly, I tested in what you said, is to read the content of less than I want to oh

CodePudding user response:


Sorry, sorry to trouble you, I found the problem, is my file path writing mistake, my god, oh
Enclosing ini_path=@ "configuration files. Ini";
To the following:
Enclosing ini_path=@ ". \ configuration file. Ini ".

CodePudding user response:

Why the s still use INI.. JSON is easy

CodePudding user response:

refer to the 13th floor Let 's GO GO GO response:
why this age still use INI.. JSON is easy to do

Because the configuration file to convenient for customers to read and modify, afraid customer will not see other types of configuration files

CodePudding user response:

Microsoft itself with XML, you also use the ini

CodePudding user response:

Actually the ini file is a text file, you got to use regular to get position can be, why should use ini functions

CodePudding user response:

an error prompt can't see your file path is wrong?

CodePudding user response:

references on 17th floor shining sunny reply:
an error prompt don't see your file path is wrong?

No error, write so that I thought was right

CodePudding user response:

on the 14th floor big tiger elder brother 37 references reply:
Quote: refer to the 13th floor Let 's GO GO GO response:
why this age still use INI.. JSON is easy to do

Because the configuration file to convenient for customers to read and modify, other types of configuration files afraid customer will not see

Json and XML readability good ah, you misplaced.
  •  Tags:  
  • C#
  • Related