ConnectionString from app.config of a DLL is null

后端 未结 3 1253
闹比i
闹比i 2020-12-18 11:55

I have a class library that contains a valid connectionString inside the app.config. Inside that class library I want to use it with

ConfigurationManager.Con         


        
3条回答
  •  青春惊慌失措
    2020-12-18 12:35

    In case if you don't waana use ConfigurationManager

    If i assume the config file is mydll.dll.config i can load it as XElement and parse it using Linq as

    var xe = XElement.Load("mydll.dll.config");
    var connectionString = xe.Descendants("connectionStrings")
         .Elements("add")
         .FirstOrDefault(a => a.Attribute("name").Value == Name)
         .Attribute("connectionString").Value;
    

    where Name is the connectionString name in the XML. Without using the ConfigurationManager import and other stuffs. The only requirement for this to make sure that the config file sit next to the dll.

提交回复
热议问题