だから私はここ数日間、ここSOとグーグルの両方で についての情報を探していましたapp.config.
ユーザーが入力した値を使用して SQL スクリプトを生成する必要があるプログラムを作成しています。もともとapp.config、最初の起動時にプログラムにロードするデフォルト値を保存するために使用していました。app.config新しい値をファイルに保存しようとするまで、これはうまくいきました。これは、app.configが読み取り専用であり、 を使用する必要があることを発見したときuser.configです。
答えが見つからないように見えるいくつかの質問があります。
settings.Setting使用したいすべての値を宣言するために使用することをお勧めしますapp.configか? それとも手入力で十分ですか?user.config設定を上書きする方法について読み続けていapp.configます。しかし、user.configファイルを更新すると、プログラムはまだ元のapp.configファイルから読み取ります
これは私のラッパークラスからのものです
public NameValueCollection ReadSettings(string sectionName)
{
NameValueCollection scripts = null;
try
{
//read in the current values from the section
scripts = (NameValueCollection)ConfigurationManager.GetSection(sectionName);
if (scripts == null) throw new appConfigException(String.Format("The section {0} does not exists in app.config", sectionName));
}catch (Exception e){
//print out the log file
StreamWriter writer = new StreamWriter(DateTime.Now.ToString("d-MMM-yyyy") + "log.txt");
writer.WriteLine(e.ToString());
writer.Close();
//kill the application process so the user cannot advance further
System.Diagnostics.Process.GetCurrentProcess().Kill();
}
return scripts;
}
ConfigurationManagerから読み取りを開始することを自動的に認識することになっていますかuser.config? または、それを反映するためにコードのこのセクションを変更する必要がありますか?