var config = new MiniConfig("setting.conf");
config.AddOrUpdate("port", "1580");
if (config.TryGet("port", out int port)) // if config exist
{
Console.Write(port);
}
public class DLLConfig
{
public static string GetSettingByKey(AppDomain currentDomain, string configName, string key)
{
string value = string.Empty;
try
{
string exeConfigPath = (currentDomain.RelativeSearchPath ?? currentDomain.BaseDirectory) + "\\" + configName;
if (File.Exists(exeConfigPath))
{
using (Stream stream = File.OpenRead(exeConfigPath))
{
XDocument xdoc = XDocument.Load(stream);
XElement element = xdoc.Element("configuration").Element("appSettings").Elements().First(a => a.Attribute("key").Value == key);
value = element.Attribute("value").Value;
}
}
}
catch (Exception ex)
{
}
return value;
}
}
像这样在库类中使用它;
namespace ProjectName
{
public class ClassName
{
public static string SomeStaticMethod()
{
string value = DLLConfig.GetSettingByKey(AppDomain.CurrentDomain,"ProjectName.dll.config", "keyname");
}
}
}