.Net Applikation für den PC um den Arduino-FSRemotePowerSwitch zu steuern
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace FSRemotePowerSwitch {
  7. public class PropertiesHandler {
  8. private Dictionary<String, String> list;
  9. private String filename;
  10. public PropertiesHandler(String file) {
  11. reload(file);
  12. }
  13. public String get(String field, String defValue) {
  14. return (get(field) == null) ? (defValue) : (get(field));
  15. }
  16. public String get(String field) {
  17. return (list.ContainsKey(field)) ? (list[field]) : (null);
  18. }
  19. public void set(String field, Object value) {
  20. if (!list.ContainsKey(field))
  21. list.Add(field, value.ToString());
  22. else
  23. list[field] = value.ToString();
  24. }
  25. public void Save() {
  26. Save(this.filename);
  27. }
  28. public void Save(String filename) {
  29. this.filename = filename;
  30. if (!System.IO.File.Exists(filename))
  31. System.IO.File.Create(filename);
  32. System.IO.StreamWriter file = new System.IO.StreamWriter(filename);
  33. foreach (String prop in list.Keys.ToArray())
  34. if (!String.IsNullOrWhiteSpace(list[prop]))
  35. file.WriteLine(prop + "=" + list[prop]);
  36. file.Close();
  37. }
  38. public void reload() {
  39. reload(this.filename);
  40. }
  41. public void reload(String filename) {
  42. this.filename = filename;
  43. list = new Dictionary<String, String>();
  44. if (System.IO.File.Exists(filename))
  45. loadFromFile(filename);
  46. else
  47. System.IO.File.Create(filename);
  48. }
  49. private void loadFromFile(String file) {
  50. foreach (String line in System.IO.File.ReadAllLines(file)) {
  51. if ((!String.IsNullOrEmpty(line)) &&
  52. (!line.StartsWith(";")) &&
  53. (!line.StartsWith("#")) &&
  54. (!line.StartsWith("'")) &&
  55. (line.Contains('='))) {
  56. int index = line.IndexOf('=');
  57. String key = line.Substring(0, index).Trim();
  58. String value = line.Substring(index + 1).Trim();
  59. if ((value.StartsWith("\"") && value.EndsWith("\"")) ||
  60. (value.StartsWith("'") && value.EndsWith("'"))) {
  61. value = value.Substring(1, value.Length - 2);
  62. }
  63. try {
  64. //ignore dublicates
  65. list.Add(key, value);
  66. } catch { }
  67. }
  68. }
  69. }
  70. }
  71. }