2

フォルダ内にあるすべての設定ファイル(サブフォルダを含む)からConnectionstringを読み取るアプリを開発したかったのです。私はこのための勝利フォームアプリを開発しました。

 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
 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 に答える