C# DLL config file

后端 未结 17 2589
梦毁少年i
梦毁少年i 2020-11-22 05:05

Im trying to add an app.config file to my DLL, but all attempts have failed.

According to MusicGenesis in \'Putting configuration information in a DLL\' this should

17条回答
  •  一整个雨季
    2020-11-22 05:10

    It confusing to mock a "real" application configuration file. I suggest you roll your own because it is quite easy to parse an XML file using e.g. LINQ.

    For example create an XML file MyDll.config like below and copy it alongside the DLL. To Keep it up to date set its property in Visual Studio to "Copy to Output Directory"

    
     
      
     
    

    In your Code read it like this:

        XDocument config = XDocument.Load("MyDll.config");
        var settings = config.Descendants("setting").Select(s => new { Key = s.Attribute("key").Value, Value = s.Attribute("value").Value });
        bool keyboardEmulation = settings.First(s => s.Key == "KeyboardEmulation").Value == "On";
    

提交回复
热议问题