人々はここに設定ファイルの全体のポイントを言っているのと同じように、アプリケーションの外にいくつかの設定を変更するためです。ハードコードや定数の使用は可能ですが、必要に応じてWindowsでレジストリを使用することもできます。そうすれば、アプリケーションに変更を加えることができますが、単一のexeファイルしか持てません。
コードプロジェクトには、レジストリからの読み込み、書き込み、削除に関する良い情報があります。 http://www.codeproject.com/KB/system/modifyregistry.aspx ただし、レジストリを編集するときは注意してください。アプリケーションの多くはそれに依存しているので、何か問題があればいくつかの設定を破壊する可能性があります。私は読んで、次にやります。
public string Read(string KeyName) {
RegistryKey rk = baseRegistryKey;
// Open a subKey as read-only
RegistryKey sk1 = rk.OpenSubKey(subKey);
// If the RegistrySubKey doesn't exist -> (null)
if (sk1 == null)
{
return null;
}
else
{
try
{
// If the RegistryKey exists I get its value
// or null is returned.
return (string)sk1.GetValue(KeyName.ToUpper());
}
catch (Exception e)
{
// AAAAAAAAAAARGH, an error!
ShowErrorMessage(e, "Reading registry " + KeyName.ToUpper());
return null;
}
}
}
public bool Write(string KeyName, object Value) {
try
{
// Setting
RegistryKey rk = baseRegistryKey ;
// I have to use CreateSubKey
// (create or open it if already exits),
// 'cause OpenSubKey open a subKey as read-only
RegistryKey sk1 = rk.CreateSubKey(subKey);
// Save the value
sk1.SetValue(KeyName.ToUpper(), Value);
return true;
}
catch (Exception e) {
// AAAAAAAAAAARGH, an error!
ShowErrorMessage(e, "Writing registry " + KeyName.ToUpper());
return false;
}
}
public bool DeleteKey(string KeyName) {
try
{
// Setting
RegistryKey rk = baseRegistryKey ;
RegistryKey sk1 = rk.CreateSubKey(subKey);
// If the RegistrySubKey doesn't exists -> (true)
if (sk1 == null)
return true;
else
sk1.DeleteValue(KeyName);
return true;
}
catch (Exception e)
{
// AAAAAAAAAAARGH, an error!
ShowErrorMessage(e, "Deleting SubKey " + subKey);
return false;
}
}
もちろん、これはWindowsでのみ機能します。私はおそらくあなたがWindowsを使用しているので、Visual Studioを使用していると仮定します。
ハッピーコーディングと幸運!
AFAIK設定ファイルをexeに含める方法はありません。設定が単純な場合は、自分のファイルに保存し、最初の実行時にそのファイルをデフォルト設定で作成するだけです。もう一つのオプションはWindowsのレジストリですが、それからVista上で昇格する必要があります(これは難しくありません) – gideon
[組み込みリソースとしての設定ファイル](http://stackoverflow.com/questions/462583)/configuration-file-as-embedded-resource) –
少し考えてみてください。可能であれば、ユーザーは接続文字列を変更できなくなりました。 1つのポイントを破る。 1つのファイルは決して問題ではない、それはsetup.exe –