3

私は Visual Studio の初心者で、app.config ファイルを扱っています。ちょっとしたヒントをお聞きしたいのですが、Windows フォームを使用して app.config ファイルで値キーを数回更新する最良の方法は何ですか。これまでのところ、私はこれを試しました:

Form1 が閉じられる直前に、次のコードで値を更新します。

Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(Application.StartupPath & "\MyProyect.exe")
Dim aps As AppSettingsSection = config.AppSettings 
aps.Settings.Item("SomeKey").Value = 5 'just an example
config.Save(ConfigurationSaveMode.Modified)

次に、次のフォームが次のように開かれます。

Form1.Hide()
Form2.Show() 

しかし、新しい Form2 の同じキーに値を再度保存しようとすると、プログラムがフリーズしてエラーが発生します。

構成ファイルが別のプログラムによって変更されています。 (C:\Users\RH\Documents\Visual Studio 2010\Projects\MyProyect\MyProyect\bin\Debug\MyProyect.exe.config)

本当に解決策を探していますが、この種の問題を抱えているのは私だけのようです。言ってしまえば、ただの初心者です。アドバイスをお願いできますか?

4

2 に答える 2

1

あなたの問題はこれだと思います。メソッドのドキュメントを確認するconfig.Saveと、このステートメントがあります。

この Configuration オブジェクトの作成後に構成ファイルが変更された場合、実行時エラーが発生します。

SaveConfigurationファイルを変更するので、オブジェクトのインスタンスごとに 1 回しか save メソッドを呼び出せないと思います。だから、これは私がこれを信じるように導きます、

Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(Application.StartupPath & "\MyProyect.exe")
Dim aps As AppSettingsSection = config.AppSettings 
aps.Settings.Item("SomeKey").Value = 5 'just an example
config.Save(ConfigurationSaveMode.Modified)
aps.Settings.Item("SomeKey").Value = 15 'just an example
config.Save(ConfigurationSaveMode.Modified)

2回目の保存では失敗しますが、これは、

Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(Application.StartupPath & "\MyProyect.exe")
Dim aps As AppSettingsSection = config.AppSettings 
aps.Settings.Item("SomeKey").Value = 5 'just an example
config.Save(ConfigurationSaveMode.Modified)
'reopen
config = ConfigurationManager.OpenExeConfiguration(Application.StartupPath & "\MyProyect.exe")
aps = config.AppSettings 
aps.Settings.Item("SomeKey").Value = 15 'just an example
config.Save(ConfigurationSaveMode.Modified)

成功するでしょう。

于 2012-12-06T19:43:39.047 に答える
1

ユーザーが構成可能な値を保存しようとしていますか? その場合、最良のケースは設定ファイルを使用することです。これは app.config ファイルに似ていますが、アプリケーションの実行中に更新可能です。実際、*.settings ファイルに入力した値は app.config ファイルに挿入されますが、読み取りと更新のプロセスはアプリケーションによって管理されます。

ユーザーがフォルダーからファイルを読み取れるようにするアプリケーションがあり、最後のフォルダーの場所を設定ファイルに保存しています。次にアプリケーションを実行するときに、その特定のユーザーの値を再度読み取ることができます。

C# のコード例を次に示します。

//read the property on load    
if (Properties.Settings.Default["FileLocation"] != null 
    && !string.IsNullOrWhiteSpace(Properties.Settings.Default["FileLocation"].ToString()))
{
    DirectoryInfo dirInfo 
        = new DirectoryInfo(Properties.Settings.Default["FileLocation"].ToString());

    if (dirInfo.Exists)
        dlg.InitialDirectory = dirInfo.FullName;
}

//code removed for clarity
//....

//save on exit from method
FileInfo fi = new FileInfo(dlg.FileName);
Properties.Settings.Default["FileLocation"] = fi.DirectoryName;
Properties.Settings.Default.Save();

私はそれを VB.Net に翻訳しましたが、しばらく VB.Net をやっていなかったので、前もってお詫び申し上げます。:-D

'read the property on load    
If (Properties.Settings.Default["FileLocation"] IsNot Nothing _
    AndAlso Not string.IsNullOrWhiteSpace(Properties.Settings.Default["FileLocation"].ToString())) Then
    Dim dirInfo as DirectoryInfo _
        = new DirectoryInfo(Properties.Settings.Default["FileLocation"].ToString())

    if (dirInfo.Exists) Then
        dlg.InitialDirectory = dirInfo.FullName
    End If
End If

'code removed for clarity
'....

'save on exit from method
Dim fi as FileInfo = new FileInfo(dlg.FileName)
Properties.Settings.Default["FileLocation"] = fi.DirectoryName
Properties.Settings.Default.Save()
于 2012-12-06T20:04:50.903 に答える