Configuration File INI? XML? Other?
我目前在学区工作,我们正在尝试为我们的工具箱创建一个C#exe。我们是C#的新手,正在学习它,但由于其灵活性而希望使用它。 C#工具包将用于自动设置具有我们选择的设置,程序,Regedit更改和首选项的新计算机或重新映像的计算机。
我们有一个工作版本,已在Powershell中进行了硬编码,然后将其转移到c#。
当前存在的问题是如何使用设置/配置文件(如ini或xml)从中获取通用信息并填充函数和变量。这样一来,我们就可以创建可以选择并运行的常规配置文件。例如:
1 2 3 4 5 6 7 8 9 10 11 | [Server] IP=172.1.0.10 Port=9999 PathToCSV=\\\\c:\\Files.csv [Client] RestartComputerAfterScript=1 Install.Browser.Firefox=1 Install.Browser.Chrome=1 Install.Java=0 Install.PrinterLogic=1 SysConfig.TempLoc=d:\\TEMP |
将从我们的C#GUI中选择此ini文件,并填充该文件:
我们最终还将尝试传递其他变量,例如注册表路径和值等。
有没有人对我们如何使用INI提出建议,或者对创建构建配置文件有更好的建议?
我研究过的资源:
INI:
https://www.codeproject.com/Articles/1966/An-INI-file-handling-class-using-C
如果您可以使用其他文件格式,我会使用JSON,因为它简单易用。
使用精彩的Newtonsoft.Json库:
模式类别:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | class Server { public string IP { get; set; } public int Port { get; set; } public string PathToCSV { get; set; } } class Client { public bool RestartComputerAfterScript { get; set; } public List<string> Install { get; set; } public string TempLoc { get; set; } } class Config { public Server ServerConfig { get; set; } public Client ClientConfig { get; set; } } |
config.json:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | { "ServerConfig": { "IP":"172.1.0.10", "Port": 9999, "PathToCSV":"C:\\\\Files.csv" }, "ClientConfig": { "RestartComputerAfterScript": true, "Install": [ "Firefox", "Chrome", "PrinterLogic" ], "TempLoc":"D:\\\\TEMP" } } |
从磁盘读取文件:
1 2 3 4 | public static Config Read(string path) { return JsonConvert.DeserializeObject<Config>(System.IO.File.ReadAllText(path)); } |
保存配置文件:
1 2 3 4 | public static void Write(string path, Config config) { System.IO.File.WriteAllText(path, JsonConvert.SerializeObject(config, Formatting.Indented)); } |
.Net Framework具有为此目的而设计的ConfigurationManager类。
节选:
XML
1 2 3 4 5 6 7 8 9 10 | <?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </appSettings> </configuration> |
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | using System; using System.Configuration; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { ReadAllSettings(); ReadSetting("Setting1"); ReadSetting("NotValid"); AddUpdateAppSettings("NewSetting","May 7, 2014"); AddUpdateAppSettings("Setting1","May 8, 2014"); ReadAllSettings(); } static void ReadAllSettings() { try { var appSettings = ConfigurationManager.AppSettings; if (appSettings.Count == 0) { Console.WriteLine("AppSettings is empty."); } else { foreach (var key in appSettings.AllKeys) { Console.WriteLine("Key: {0} Value: {1}", key, appSettings[key]); } } } catch (ConfigurationErrorsException) { Console.WriteLine("Error reading app settings"); } } static void ReadSetting(string key) { try { var appSettings = ConfigurationManager.AppSettings; string result = appSettings[key] ??"Not Found"; Console.WriteLine(result); } catch (ConfigurationErrorsException) { Console.WriteLine("Error reading app settings"); } } static void AddUpdateAppSettings(string key, string value) { try { var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var settings = configFile.AppSettings.Settings; if (settings[key] == null) { settings.Add(key, value); } else { settings[key].Value = value; } configFile.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name); } catch (ConfigurationErrorsException) { Console.WriteLine("Error writing app settings"); } } } } |
另一种选择是使用"设置"。
由于一致性,我个人更喜欢使用XML配置文件,或者使用JSON设置Web应用程序。
INI格式确实提供的一件事是非技术人员也可以读取。
尽管如此,如果您对使用INI格式感兴趣,那么我建议您尝试一下我的INI库。它可以简化您处理INI文件的任务。
另外,我建议您看一下解析和映射功能,简而言之,它们将使您能够将" 0"值映射为" false",将" 1"值映射为" true",然后可以在其中检索这些值。 INI键中的布尔类型。请参见以下示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | IniFile ini = new IniFile(); ini.Load(@"C:\\Files\\Sample.ini"); ini.ValueMappings.Add("0", false); ini.ValueMappings.Add("1", true); IniSection section = ini.Sections["Client"]; bool installChrome; section.Keys["Install.Browser.Chrome"].TryParseValue(out installChrome); bool installJava; section.Keys["Install.Java"].TryParseValue(out installJava); Console.WriteLine(installChrome); Console.WriteLine(installJava); |