私のアプリケーションでのやり方。私は次のように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");