Newer
Older
TIASShot / TIASshot / Config.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Configuration;
using System.Windows.Documents;
using System.Xml;

namespace TIASshot {
    internal static class Config {
        static string configFile = "config.xml";
        static XmlDocument doc = new XmlDocument();

        /// <summary>
        /// 設定ファイルを読み込む
        /// </summary>
        public static void Load() {
            try {
                doc.Load(configFile);
            } catch (Exception) { 
            }
        }

        /// <summary>
        /// 設定ファイル読み込み判定
        /// </summary>
        /// <returns></returns>
        public static bool IsLoaded() {
            XmlNode node = doc.SelectSingleNode($"//Config");
            return node != null;
        }

        /// <summary>
        /// 設定ファイルからfloat値を取得
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static float GetFloat(string param) {
            float value = 0.0f;
            XmlNode node = doc.SelectSingleNode($"//Config/{param}");

            if (node != null && float.TryParse(node.InnerText, out float result)) {
                value = result;
            }
            return value;
        }

        /// <summary>
        /// 設定ファイルからint値を取得
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static int GetInt(string param) {
            int value = 0;
            XmlNode node = doc.SelectSingleNode($"//Config/{param}");

            if (node != null && int.TryParse(node.InnerText, out int result)) {
                value = result;
            }
            return value;
        }

        /// <summary>
        /// 設定ファイルから文字列を取得
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static string GetString(string param) {
            XmlNode node = doc.SelectSingleNode($"//Config/{param}");
            return node?.InnerText ?? "";
        }
    }
}