贴一个解析app.config内容的代码,抛砖引玉,注意这个ConfigurationManager.cs,要放在项目根路径下
全算上,50行吧using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Resources;
using System.Xml.Linq;namespace EcToEv.Util
{
/// <summary>
/// Access appSettings from a configuration file
/// </summary>
/// <res>Your appConfig file must be in the root of your applcation</res>
public static class ConfigurationManager
{
static ConfigurationManager()
{
AppSettings = new Dictionary<string, string>();
ReadSettings();
} public static Dictionary<string, string> AppSettings { get; set; }
private static void ReadSettings()
{
// Get the name of the executing assemby - we are going to be looking in the root folder for
// a file called app.config
string assemblyName = Assembly.GetExecutingAssembly().FullName;
assemblyName = assemblyName.Substring(0, assemblyName.IndexOf(','));
string url = String.Format("{0};component/app.config", assemblyName);
StreamResourceInfo configFile = Application.GetResourceStream(new Uri(url, UriKind.Relative));
if (configFile != null && configFile.Stream != null)
{
Stream stream = configFile.Stream;
XDocument document = XDocument.Load(stream);
foreach (XElement element in document.Descendants("appSettings").DescendantNodes())
{
AppSettings.Add(element.Attribute("key").Value, element.Attribute("value").Value);
}
}
}
}
}
全算上,50行吧using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Resources;
using System.Xml.Linq;namespace EcToEv.Util
{
/// <summary>
/// Access appSettings from a configuration file
/// </summary>
/// <res>Your appConfig file must be in the root of your applcation</res>
public static class ConfigurationManager
{
static ConfigurationManager()
{
AppSettings = new Dictionary<string, string>();
ReadSettings();
} public static Dictionary<string, string> AppSettings { get; set; }
private static void ReadSettings()
{
// Get the name of the executing assemby - we are going to be looking in the root folder for
// a file called app.config
string assemblyName = Assembly.GetExecutingAssembly().FullName;
assemblyName = assemblyName.Substring(0, assemblyName.IndexOf(','));
string url = String.Format("{0};component/app.config", assemblyName);
StreamResourceInfo configFile = Application.GetResourceStream(new Uri(url, UriKind.Relative));
if (configFile != null && configFile.Stream != null)
{
Stream stream = configFile.Stream;
XDocument document = XDocument.Load(stream);
foreach (XElement element in document.Descendants("appSettings").DescendantNodes())
{
AppSettings.Add(element.Attribute("key").Value, element.Attribute("value").Value);
}
}
}
}
}
但是在注册完B.exe到系统services内之后, 总是无法启动, 看事件日志说应用有0个终结点, 这可能是因为没有配置文件造成的, 它没有去读取app.config内的配置, 搞不懂啊.. 抓狂.