コマンドラインパラメーターから.Netアプリケーション構成ファイルを選択するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/163803

質問

コマンドラインパラメーターを渡すことで、標準のapp.configの使用をオーバーライドしたいと思います。 ConfigurationManager.AppSettingsにアクセスするときにコマンドラインで指定された構成ファイルにアクセスするように、デフォルトのアプリケーション構成ファイルを変更するにはどうすればよいですか?

編集:

EXEと.configの名前とは異なる設定ファイルを読み込む正しい方法は、OpenMappedExeConfigurationを使用することです。例えば。

ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
configFile.ExeConfigFilename = Path.Combine(Environment.CurrentDirectory, "Shell2.exe.config");
currentConfiguration = ConfigurationManager.OpenMappedExeConfiguration(configFile,ConfigurationUserLevel.None);

これは部分的に機能します。 appSettingsセクションにすべてのキーが表示されていますが、すべての値がnullです。

役に立ちましたか?

解決

ここで、実際にデフォルト以外の設定ファイルのappSettingsセクションに実際にアクセスできるようにするコードを示します。

ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
configFile.ExeConfigFilename = Path.Combine(Environment.CurrentDirectory, "Alternate.config");
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFile,ConfigurationUserLevel.None);

AppSettingsSection section = (AppSettingsSection)config.GetSection("appSettings");
string MySetting = section.Settings["MySetting"].Value;

他のヒント

目的の構成ファイルをappname.exe.configにコピーし、appname.exeを実行するバッチファイル。

私のアプリでもこれを行う必要があり、標準の設定オブジェクトを扱うと、このルートに行ったようなシンプルなコンセプトのために、非常に面倒になりました:

  1. app.configと同様のXML形式で複数の構成ファイルを保持する
  2. 指定された構成ファイルを DataSet に(.ReadXMLを介して)ロードし、 DataTable に構成情報を構成オブジェクトとして使用します
  3. すべてのコードは、 Configuration DataTable を処理して値を取得しますが、その難解な難読化されたアプリ構成オブジェクトではありません。

その後、コマンドラインで必要な設定ファイル名を渡すことができます。設定ファイルがない場合は、app.configを DataSet にロードしてください。

Jeezusそれはその後ずっとすっごく簡単になりました。 :-)

ロン

これは、デフォルトの設定を使用し、コマンドライン経由でオーバーライドを受け入れるアプリのソースの関連部分です:

現在の構成またはユーザーの構成を構成オブジェクトに取得する

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
string defCfgName = Environment.GetCommandLineArgs()[0] + ".config";

if (arg.Length != 0)
{
    string ConfigFileName = arg[0];
    if (!File.Exists(ConfigFileName))
        Fatal("File doesn't exist: " + ConfigFileName, -1);                
    config = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = ConfigFileName }, ConfigurationUserLevel.None);
}
else if (!File.Exists(defCfgName)) Fatal("Default configuration file doesn't exist and no override is set." , -1);

configオブジェクトを使用

AppSettingsSection s = (AppSettingsSection)config.GetSection("appSettings");
KeyValueConfigurationCollection a = s.Settings;
ConnectionString = a["ConnectionString"].Value;

これは、実際に ConfigurationManager 静的オブジェクトが別のパスを指すようにします。しかし、それはあなたの問題の正しい解決策だと思います。 OpenExeConfiguration メソッドをチェックしてください。 ConfigurationManager クラス。

上記の方法がお望みのものでない場合は、 Enterprise Libraryフレームワークの構成機能(Microsoft Patterns& Practicesチームによって開発および管理されています)。

特に FileConfigurationSource クラス。

FileConfigurationSource の使用を強調するコードを次に示します。 -99BE-F8ECFBBC5B65&displaylang = ja&Hash = oOfFascYgZ%2bhSk6bMSD0ctdKGJBo3jL9XEtSJS2%2bCaArLTPNHRCuJ5k%2bnhgG8YkFIMOdGq74rreerthisrefernreerhererefer \ rrel> rreferrreferer = "require"このためにEnt Libから必要な唯一のアセンブリは、 Microsoft.Practices.EnterpriseLibrary.Common.dll です。

static void Main(string[] args)
{
    //read from current app.config as default
    AppSettingsSection ass = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSettings;

    //if args[0] is a valid file path assume it's a config for this example and attempt to load
    if (args.Length > 0 && File.Exists(args[0]))
    {
        //using FileConfigurationSource from Enterprise Library
        FileConfigurationSource fcs = new FileConfigurationSource(args[0]);
        ass = (AppSettingsSection) fcs.GetSection("appSettings");
    }

    //print value from configuration
    Console.WriteLine(ass.Settings["test"].Value);
    Console.ReadLine(); //pause
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top