{"id":130,"date":"2012-08-07T10:09:42","date_gmt":"2012-08-07T09:09:42","guid":{"rendered":"http:\/\/oprsteny.cz\/?p=130"},"modified":"2012-08-07T10:09:42","modified_gmt":"2012-08-07T09:09:42","slug":"manipulate-ini-files-in-c","status":"publish","type":"post","link":"https:\/\/oprsteny.cz\/?p=130","title":{"rendered":"Manipulate INI files in C#"},"content":{"rendered":"<p>If you want to read or write *.ini files within a C# code you can use the following code snippet.<!--more--><\/p>\n<pre lang=\"csharp\">using System;\r\nusing System.Runtime.InteropServices;\r\nusing System.Text;\r\n\r\nnamespace INI\r\n{\r\n  \/\/\/ &lt;summary&gt;\r\n\r\n  \/\/\/ Create a New INI file to store or load data\r\n\r\n  \/\/\/ &lt;\/summary&gt;\r\n\r\n  public class IniFile\r\n  {\r\n    public string path;\r\n\r\n    [DllImport(\"kernel32\")]\r\n    private static extern long WritePrivateProfileString(string section,\r\n        string key, string val, string filePath);\r\n    [DllImport(\"kernel32\")]\r\n    private static extern int GetPrivateProfileString(string section,\r\n             string key, string def, StringBuilder retVal,\r\n        int size, string filePath);\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ INIFile Constructor.\r\n    \/\/\/ &lt;\/summary&gt;\r\n    \/\/\/ &lt;PARAM name=\"INIPath\"&gt;&lt;\/PARAM&gt;\r\n    public IniFile(string INIPath)\r\n    {\r\n      path = INIPath;\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ Write Data to the INI File\r\n    \/\/\/ &lt;\/summary&gt;\r\n    \/\/\/ &lt;PARAM name=\"Section\"&gt;&lt;\/PARAM&gt;\r\n    \/\/\/ Section name\r\n    \/\/\/ &lt;PARAM name=\"Key\"&gt;&lt;\/PARAM&gt;\r\n    \/\/\/ Key Name\r\n    \/\/\/ &lt;PARAM name=\"Value\"&gt;&lt;\/PARAM&gt;\r\n    \/\/\/ Value Name\r\n    public void IniWriteValue(string Section, string Key, string Value)\r\n    {\r\n      WritePrivateProfileString(Section, Key, Value, this.path);\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ Read Data Value From the Ini File\r\n    \/\/\/ &lt;\/summary&gt;\r\n    \/\/\/ &lt;PARAM name=\"Section\"&gt;&lt;\/PARAM&gt;\r\n    \/\/\/ &lt;PARAM name=\"Key\"&gt;&lt;\/PARAM&gt;\r\n    \/\/\/ &lt;PARAM name=\"Path\"&gt;&lt;\/PARAM&gt;\r\n    \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\r\n    public string IniReadValue(string Section, string Key)\r\n    {\r\n      StringBuilder temp = new StringBuilder(255);\r\n      int i = GetPrivateProfileString(Section, Key, \"\", temp,\r\n                                      255, this.path);\r\n      return temp.ToString();\r\n    }\r\n  }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you want to read or write *.ini files within a C# code you can use the following code snippet.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[11,9],"tags":[445,23,22],"class_list":["post-130","post","type-post","status-publish","format-standard","hentry","category-c","category-development","tag-c","tag-dllimport","tag-ini"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3nYbe-26","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=130"}],"version-history":[{"count":2,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/130\/revisions"}],"predecessor-version":[{"id":132,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/130\/revisions\/132"}],"wp:attachment":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}