C# 主程序未运行时设置值为空
我已将程序设置值放在解决方案中名为C# 主程序未运行时设置值为空,c#,visual-studio,application-settings,C#,Visual Studio,Application Settings,我已将程序设置值放在解决方案中名为Common的单独项目中。我有一个主要项目(我们称之为a),还有一些其他项目(B和C),然后是一个普通项目通用添加到A、B和C的参考中。我已尝试使用以下两种方法访问公共设置值: Visual studio的设置。设置,很容易使用,但在阅读其他项目时很棘手 自制的ini文件,用于读取和写入 现在我意识到他们的行为方式是一样的,我想我错过了一个重要的观点。主项目运行时,可以访问设置值。当我更新我的设置时,它们会被保存,并且一切正常 但有时我需要在A未运行时从其他项目
Common
的单独项目中。我有一个主要项目(我们称之为a
),还有一些其他项目(B
和C
),然后是一个普通项目<代码>通用
添加到A
、B
和C
的参考中。我已尝试使用以下两种方法访问公共设置值:
Visual studio的设置。设置
,很容易使用,但在阅读其他项目时很棘手
自制的ini
文件,用于读取和写入
现在我意识到他们的行为方式是一样的,我想我错过了一个重要的观点。主项目运行时,可以访问设置值。当我更新我的设置时,它们会被保存,并且一切正常
但有时我需要在A
未运行时从其他项目访问这些值。在我的例子中,projectB
是由Windows服务触发的。然后,它从数据库中读取数据,然后执行批处理文件。但它还需要访问公共设置。它们是空字符串
<如果>我们考虑上面的第二种方法(从<代码> IN/<代码>中阅读)<代码>通用<代码>在其<代码> CONF静态对象(我用来读取和写入设置)中称为<代码>连接字符串。在主程序中,只需调用Common.Conf.ConnectionString
即可访问该值。但是当被B
使用时,此值为空。因此,我无法访问我的设置值
我最好创建一个函数,每当我需要这些值时从ini
文件中读取,并再次解析它们,就像公共项目不存在一样
但我还能做些什么来让它工作吗
这是我用来从ini
文件读/写的Common
的Conf
密封类的代码
namespace Common
{
public sealed class Conf
{
public static string ConnectionString { get; set; }
public static bool LoggerEnabled { get; set; }
public static string FolderLocation { get; set; }
public static string Delimeter { get; set; }
static readonly string SETTINGS = "conf.ini";
static readonly Conf instance = new Conf();
Conf() { }
static Conf()
{
string property = "";
string[] settings = File.ReadAllLines(SETTINGS);
foreach (string s in settings)
try
{
string[] split = s.Split(new char[] { ':' }, 2);
if (split.Length != 2)
continue;
property = split[0].Trim();
string value = split[1].Trim();
PropertyInfo propInfo = instance.GetType().GetProperty(property);
switch (propInfo.PropertyType.Name)
{
case "Int32":
propInfo.SetValue(null, Convert.ToInt32(value), null);
break;
case "String":
propInfo.SetValue(null, value, null);
break;
case "Boolean":
if (value == "1")
propInfo.SetValue(null, true, null);
else
propInfo.SetValue(null, false, null);
break;
}
}
catch
{
throw new Exception("Invalid setting '" + property + "'");
}
}
/// <summary>
/// Save new configuration in conf.ini
/// </summary>
public static void SaveConf()
{
StringBuilder sb = new StringBuilder();
PropertyInfo[] properties = typeof(Conf).GetProperties();
Type myType = typeof(Conf);
PropertyInfo[] propertyinfos = myType.GetProperties(
BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
foreach (PropertyInfo p in propertyinfos)
{
if (p.GetValue(myType, null) == null)
sb.AppendLine(string.Format("{0}:{1}", p.Name, string.Empty));
else
sb.AppendLine(string.Format("{0}:{1}", p.Name, p.GetValue(myType, null).ToString()));
}
string result = sb.ToString();
File.WriteAllText(SETTINGS, String.Empty);
File.WriteAllText(SETTINGS, result);
}
}
}
名称空间公用
{
公共密封类配置文件
{
公共静态字符串连接字符串{get;set;}
公共静态bool LoggerEnabled{get;set;}
公共静态字符串FolderLocation{get;set;}
公共静态字符串Delimeter{get;set;}
静态只读字符串设置=“conf.ini”;
静态readonly Conf instance=new Conf();
Conf(){}
静态配置()
{
字符串属性=”;
string[]settings=File.ReadAllLines(设置);
foreach(设置中的字符串s)
尝试
{
string[]split=s.split(新字符[]{':'},2);
如果(拆分长度!=2)
持续
属性=拆分[0]。修剪();
字符串值=拆分[1]。修剪();
PropertyInfo-propInfo=instance.GetType().GetProperty(属性);
开关(propInfo.PropertyType.Name)
{
案例“Int32”:
propInfo.SetValue(null,Convert.ToInt32(value),null);
打破
大小写“字符串”:
propInfo.SetValue(null,value,null);
打破
案例“布尔”:
如果(值=“1”)
设置值(null、true、null);
其他的
propInfo.SetValue(null、false、null);
打破
}
}
接住
{
抛出新异常(“无效设置“+”属性“+””);
}
}
///
///在conf.ini中保存新配置
///
公共静态void SaveConf()
{
StringBuilder sb=新的StringBuilder();
PropertyInfo[]properties=typeof(Conf).GetProperties();
类型myType=typeof(Conf);
PropertyInfo[]PropertyInfo=myType.GetProperties(
BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
foreach(PropertyInfo中的PropertyInfo p)
{
if(p.GetValue(myType,null)==null)
sb.AppendLine(string.Format(“{0}:{1}”,p.Name,string.Empty));
其他的
AppendLine(string.Format(“{0}:{1}”,p.Name,p.GetValue(myType,null.ToString());
}
字符串结果=sb.ToString();
File.WriteAllText(设置,String.Empty);
File.WriteAllText(设置、结果);
}
}
}
我要做的是拥有一个自制的设置对象,将其序列化并作为.xml文件持久化。通过一个公共模块中的一组方法实现设置对象的读(反序列化)和写(序列化)接口。设置对象是一个简单的.Net类/对象,可以简单地通过引用传递。@RenniePet好的设置。设置
最终被反序列化为xml
文件,对吗?我已经在使用ini
文件,除了它不是xml
之外,使用的方式基本相同。