我想开发一个App,它应该从文件夹(包括子文件夹)中的所有配置文件中读取Connectionstring。我为此开发了win form App。
private void btnConnString_Click(object sender, EventArgs e)
{
var files = Directory.GetFiles(
this.txtPath.Text.Trim(),
"*.config",
SearchOption.AllDirectories);
foreach (string filepath in files)
{
string fileName;
fileName = Path.GetFileName(filepath);
}
}
现在我必须读取连接字符串。我该怎么办?
答案 0 :(得分:3)
你应该尝试这种方法:
ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
configMap.ExeConfigFilename = fileName;
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
string connString = config.ConnectionStrings.ConnectionStrings["ConnectionName"];
希望帮助!
答案 1 :(得分:1)
System.Configuration.Configuration rootWebConfig =
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/MyWebSiteRoot");
System.Configuration.ConnectionStringSettings connString;
if (rootWebConfig.ConnectionStrings.ConnectionStrings.Count > 0)
{
connString =
rootWebConfig.ConnectionStrings.ConnectionStrings["NorthwindConnectionString"];
if (connString != null)
Console.WriteLine("Northwind connection string = \"{0}\"",
connString.ConnectionString);
else
Console.WriteLine("No Northwind connection string");
}
答案 2 :(得分:0)
string fileName;
fileName = Path.GetFileName(filepath);
MessageBox.Show(filepath);
ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
configMap.ExeConfigFilename = fileName;
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
System.Configuration.ConnectionStringSettings connString = config.ConnectionStrings.ConnectionStrings["ConnectionString"];
if(connString!= null)
{
MessageBox.Show(connString.ConnectionString);
}
else
MessageBox.Show("No ConnectionString");
我添加了这个,我的连接字符串中有一个条目,但它总是采用空值
答案 3 :(得分:0)
class AppConfigurationReader
{
public List<string> key1;
public List<string> key2;
public int ConnectionStringsFound;
public int GetTotalConnectionStringsFound()
{
ConnectionStringsFound = key1.Count();
return (ConnectionStringsFound);
}
public AppConfigurationReader()
{
key1= new List<string>();
key12 = new List<string>();
ConnectionStringsFound = 0;
}
public void getAppConfigconnectionStrings(string webConfigPath)
{
DataTable dtKeyValue = new DataTable();
dtKeyValue.TableName = "app.config connectionString";
dtKeyValue.Columns.Add("name");
dtKeyValue.Columns.Add("connectionString");
XmlTextReader reader = new XmlTextReader(webConfigPath);
XmlDocument xdoc = new XmlDocument();
xdoc.Load(reader);
reader.Close();
XmlElement root = xdoc.DocumentElement;
XmlNodeList appSettings = xdoc.SelectNodes("/configuration/connectionStrings/add");
foreach (XmlNode node in appSettings)
{
DataRow row = dtKeyValue.NewRow();
String name = node.Attributes["name"].Value.ToString();
String connectionString = node.Attributes["connectionString"].Value.ToString();
//row["name"] = name;
//row["connectionString"] = connectionString;
//dtKeyValue.Rows.Add(row);
if (String.Equals(name, "Mykey1") == true)
{
key1.Add(connectionString);
}
else if (String.Equals(name, "Mykey2") == true)
{
key2.Add(connectionString);
}
}
//return dtKeyValue;
}
}
答案 4 :(得分:0)
//试试这个对我有用
ExeConfigurationFileMap Map = new ExeConfigurationFileMap();
Map.ExeConfigFilename = fileName;
Configuration configurationManager = ConfigurationManager.OpenMappedExeConfiguration(Map , ConfigurationUserLevel.None);
string connectionString = configurationManager.ConnectionStrings.ConnectionStrings["YOUR CONNECTION NAME HERE"];