1

私はすぐにインストールの準備ができている WPF アプリケーションに取り組んでいますが、ユーザーが app.config ファイルを変更できるようにする機能があり、アプリケーションのコード ビハインドからそれを行う方法がわかりません。また、アプリのインストール後にこれがどのように機能するかわかりません。

簡単に言うと、別のアプリケーションの web.config で検索されるテキストをユーザーが入力できるウィンドウがあります。したがって、私の app.config には別の検索があり、インストール後にユーザーが必要です(ウィンドウの textoxes に値を入力した後) アプリケーションの app.config に新しい値を入力できるようにします。

それが可能かどうか、そして最終的にどうすればこれを達成できるかを誰かが教えてもらえますか?

4

2 に答える 2

2

私のアプリケーションでのやり方。私は次のようにapp.configを持っています

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="StoreConnectionString"
          connectionString="Data Source=.\;MultipleActiveResultSets=True;Initial Catalog=Store;Integrated Security=False;"
          providerName="System.Data.SqlClient" />
    </connectionStrings>

    <appSettings>
        <add key="ExportPath" value="D:\" />
        <add key="CompanyName" value="My Company" />
        <add key="mail" value="email@mail.com" />
        <add key="phone" value="+992918254040" />
        <add key="ExpDate" value="Pink" />
        <add key="Print" value="No" />
        <add key="EnforcePassw" value="Yes"/>
    </appSettings>
</configuration>

したがって、アプリケーションからアプリの設定を変更して保存できます。コードは次のとおりです

private void btnSave(object sender, RoutedEventArgs e)
        {
            //returns path of folder where your application is
            string appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            //combines path of folder and file
            string configFile = System.IO.Path.Combine(appPath, "MyApp.exe.config");
            //Defines the configuration file mapping for an .exe application
            ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
            configFileMap.ExeConfigFilename = configFile;
            System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

            config.AppSettings.Settings["ExportPath"].Value = txtExport.Text;
            config.AppSettings.Settings["CompanyName"].Value = txtComapny.Text;
            config.AppSettings.Settings["mail"].Value = txtEmail.Text;
            config.AppSettings.Settings["phone"].Value = txtPhone.Text;
            config.AppSettings.Settings["Print"].Value = print;
            config.AppSettings.Settings["EnforcePassw"].Value = password;
            config.AppSettings.Settings["ExpDate"].Value = color;
            config.Save(); 

        }

それがあなたを助けることを願っています!

新しい文字列を追加したい場合は、このコードを使用してください。

        config.AppSettings.Settings.Add("Key", "Value");
于 2013-06-17T12:47:31.810 に答える
0

これは、WPF でアプリケーション/ユーザー設定を簡単に構成するのに役立つ場合があります 。

于 2013-06-17T11:52:45.283 に答える