c# - Read and edit List with ini file -
i have list contains several names , want add or remove of names of ini file. prefer ini file if isn't possible or hard kind of config file fine.
here code ini class if help.
private string _filepath; [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 clsini(string filepath) { _filepath = filepath; } public void write(string section, string key, string value) { try { writeprivateprofilestring(section, key, value, _filepath); } catch {} } public string read(string section, string key) { try { var sb = new stringbuilder(255); getprivateprofilestring(section, key, "", sb, 255, _filepath); return sb.tostring(); } catch { return null; } }
at moment can edit strings , ints ini file, want add or remove items in list don't know how.
here add string username example:
to make file:
`_cini = new clsini(path.combine(application.startuppath, "settings.ini"));`
to write file:
`username = ""; _cini.write("settings", "username", username);
to read file:
`username = _cini.read("settings", "username"); `
`
elements in ini-file strings. create string holds list of strings need decide on seperator.
there several characters come mind, naturally tab
, vtab
characters: \t
, \v
. others possible, too, see below! catch is, sure won't occur in data.
therefore commas
, semicolons
, used, not choices, when don't know precisely , won't store..
let's example:
we start strange , mysterious list of names:
list<string> ec = new list<string>() { "fx huberman", "kitty collins", "swede anderson", "cody jarrett", };
now join them tab
character separator , write them our file::
_cini.write("settings", "eoc", string.join("\t", ec.toarray()));
and can read back, maybe list, splitting again tab
character:
list<string> fc = _cini.read("settings", "eoc").split('\t').tolist();
see here discussion of other suitable control charcters:
data structuring
the separators (file, group, record, , unit: fs, gs, rs , us) made structure data, on tape, in order simulate punched cards. end of medium (em) warns tape (or other recording medium) ending. while many systems use cr/lf , tab structuring data, possible encounter separator control characters in data needs structured. separator control characters not overloaded; there no general use of them except separate data structured groupings. numeric values contiguous space character, can considered member of group, word separator.
Comments
Post a Comment