I am developing a plugin for a .NET 4 application and I want to add a config file to the dll as I dont want to put the configuration in the main config file.
I have added the app.config t the project and it is correctly compile and dllName.dll.config generated.
Here is my configuration file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="MyTabsConfig" type="NewApp.UI.MyTabsConfigHandler, NewApp.UI" />
</configSections>
<MyTabsConfig>
<MyTabs>
<MyTab Name="First" Leads="2" />
<MyTab Name="Second" Leads="4" />
<MyTab Name="Third" Leads="1" />
</MyTabs>
</MyTabsConfig>
</configuration>
Now I have 1 problems:
If I copy the file in the ExtraPlugins directory of my main application, NewApp.UI.dll cannot be found when calling GetSection("MyTabsConfig"). I think it is looking in the main application folder.
Thanks.
Have you tried something like this?
ConfigurationSection section = ConfigurationManager.OpenExeConfiguration("myConfig.config").GetSection("mySection");
Related
I have an App.config of the form
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="Custom.Config" type="System.Configuration.DictionarySectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<kCura.Config configSource="Custom.config" />
</configuration>
With a custom configuration file Custom.config of the form
<?xml version="1.0" encoding="utf-8"?>
<Custom.Config>
<add key="foo" value="bar" />
</Custom.Config>
I've inherited this code, and there is no existing ConfigurationSection or ConfigurationSectionHandler associated with this Custom.Config section.
I can access the value of foo just fine with
DirectCast(System.Configuration.ConfigurationManager.GetSection("Custom.Config"), System.Collections.IDictionary)
But now I would like to update this value, programatically (for testing purposes). Is this possible?
I've read the following, and I'm still stumped; they seem to imply that this namespace is only for reading values, not full CRUD:
How to: Create Custom Configuration Sections Using ConfigurationSection
How to: Create Custom Configuration Sections Using IConfigurationSectionHandler
You'll have to modify the Custom.config file on disk by using XDocument then you'll need to call RefreshSection on the ConfigurationManager
This will then refresh the named section so the next time that it is retrieved it will be re-read from disk.
I have an MVC project and am trying to store my API keys in a separate config file which I will ignore when pushing the code to Git. According to MSDN I should be able to store them in an App.config like like so
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="APIKey" value="APIKeyValue" />
</appSettings>
</configuration>
I should then be able to read from the file by creating a method in a model
public class KeyTest
{
public string KeyTestCall()
{
string testkey = ConfigurationManager.AppSettings.Get("APIKey");
return testkey;
}
}
and then invoke the method in my controller to assign the value from my App.config file (just so I know I'm getting the value).
public void Testing()
{
KeyTest k = new KeyTest();
ViewBag.x = k;
}
At no point will the code break for a breakpoint, the build will succeed and I can't tell if I'm getting the value or not. Any help is much appreciated. Thanks!
For a web application such as an MVC app, it's a Web.config file, not an App.config
In addition to above (re: web.config vs app.config) if you want to remove "secrets" from source control, this is one way to do it:
In web.config
<?xml version="1.0" encoding="utf-8"?>
<cofiguration>
....
<appSettings file="AppKeys.config">
<add key="SomeOtherSettingThatHasNoSecrets" value="foo" />
...
Then in a separte AppKeys.config file (you can name this whatever.config, sample as named in the above), that you don't add to Git/source control:
<appSettings>
<add key="SomeSecretKey" value="the secret" />
...
Note that AppKeys.config doesn't have an XML declaration.
Hth.
I don't know why but i don't have AppSettings in my app.config.file.
If i add it, when i run my apps, appSetting is deleted, so i'm unable to read my data !
Here is my file :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<add key="RepertoireEntree" value="E:\DEV\Autres\DevAgréga\Input\*.txt"/>
<add key="RepertoireSortie" value="E:\DEV\Autres\DevAgréga\Output\"/>
</configuration>
And in c# :
string RepertoireEntree = ConfigurationManager.AppSettings["RepertoireEntree"];
You have your app settings in a appSettings element, else it won't be recognized:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="RepertoireEntree" value="E:\DEV\Autres\DevAgréga\Input\*.txt"/>
<add key="RepertoireSortie" value="E:\DEV\Autres\DevAgréga\Output\"/>
</appSettings>
</configuration>
By the way, it is easier to use settings the usual way. You can do this by creating a settings file from your Project Settings > Settings tab.
Visual Studio will generate the XML elements in the app.config for you and you can reference your property like this:
string re = Properties.Settings.Default.RepertoireEntree;
You do not have your xml correct in the app.config file.
note the start and end tags for appSettings
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="RepertoireEntree" value="E:\DEV\Autres\DevAgréga\Input\*.txt"/>
<add key="RepertoireSortie" value="E:\DEV\Autres\DevAgréga\Output\"/>
</appSettings>
</configuration>
PS
While this is probably not an issue for you (but moreso for future readers)
Make sure you have this reference:
Namespace: System.Configuration
Assembly: System.Configuration (in System.Configuration.dll)
as noted on MSDN
I'm getting a "OpenNetCf - Argument Exception" when I try to start the server on the desktop (windows 8.1)
Srvr = new OpenNETCF.Web.Server.WebServer();
Srvr.Start();
However, I can't see a problem with the config file. Any suggestions would be much appreciated.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="WebServer" type="OpenNETCF.Web.Configuration.ServerConfigurationHandler, opennetcf.web"/>
<section name ="httpRuntime" type="OpenNETCF.Web.Configuration.HttpRuntimeConfigurationHandler, opennetcf.web"/>
</configSections>
<WebServer
UseSsl="false"
LocalIP="0.0.0.0"
DefaultPort="80"
MaxConnections="20"
DocumentRoot=".\Inetpub\"
Logging="false">
<DefaultDocuments>
<Document>Login.aspx</Document>
</DefaultDocuments>
<VirtualDirectories />
<Cookies />
<Caching />
</WebServer>
<httpRuntime
maxRequestLength="4096"
requestLengthDiskThreshold="256" />
</configuration>
You are most likely attempting to use the Compact Framework version of the binaries. While the Padarn code base work for Compact Framework, Full Framework and Mono, the compiled assemblies are different and you must use the appropriate one based on your runtime environment.
Inside my web.config file I've got code like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
...
<section name="UninstallSurveySettings" type="dashboard.UninstallSurveyConfig" />
</configSections>
...
<UninstallSurveySettings>
<add key="fileLocation" value="C:\inetpub\wwwroot\output\" />
</UninstallSurveySettings>
...
</configuration>
I need to be able to access this field from my custom control. The control can be dropped into any website and needs to check that site's web.config for the fileLocation value in UninstallSurveySetting.
I've tried a couple different approaches with no luck. Any help on this would be greatly appreciated.
Much easier to use AppSettings.
Web.config:
<configuration>
<appSettings>
<add key="fileLocation" value="C:\inetpub\wwwroot\output\" />
</appSettings>
</configuration>
Code:
string location = System.Configuration.ConfigurationManager.AppSettings["fileLocation"];
If your section will become more complex, then:
var section = (NameValueFileSectionHandler)ConfigurationManager.GetSection("UninstallSurveySettings");
if (section != null)
{
// access section members
}
P.S.
Maybe you want to use ConfigurationSection class instead of handler.
In ASP.NET MVC 3 the tag cannot be a direct child of (it results in a configuration error).
How about adding your key to the section. Then you can easily access it via the ConfigurationManager.AppSettings collection.
using System.Configuration.ConfigurationManager and you will be able to get what you want from the web.config
I was able to solve this by creating a configuration class for it and placing this code in the web.config:
<section name="UninstallSurveyConfig" type="dashboard.UninstallSurveyConfig" />
..
<UninstallSurveyConfig dirFileLocation="C:\inetpub\wwwroot\build\output" webFileLocation="~/output" />