我想从类库项目中读取app.cofig文件 [英] I want to read app.cofig file from a class library project

查看:85
本文介绍了我想从类库项目中读取app.cofig文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个使用app.config文件的类库类型项目,但我无法访问该类库的配置文件。





我在配置对象中得到null



< b>我尝试过:



public static Dictionary< string,> GetConfig()

{

var enforcementAssembly = System.Reflection.Assembly.GetExecutingAssembly();

var location = enforcementAssembly.Location;

System.Windows.Forms.MessageBox.Show(location);

var config = System.Configuration.ConfigurationManager.OpenExeConfiguration(location);

);



object paypalConfigSection = null;



try

{

if(config!= null)

{

var sections = config.Sections; //这个的数量是21

paypalConfigSection = sections;

}



}

catch(System.Exception ex)

{

抛出新的ConfigException(无法从* .config加载'paypal'部分:+ ex.Message) ;

}



if(paypalConfigSection == null)

{

抛出新的ConfigException(

无法解析* .Config文件。确保你已正确配置'paypal'部分。);

}



System.Configuration.NameValueConfigurationCollection settings =(System.Configuration.NameValueConfigurationCollection)paypalConfigSection.GetType()。GetProperty(Settings)。GetValue(paypalConfigSection,null);

configValues = new Dictionary< string,> ;();

foreach(设置中的System.Configuration.NameValueConfigurationElement设置)

{

configValues.Add(setting.Name,setting。价值);

}



int index = 0;

foreach(System.Configuration.ConfigurationElement元素in (System.Configuration.ConfigurationElementCollection)paypalConfigSection.GetType()。GetProperty(Accounts)。GetValue(paypalConfigSection,null))

{

账户账户=(账户)元素;

if(!string.IsNullOrEmpty(account.APIUserName))

{

configValues.Add(account+ index + .apiUsername,account.APIUserName);

}

if(!string.IsNullOrEmpty(account.APIPassword))

{

configValues.Add(account + index +。apiPassword,account.APIPassword);

}

if(!string.IsNullOrEmpty(account.APISignature))

{

configValues.Add(account+ index +。apiSignature,account.APISignature);

}

if(!string .IsNullOrEmpty(account.APICertificate))

{

configValues.Add(account+ index +。apiCertificate,account.APICertificate);

}

if(!string.IsNullOrEmpty(account.PrivateKeyPassword))

{

configValues.Add(account+ index +。privateKeyPassword,account.PrivateKeyPassword);

}

if(!string.IsNullOrEmpty(account.CertificateSubject))

{

configValues.Add(account+ i ndex +.subject,account.CertificateSubject);

}

if(!string.IsNullOrEmpty(account.ApplicationId))

{

configValues.Add(account+ index +。applicationId,account.ApplicationId);

}

index ++;

}

//返回ConfigManager.Instance.GetProperties();

返回新词典< string,>(configValues);

}

Hi,

I am having a class library type project that uses a app.config file but i am not able to access that configuration file for that class library.


I am getting null in the config object

What I have tried:

public static Dictionary<string,> GetConfig()
{
var executingAssembly = System.Reflection.Assembly.GetExecutingAssembly();
var location = executingAssembly.Location;
System.Windows.Forms.MessageBox.Show(location);
var config = System.Configuration.ConfigurationManager.OpenExeConfiguration(location);
);

object paypalConfigSection = null;

try
{
if (config != null)
{
var sections = config.Sections; //count of this is 21
paypalConfigSection = sections;
}

}
catch (System.Exception ex)
{
throw new ConfigException("Unable to load 'paypal' section from *.config: " + ex.Message);
}

if (paypalConfigSection == null)
{
throw new ConfigException(
"Cannot parse *.Config file. Ensure you have configured the 'paypal' section correctly.");
}

System.Configuration.NameValueConfigurationCollection settings = (System.Configuration.NameValueConfigurationCollection)paypalConfigSection.GetType().GetProperty("Settings").GetValue(paypalConfigSection, null);
configValues = new Dictionary<string,>();
foreach (System.Configuration.NameValueConfigurationElement setting in settings)
{
configValues.Add(setting.Name, setting.Value);
}

int index = 0;
foreach (System.Configuration.ConfigurationElement element in (System.Configuration.ConfigurationElementCollection)paypalConfigSection.GetType().GetProperty("Accounts").GetValue(paypalConfigSection, null))
{
Account account = (Account)element;
if (!string.IsNullOrEmpty(account.APIUserName))
{
configValues.Add("account" + index + ".apiUsername", account.APIUserName);
}
if (!string.IsNullOrEmpty(account.APIPassword))
{
configValues.Add("account" + index + ".apiPassword", account.APIPassword);
}
if (!string.IsNullOrEmpty(account.APISignature))
{
configValues.Add("account" + index + ".apiSignature", account.APISignature);
}
if (!string.IsNullOrEmpty(account.APICertificate))
{
configValues.Add("account" + index + ".apiCertificate", account.APICertificate);
}
if (!string.IsNullOrEmpty(account.PrivateKeyPassword))
{
configValues.Add("account" + index + ".privateKeyPassword", account.PrivateKeyPassword);
}
if (!string.IsNullOrEmpty(account.CertificateSubject))
{
configValues.Add("account" + index + ".subject", account.CertificateSubject);
}
if (!string.IsNullOrEmpty(account.ApplicationId))
{
configValues.Add("account" + index + ".applicationId", account.ApplicationId);
}
index++;
}
//return ConfigManager.Instance.GetProperties();
return new Dictionary<string,>(configValues);
}

推荐答案

您提供的位置是.dll文件,因此代码无法找到.config文件的位置。您可以尝试更改引用配置文件夹的位置字符串。
The location you are providing is of .dll file so the code is not able to find the location of the .config file. Can you try by changing the location string which refer to the config folder.


这篇关于我想从类库项目中读取app.cofig文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆