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 (); } } }
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,