A config file for a .NET application is a text file that has a name of myapplication.exe.config.

The Windows CE does not make things easy for you! It allows you to add a file named “App.config” to your project and it will copy it to the appropriate bin directory and rename it to myapplication.exe.config, but you cannot access it.

App.condig is nothing more than XML. Here’s my version to access config file by Windows CE Application :

  1
  2using System;
  3using System.Collections.Generic;
  4using System.Collections.Specialized;
  5using System.IO;
  6using System.Linq;
  7using System.Reflection;
  8using System.Text;
  9using System.Xml;
 10
 11namespace SmartDevice.Utils
 12{
 13    public static class AppSettings
 14    {
 15        #region Public Static Members
 16
 17        public static NameValueCollection Settings = new NameValueCollection();
 18        public static bool _loaded = false;
 19
 20        #endregion Public Static Members
 21
 22        #region Private Static Members
 23
 24        //static string configFile = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase), "App.config");
 25        static string configFile = string.Format("{0}.{1}", Assembly.GetExecutingAssembly().GetName().CodeBase, "config");
 26        static bool _isAppConfigExist = false;
 27
 28        #endregion Private Static Members
 29
 30        #region Static Constructors
 31
 32        static AppSettings()
 33        {
 34            if (File.Exists(configFile))
 35            {
 36                isAppConfigExist = true;
 37                Load();
 38            }
 39            else
 40            {
 41                isAppConfigExist = false;
 42            }
 43        }
 44
 45        #endregion Static Constructors
 46
 47        #region Public Static Properties
 48
 49        public static bool isAppConfigExist
 50        {
 51            get
 52            {
 53                return _isAppConfigExist;
 54            }
 55            private set
 56            {
 57                _isAppConfigExist = value;
 58            }
 59        }
 60
 61        #endregion Public Static Properties
 62
 63        #region Public Static Methods
 64
 65        public static string GetFile()
 66        {
 67            StreamReader streamReader = System.IO.File.OpenText(configFile);
 68            string ret = streamReader.ReadToEnd();
 69            streamReader.Close();
 70            return ret;
 71        }
 72
 73        public static string GetValue(string key, string default_value)
 74        {
 75            if (!_loaded) Load();
 76            return isKeyOK(key) ? Settings.Get(key) : default_value;
 77        }
 78
 79        public static void Load()
 80        {
 81            if (!File.Exists(configFile))
 82            {
 83                throw new FileNotFoundException(string.Format("Application configuration file '{0}' not found.", configFile));
 84            }
 85
 86            XmlDocument xmlDocument = new XmlDocument();
 87            xmlDocument.Load(configFile);
 88            XmlNodeList nodeList = xmlDocument.GetElementsByTagName("appSettings");
 89            Settings = new NameValueCollection();
 90
 91            foreach (XmlNode node in nodeList)
 92            {
 93                foreach (XmlNode key in node.ChildNodes)
 94                {
 95                    Settings.Add(key.Attributes["key"].Value, key.Attributes["value"].Value);
 96                }
 97            }
 98            _loaded = true;
 99        }
100
101        public static void Save()
102        {
103            System.Text.StringBuilder sb = new StringBuilder();
104            sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n\r\n<configuration><appSettings>\r\n</appSettings>\r\n</configuration>");
105            XmlDocument xmlDocument = new XmlDocument();
106            xmlDocument.LoadXml(sb.ToString());
107
108            XmlNode root = xmlDocument.DocumentElement.ChildNodes[0];
109
110            for (int i = 0; i < Settings.Count; ++i)
111            {
112                XmlElement addElement = xmlDocument.CreateElement("add");
113                string key = Settings.GetKey(i);
114                string value = Settings.GetValues(i)[0].ToString();
115                addElement.SetAttribute("key", key);
116                addElement.SetAttribute("value", value);
117                root.AppendChild(addElement);
118            }
119            xmlDocument.Save(configFile);
120        }
121
122        public static void SetValue(string key, string newValue)
123        {
124            if (isKeyOK(key))
125                Settings.Set(key, newValue);
126            else
127                Settings.Add(key, newValue);
128        }
129
130        #endregion Public Static Methods
131
132        #region Private Static Methods
133
134        private static bool isKeyOK(string key)
135        {
136            if (!Settings.HasKeys()) return false;
137            key = key.ToLower();
138            if (Settings.AllKeys.Count(p => p.ToLower() == key) == 0) return false;
139            if (Settings.AllKeys.Count(p => p.ToLower() == key) != 1)
140            {
141                //string configFile = Path.Combine(appPath, "App.config");
142                throw new FileNotFoundException(string.Format("Application configuration file '{0}' had an error. Double key: ", configFile, key));
143            }
144            return true;
145        }
146
147        #endregion Private Static Methods
148    }
149}

Source: https://social.msdn.microsoft.com/Forums/vstudio/en-US/d68a872e-14bc-414a-82c4-d1035a11b4a8/how-do-i-updateinsertremove-the-config-file-during-runtime?forum=csharpgeneral http://stackoverflow.com/questions/20614189/create-app-config-file-if-doesnt-exist-c-sharp http://ryanfarley.com/blog/archive/2004/07/13/879.aspx http://www.codeproject.com/Articles/12589/Modifying-Configuration-Settings-at-Runtime http://stackoverflow.com/questions/5468342/how-to-modify-my-app-exe-config-keys-at-runtime http://stackoverflow.com/questions/6402596/xml-string-to-xml-document http://www.tutorialized.com/tutorial/Add-remove-elements-to-from-an-XML-Document-using-C/45101 https://msdn.microsoft.com/en-us/library/system.xml.xmldocument.createcomment%28v=vs.110%29.aspx