110,571
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration; //要添加对System.Configuration程序集的引用,在引用或者Bin文件夹上按右键选择添加引用。
namespace CtrlTest
{
/// <summary>
/// 对exe.Config文件中的appSettings段进行读写配置操作
/// 注意:调试时,写操作将写在vhost.exe.config文件中
/// </summary>
class MyConfiguration
{
static object configVar = new object();
public static bool SaveConfig(string key, string value)
{
try
{
lock (configVar)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (config.AppSettings.Settings[key] == null)
{
config.AppSettings.Settings.Add(key, value);
}
else
{
config.AppSettings.Settings[key].Value = value;
}
config.Save();
//config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件
}
return true;
}
catch
{
return false;
}
}
///<summary>
///在*.exe.config文件中appSettings配置节增加一对键、值对
///</summary>
///<param name="newKey"></param>
///<param name="newValue"></param>
public static void UpdateAppConfig(string newKey, string newValue)
{
bool isModified = false;
foreach (string key in ConfigurationManager.AppSettings)
{
if (key == newKey)
{
isModified = true;
}
}
lock (configVar)
{
// Open App.Config of executable
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// You need to remove the old settings object before you can replace it
if (isModified)
{
config.AppSettings.Settings.Remove(newKey);
}
// Add an Application Setting.
config.AppSettings.Settings.Add(newKey, newValue);
// Save the changes in App.config file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");
}
}
public static string GetConfig(string key)
{
lock (configVar)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (config.AppSettings.Settings[key] == null)
return "";
return config.AppSettings.Settings[key].Value;
//return ConfigurationManager.AppSettings[key];
}
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Lexium23dPort" value="COM2"></add>
<add key="MagPort" value="COM3"></add>
<add key="FanPort" value="COM4"></add>
<add key="MaxonHorPort" value="COM5"></add>
<add key="MaxonVerPort" value="COM6"></add>
<add key="JoyStickLexium23dPort" value="COM8"></add>
<add key="JoyStickMaxonPort" value="COM7"></add>
<add key="Lexium23dXNorLoad" value="70"></add>
<add key="Lexium23dYNorLoad" value="50"></add>
<add key="Lexium23dZNorLoad" value="120"></add>
<add key="MaxonVerNorCurrent" value="520"></add>
<add key="MaxonHorNorCurrent" value="300"></add>
<add key="MaxonVelDValue" value="15"></add>
<add key="Lexium23dXLength" value="15"></add>
<add key="Lexium23dYLength" value="15"></add>
<add key="Lexium23dZLength" value="15"></add>
<add key="MaxonType" value="0"></add>
<add key="MaxonVerNewNorCurrent" value="300"></add>
<add key="MaxonHorNewNorCurrent" value="300"></add>
<add key="Lexium23dXCoordinate" value="15"></add>
<add key="Lexium23dYCoordinate" value="15"></add>
<add key="Lexium23dZCoordinate" value="15"></add>
</appSettings>
</configuration>