示例#1
0
        /// <summary>
        /// Loads the ini file and stores all settings within the global PlatformSettings instance.
        /// </summary>
        private static void LoadIniFile()
        {
            string procName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
            string iniName  = $"{procName}.ini".ToLowerInvariant();

            string        currentDirectoryPath = Directory.GetCurrentDirectory();
            DirectoryInfo directoryInfo        = new DirectoryInfo(currentDirectoryPath);
            FileInfo      iniFile = directoryInfo.GetFiles(iniName).FirstOrDefault();

            if (iniFile == null)
            {
                throw new InvalidOperationException(string.Format("No ini file '{1}' could be found within the current working dir '{0}'", currentDirectoryPath, iniName));
            }

            IniDocumentParser  iniDocumentParser = new IniDocumentParser(iniFile);
            IPlainTextDocument document          = iniDocumentParser.Parse();
            PlatformSettings   platformSettings  = PlatformSettings.Instance;

            using (IEnumerator <KeyValuePair <string, string[]> > itr = document.GetEnumerator()) {
                while (itr.MoveNext())
                {
                    KeyValuePair <string, string[]> pair = itr.Current;
                    string   key    = pair.Key;
                    string[] values = pair.Value;
                    string   value  = (values.Length == 0 ? string.Empty : values[0]);
                    platformSettings.Add(key, value);

                    // Make settings for all components available
                    Environment.SetEnvironmentVariable($"motoi:{key}", value, EnvironmentVariableTarget.Process);
                }
            }
        }
示例#2
0
        public void Parse()
        {
            const string fileContent = "application: motoi.test\r\n" +
                                       "nl: de";

            MemoryStream       stream   = new MemoryStream(Encoding.Default.GetBytes(fileContent));
            IniDocumentParser  parser   = new IniDocumentParser(stream);
            IPlainTextDocument document = parser.Parse();

            stream.Dispose();

            string applicationValue = document.SelectValue("application");
            string nlValue          = document.SelectValue("nl");

            Assert.AreEqual("motoi.test", applicationValue, "Wrong application value");
            Assert.AreEqual("de", nlValue, "Wrong nl value");
        }