2

我想开发一个应用程序,它应该从文件夹(包括子文件夹)中存在的所有配置文件中读取 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);
     } 
 }

现在我必须阅读连接字符串。我能怎么做?

4

4 回答 4

4

您应该尝试这种方法:

ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
configMap.ExeConfigFilename = fileName;
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
string connString =  config.ConnectionStrings.ConnectionStrings["ConnectionName"];

希望有帮助!

于 2013-03-01T09:10:47.370 回答
1
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");

我已经添加了这个在我的连接字符串中有一个条目仍然它总是取空值

于 2013-03-06T11:59:04.537 回答
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");
        }
于 2013-03-01T09:03:47.063 回答
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;
        }
}
于 2013-07-25T11:06:18.513 回答