{"id":5029,"date":"2014-05-04T21:20:09","date_gmt":"2014-05-04T12:20:09","guid":{"rendered":"https:\/\/mimumimu.net\/blog\/?p=5029"},"modified":"2014-05-04T21:33:41","modified_gmt":"2014-05-04T12:33:41","slug":"c-%e3%81%a7-ini-%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%81%ae%e5%86%85%e5%ae%b9%e3%82%92%e3%82%af%e3%83%a9%e3%82%b9%e3%81%ab%e5%85%a5%e3%82%8c%e3%82%8b","status":"publish","type":"post","link":"https:\/\/mimumimu.net\/blog\/2014\/05\/04\/c-%e3%81%a7-ini-%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%81%ae%e5%86%85%e5%ae%b9%e3%82%92%e3%82%af%e3%83%a9%e3%82%b9%e3%81%ab%e5%85%a5%e3%82%8c%e3%82%8b\/","title":{"rendered":"C# \u3067 ini \u30d5\u30a1\u30a4\u30eb\u306e\u5185\u5bb9\u3092\u4efb\u610f\u306e\u30af\u30e9\u30b9\u306b\u683c\u7d0d\u3059\u308b\u30b3\u30fc\u30c9\u3092\u66f8\u3044\u3066\u307f\u305f\u3002"},"content":{"rendered":"<p>\u3069\u3046\u3082\u307f\u3080\u3089\u3067\u3059\u3002<\/p>\n<p>C# \u3067 ini \u30d5\u30a1\u30a4\u30eb\u3092\u64cd\u4f5c\u3059\u308b\u3068\u306a\u308b\u3068\u3001    <br \/>\u3088\u304f\u3042\u308b\u306e\u304c GetPrivateProfileString \u3042\u305f\u308a\u3092\u53e9\u3044\u3066\u53d6\u5f97\u3059\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u3067\u3082\u305d\u306e\u5834\u5408\u3001\u3044\u3061\u3044\u3061\u8981\u7d20\u3092\u6307\u5b9a\u3057\u3066\u53d6\u5f97\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u305a\u3001    <br \/>\u7279\u306b\u8907\u6570\u306e\u30ad\u30fc\uff08\u30d5\u30a3\u30fc\u30eb\u30c9\uff09\u304c\u3042\u308b\u5834\u5408\u306b\u8907\u6570\u56de\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u306e\u3082\u9762\u5012\u3067\u3059\u3002<\/p>\n<p>\u7279\u306b ini \u30d5\u30a1\u30a4\u30eb\u306e\u9805\u76ee\u304c\u591a\u304f\u3066\u3001\u3053\u3093\u306a\u72b6\u614b\u306b\u306a\u3063\u305f\u65e5\u306b\u306f\u30fb\u30fb\u3002<\/p>\n<p><a href=\"http:\/\/mimumimu.net\/blog\/wp-content\/uploads\/2014\/05\/image.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px\" border=\"0\" alt=\"image\" src=\"http:\/\/mimumimu.net\/blog\/wp-content\/uploads\/2014\/05\/image_thumb.png\" width=\"397\" height=\"698\" \/><\/a><\/p>\n<p>&#160;<\/p>\n<p>\u30fb\u30fb\u3068\u3044\u3046\u3053\u3068\u3067\u3001 Reflection \u3092\u4f7f\u7528\u3057\u3066    <br \/>\u3053\u306e\u8fba\u3092\u3059\u3054\u304f\u697d\u306b\u51fa\u6765\u308b\u3088\u3046\u306a\u611f\u3058\u3067\u30b3\u30fc\u30c9\u3092\u66f8\u3044\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p>&#160;<\/p>\n<h3>\u30b3\u30fc\u30c9\uff1a<\/h3>\n<div id=\"scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:7a9f229a-f3bc-457e-a70a-6b5d07726187\" class=\"wlWriterEditableSmartContent\" style=\"float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px\">\n<pre class=\"brush: c#;\">using System;\nusing System.IO;\nusing System.Runtime.InteropServices;\nusing System.Text;\n\npublic static class Ini\n{\n    [DllImport(\"KERNEL32.DLL\")]\n    public static extern uint GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, uint nSize, string lpFileName);\n\n    [DllImport(\"KERNEL32.DLL\")]\n    public static extern uint GetPrivateProfileInt(string lpAppName, string lpKeyName, int nDefault, string lpFileName);\n\n    [DllImport(\"KERNEL32.DLL\")]\n    public static extern uint WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName);\n\n    public static T Read&lt;T&gt;(string section, string filepath)\n    {\n        T ret = (T)Activator.CreateInstance(typeof(T));\n\n        foreach (var n in typeof(T).GetFields())\n        {\n            if (n.FieldType == typeof(int))\n            {\n                n.SetValue(ret, (int)GetPrivateProfileInt(section, n.Name, 0, Path.GetFullPath(filepath)));\n            }\n            else if (n.FieldType == typeof(uint))\n            {\n                n.SetValue(ret, GetPrivateProfileInt(section, n.Name, 0, Path.GetFullPath(filepath)));\n            }\n            else\n            {\n                var sb = new StringBuilder(1024);\n                GetPrivateProfileString(section, n.Name, \"\", sb, (uint)sb.Capacity, Path.GetFullPath(filepath));\n                n.SetValue(ret, sb.ToString());\n            }\n        };\n\n        return ret;\n    }\n\n    public static void Write&lt;T&gt;(string secion, T data, string filepath)\n    {\n        foreach (var n in typeof(T).GetFields())\n        {\n            WritePrivateProfileString(secion, n.Name, n.GetValue(data).ToString(), Path.GetFullPath(filepath));\n        };\n    }\n}<\/pre>\n<\/div>\n<p><\/p>\n<p>&#160;<\/p>\n<h3>\u4f7f\u3044\u65b9\uff1a<\/h3>\n<p>ini \u30d5\u30a1\u30a4\u30eb\u306e\u8aad\u307f\u53d6\u308a\u305f\u3044\u30bb\u30af\u30b7\u30e7\u30f3\u306b\u5408\u308f\u305b\u305f\u30af\u30e9\u30b9\u3092\u7528\u610f\u3057\u307e\u3059\u3002<\/p>\n<div id=\"scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:16f6cf7d-3442-4f92-b6de-32eeadf7b200\" class=\"wlWriterEditableSmartContent\" style=\"float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px\">\n<pre class=\"brush: c#;\">    public class Human\n    {\n        public string name;\n        public int age;\n    }<\/pre>\n<\/div>\n<p>\u3067\u3082\u3063\u3066\u6b21\u306e\u3088\u3046\u306a ini \u30d5\u30a1\u30a4\u30eb\u3092\u7528\u610f\u3057\u3066<\/p>\n<div id=\"scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:32cd4587-6366-4cd7-9672-86a8a9795777\" class=\"wlWriterEditableSmartContent\" style=\"float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px\">\n<pre class=\"brush: text;\">[01]\nname=TARO\nage=20\n<\/pre>\n<\/div>\n<p>\u3042\u3068\u306f\u3053\u3093\u306a\u98a8\u306b\u547c\u3073\u51fa\u3057\u307e\u3059\u3002<\/p>\n<div id=\"scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:7e5c952a-606e-4974-b4f6-a6d8ecaecb52\" class=\"wlWriterEditableSmartContent\" style=\"float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px\">\n<pre class=\"brush: c#;\">        static void Main(string[] args)\n        {\n            var h = Ini.Read&lt;Human&gt;(\"01\", \"DATA.INI\");\n\n            h.age = 15;\n            h.name = \"JIRO\";\n            Ini.Write(\"02\", h, \"DATA.INI\");\n        }<\/pre>\n<\/div>\n<p>&#160;<\/p>\n<p>\u5b9f\u884c\u3057\u307e\u3059\u3068\u3001<\/p>\n<p><a href=\"http:\/\/mimumimu.net\/blog\/wp-content\/uploads\/2014\/05\/image1.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px\" border=\"0\" alt=\"image\" src=\"http:\/\/mimumimu.net\/blog\/wp-content\/uploads\/2014\/05\/image_thumb1.png\" width=\"450\" height=\"158\" \/><\/a><\/p>\n<p>\u3053\u3093\u306a\u611f\u3058\u3067\u30c7\u30fc\u30bf\u304c\u8aad\u307f\u51fa\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>\u3067\u3082\u3063\u3066\u3001 ini \u30d5\u30a1\u30a4\u30eb\u306f<\/p>\n<div id=\"scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:0198fded-6812-4489-9034-931afe6c1a7f\" class=\"wlWriterEditableSmartContent\" style=\"float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px\">\n<pre class=\"brush: text;\">[01]\nname=TARO\nage=20\n[02]\nname=JIRO\nage=15\n<\/pre>\n<\/div>\n<p>\u3053\u3093\u306a\u611f\u3058\u306b\u3001\u3061\u3083\u3093\u3068\u30bb\u30af\u30b7\u30e7\u30f3 02 \u304c\u66f8\u304d\u8fbc\u307e\u308c\u307e\u3059\u3002<\/p>\n<hr \/>\n<h3>\u8e93\u304d\u305d\u3046\u306a\u3068\u3053\u308d\uff1a<\/h3>\n<p>\u30c7\u30fc\u30bf\u3092\u683c\u7d0d\u3059\u308b\u30af\u30e9\u30b9\u304c\u3001\u3053\u3046\u3044\u3046\u611f\u3058\u306b {get; set;} \u306a\u72b6\u614b\u306b\u306a\u3063\u3066\u3044\u308b\u5834\u5408\uff1a<\/p>\n<div id=\"scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:4b4636a7-f073-4e83-a23c-b399825573c4\" class=\"wlWriterEditableSmartContent\" style=\"float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px\">\n<pre class=\"brush: c#;\">    public class Human\n    {\n        public string name { get; set; }\n        public int age { get; set; }\n    }<\/pre>\n<\/div>\n<p>\u4e0a\u8a18\u30b3\u30fc\u30c9\u5185\u306e GetFields \u3092 GetProperties \u306b\u63db\u3048\u3001 FieldType \u3092 PropertyType \u306b\u5909\u3048\u308c\u3070\u52d5\u4f5c\u3057\u307e\u3059\u3002<\/p>\n<hr \/>\n<p>\u30fb\u30fb\u305d\u3093\u306a\u308f\u3051\u3067\u3001\u3082\u3057\u826f\u3051\u308c\u3070\u81ea\u5df1\u8cac\u4efb\u3067\u3054\u5229\u7528\u304f\u3060\u3055\u3044\uff0d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3069\u3046\u3082\u307f\u3080\u3089\u3067\u3059\u3002 C# \u3067 ini \u30d5\u30a1\u30a4\u30eb\u3092\u64cd\u4f5c\u3059\u308b\u3068\u306a\u308b\u3068\u3001 \u3088\u304f\u3042\u308b\u306e\u304c GetPrivateProfileString \u3042\u305f\u308a\u3092\u53e9\u3044\u3066\u53d6\u5f97\u3059\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002 \u3067\u3082\u305d\u306e\u5834\u5408\u3001\u3044\u3061\u3044\u3061\u8981\u7d20\u3092\u6307\u5b9a\u3057\u3066\u53d6\u5f97\u3057\u306a\u3051\u308c\u3070 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,15],"tags":[128,499,504,500,107,448,503,505,502],"class_list":["post-5029","post","type-post","status-publish","format-standard","hentry","category-csharp","category-windows","tag-c","tag-ini","tag-504","tag-500","tag-107","tag-448","tag-503","tag-505","tag-502"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/mimumimu.net\/blog\/wp-json\/wp\/v2\/posts\/5029","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mimumimu.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mimumimu.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mimumimu.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mimumimu.net\/blog\/wp-json\/wp\/v2\/comments?post=5029"}],"version-history":[{"count":0,"href":"https:\/\/mimumimu.net\/blog\/wp-json\/wp\/v2\/posts\/5029\/revisions"}],"wp:attachment":[{"href":"https:\/\/mimumimu.net\/blog\/wp-json\/wp\/v2\/media?parent=5029"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mimumimu.net\/blog\/wp-json\/wp\/v2\/categories?post=5029"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mimumimu.net\/blog\/wp-json\/wp\/v2\/tags?post=5029"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}