c# how to make app use a specified configure file

      //Main method

public static void Main(string[] args)
{
            if(args != null && args.Length > 0)
            {
                string clientConfigFile = args[0];

                 if(!File.Exists(Environment.CurrentDirectory  + "\\" + clientConfigFile))
                 {
                     throw new Exception("client config file: " + clientConfigFile + " does not exist!");
                 }

                ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
                fileMap.ExeConfigFilename = clientConfigFile;
                Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                AppSettingsSection section = (AppSettingsSection)config.GetSection("appSettings");
                Utils.Section = section;
            }
            else
            {
                throw new Exception("No config file specified!");
            }
}

</pre><p></p><p></p><p>       //Utils.cs</p><p>    <pre name="code" class="csharp">  public class Utils
	{
        public static AppSettingsSection Section = null;


        public static String ListURL
        {
            get
            {
                return Section.Settings[SourceName + "ListURL"].Value;
            }
        }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章