|
| 1 | +// Copyright (c) 2013-2022 Cemalettin Dervis, MIT License. |
| 2 | +// https://github.com/cemdervis/SharpConfig |
| 3 | + |
| 4 | +using SharpConfig; |
| 5 | + |
| 6 | +class SomeClass |
| 7 | +{ |
| 8 | + public string SomeString { get; set; } |
| 9 | + |
| 10 | + public int SomeInt { get; set; } |
| 11 | + |
| 12 | + public int[] SomeInts { get; set; } |
| 13 | + |
| 14 | + public DateTime SomeDate { get; set; } |
| 15 | + |
| 16 | + // This field will be ignored by SharpConfig |
| 17 | + // when creating sections from objects and vice versa. |
| 18 | + [SharpConfig.Ignore] |
| 19 | + public int SomeIgnoredField; |
| 20 | + |
| 21 | + // Same for this property. |
| 22 | + [SharpConfig.Ignore] |
| 23 | + public int SomeIgnoredProperty { get; set; } |
| 24 | +} |
| 25 | + |
| 26 | +internal static class Program |
| 27 | +{ |
| 28 | + public static void Main() |
| 29 | + { |
| 30 | + // Call the methods in this file here to see their effect. |
| 31 | + |
| 32 | + //HowToLoadAConfig(); |
| 33 | + //HowToCreateAConfig(); |
| 34 | + //HowToSaveAConfig(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "TestCfg.ini")); |
| 35 | + //HowToCreateObjectsFromSections(); |
| 36 | + //HowToCreateSectionsFromObjects(); |
| 37 | + //HowToHandleArrays(); |
| 38 | + |
| 39 | + Console.ReadLine(); |
| 40 | + } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Shows how to load a configuration from a string (our sample config). |
| 44 | + /// The same works for loading files and streams. Just call the appropriate method |
| 45 | + /// such as LoadFromFile and LoadFromStream, respectively. |
| 46 | + /// </summary> |
| 47 | + private static void HowToLoadAConfig() |
| 48 | + { |
| 49 | + // Read our example config. |
| 50 | + Configuration cfg = Configuration.LoadFromFile("SampleCfg.txt"); |
| 51 | + |
| 52 | + // Just print all sections and their settings. |
| 53 | + PrintConfig(cfg); |
| 54 | + } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Shows how to create a configuration in memory. |
| 58 | + /// </summary> |
| 59 | + private static void HowToCreateAConfig() |
| 60 | + { |
| 61 | + var cfg = new Configuration(); |
| 62 | + |
| 63 | + cfg["SomeStructure"]["SomeString"].StringValue = "foobar"; |
| 64 | + cfg["SomeStructure"]["SomeInt"].IntValue = 2000; |
| 65 | + cfg["SomeStructure"]["SomeInts"].IntValueArray = new[] { 1, 2, 3 }; |
| 66 | + cfg["SomeStructure"]["SomeDate"].DateTimeValue = DateTime.Now; |
| 67 | + |
| 68 | + // We can obtain the values directly as strings, ints, floats, or any other (custom) type, |
| 69 | + // as long as the string value of the setting can be converted to the type you wish to obtain. |
| 70 | + string nameValue = cfg["SomeStructure"]["SomeString"].StringValue; |
| 71 | + |
| 72 | + int ageValue = cfg["SomeStructure"]["SomeInt"].IntValue; |
| 73 | + |
| 74 | + DateTime dateValue = cfg["SomeStructure"]["SomeDate"].DateTimeValue; |
| 75 | + |
| 76 | + // Print our config just to see that it works. |
| 77 | + PrintConfig(cfg); |
| 78 | + } |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// Shows how to save a configuration to a file. |
| 82 | + /// </summary> |
| 83 | + /// <param name="filename">The destination filename.</param> |
| 84 | + private static void HowToSaveAConfig(string filename) |
| 85 | + { |
| 86 | + var cfg = new Configuration(); |
| 87 | + |
| 88 | + cfg["SomeStructure"]["SomeString"].StringValue = "foobar"; |
| 89 | + cfg["SomeStructure"]["SomeInt"].IntValue = 2000; |
| 90 | + cfg["SomeStructure"]["SomeInts"].IntValueArray = new[] { 1, 2, 3 }; |
| 91 | + cfg["SomeStructure"]["SomeDate"].DateTimeValue = DateTime.Now; |
| 92 | + |
| 93 | + cfg.SaveToFile(filename); |
| 94 | + |
| 95 | + Console.WriteLine("The config has been saved to {0}!", filename); |
| 96 | + } |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// Shows how to create C#/.NET objects directly from sections. |
| 100 | + /// </summary> |
| 101 | + private static void HowToCreateObjectsFromSections() |
| 102 | + { |
| 103 | + var cfg = new Configuration(); |
| 104 | + |
| 105 | + // Create the section. |
| 106 | + cfg["SomeStructure"]["SomeString"].StringValue = "foobar"; |
| 107 | + cfg["SomeStructure"]["SomeInt"].IntValue = 2000; |
| 108 | + cfg["SomeStructure"]["SomeInts"].IntValueArray = new[] { 1, 2, 3 }; |
| 109 | + cfg["SomeStructure"]["SomeDate"].DateTimeValue = DateTime.Now; |
| 110 | + |
| 111 | + // Now create an object from it. |
| 112 | + var p = cfg["SomeStructure"].ToObject<SomeClass>(); |
| 113 | + |
| 114 | + // Test. |
| 115 | + Console.WriteLine("SomeString: " + p.SomeString); |
| 116 | + Console.WriteLine("SomeInt: " + p.SomeInt); |
| 117 | + PrintArray("SomeInts", p.SomeInts); |
| 118 | + Console.WriteLine("SomeDate: " + p.SomeDate); |
| 119 | + } |
| 120 | + |
| 121 | + /// <summary> |
| 122 | + /// Shows how to create sections directly from C#/.NET objects. |
| 123 | + /// </summary> |
| 124 | + private static void HowToCreateSectionsFromObjects() |
| 125 | + { |
| 126 | + var cfg = new Configuration(); |
| 127 | + |
| 128 | + // Create an object. |
| 129 | + var p = new SomeClass |
| 130 | + { |
| 131 | + SomeString = "foobar", |
| 132 | + SomeInt = 2000, |
| 133 | + SomeInts = new[] { 1, 2, 3 }, |
| 134 | + SomeDate = DateTime.Now |
| 135 | + }; |
| 136 | + |
| 137 | + // Now create a section from it. |
| 138 | + cfg.Add(Section.FromObject("SomeStructure", p)); |
| 139 | + |
| 140 | + // Print the config to see that it worked. |
| 141 | + PrintConfig(cfg); |
| 142 | + } |
| 143 | + |
| 144 | + /// <summary> |
| 145 | + /// Shows the usage of arrays in SharpConfig. |
| 146 | + /// </summary> |
| 147 | + private static void HowToHandleArrays() |
| 148 | + { |
| 149 | + var cfg = new Configuration(); |
| 150 | + |
| 151 | + cfg["GeneralSection"]["SomeInts"].IntValueArray = new[] { 1, 2, 3 }; |
| 152 | + |
| 153 | + // Get the array back. |
| 154 | + int[] someIntValuesBack = cfg["GeneralSection"]["SomeInts"].GetValueArray<int>(); |
| 155 | + float[] sameValuesButFloats = cfg["GeneralSection"]["SomeInts"].GetValueArray<float>(); |
| 156 | + string[] sameValuesButStrings = cfg["GeneralSection"]["SomeInts"].GetValueArray<string>(); |
| 157 | + |
| 158 | + // There is also a non-generic variant of GetValueArray: |
| 159 | + object[] sameValuesButObjects = cfg["GeneralSection"]["SomeInts"].GetValueArray(typeof(int)); |
| 160 | + |
| 161 | + PrintArray("someIntValuesBack", someIntValuesBack); |
| 162 | + PrintArray("sameValuesButFloats", sameValuesButFloats); |
| 163 | + PrintArray("sameValuesButStrings", sameValuesButStrings); |
| 164 | + PrintArray("sameValuesButObjects", sameValuesButObjects); |
| 165 | + } |
| 166 | + |
| 167 | + /// <summary> |
| 168 | + /// Prints all sections and their settings to the console. |
| 169 | + /// </summary> |
| 170 | + /// <param name="cfg">The configuration to print.</param> |
| 171 | + private static void PrintConfig(Configuration cfg) |
| 172 | + { |
| 173 | + foreach (Section section in cfg) |
| 174 | + { |
| 175 | + Console.WriteLine("[{0}]", section.Name); |
| 176 | + |
| 177 | + foreach (Setting setting in section) |
| 178 | + { |
| 179 | + Console.Write(" "); |
| 180 | + |
| 181 | + if (setting.IsArray) |
| 182 | + Console.Write("[Array, {0} elements] ", setting.ArraySize); |
| 183 | + |
| 184 | + Console.WriteLine(setting.ToString()); |
| 185 | + } |
| 186 | + |
| 187 | + Console.WriteLine(); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + // Small helper method for printing objects of an arbitrary type. |
| 192 | + private static void PrintArray<T>(string arrName, IReadOnlyList<T> arr) |
| 193 | + { |
| 194 | + Console.Write(arrName + " = { "); |
| 195 | + |
| 196 | + for (int i = 0; i < arr.Count - 1; i++) |
| 197 | + Console.Write(arr[i] + ", "); |
| 198 | + |
| 199 | + Console.Write(arr[^1]!.ToString()); |
| 200 | + Console.WriteLine(" }"); |
| 201 | + } |
| 202 | +} |
0 commit comments