using System.Configuration
Configuration _config;
string _paraPath = "Path";
public MainWindow()
{
InitializeComponent();
this.Dispatcher.ShutdownStarted += Dispatcher_ShutdownStarted;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
InitConfig();
// E:\Repository\****.csproj
string filePath = _config.AppSettings.Settings[_paraPath].Value;
GetData(filePath);
}
private void Dispatcher_ShutdownStarted(object sender, EventArgs e)
{
_config.AppSettings.Settings[_paraPath].Value = txtDir.Text;
_config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
private void InitConfig()
{
try
{
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
Assembly assembly = Assembly.GetCallingAssembly();
Uri uri = new Uri(Path.GetDirectoryName(assembly.CodeBase));
string configName = Path.Combine(uri.LocalPath, assembly.GetName().Name + ".exe.config");
if (!File.Exists(configName))
{
XmlDocument xml = new XmlDocument();
xml.AppendChild(xml.CreateXmlDeclaration("1.0", "utf-8", null));
xml.AppendChild(xml.CreateElement("configuration"));
xml.Save(configName);
map.ExeConfigFilename = configName;
_config = ConfigurationManager.OpenMappedExeConfiguration(map, 0);
_config.AppSettings.Settings.Add(_paraPath, @"E:\Repository\****.csproj");
}
else
{
map.ExeConfigFilename = configName;
_config = ConfigurationManager.OpenMappedExeConfiguration(map, 0);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void GetData(string filePath)
{
txtDir.Text = filePath;
}