I'd like to read a part of the appSettings of my console application from an external configuration file named, say, secrets.config, while the rest of it I would like to read from the app.config.
Presently, I have this set up but it seems like it isn't reading from secrets.config and it isn't even telling me about the read failure.
In my app.config
<appSettings file = "secrets.config">
<add key = "Foo" value = "Bar" />
</appSettings>
In secrets.config, which is in the same folder as app.config
<appSettings>
<add key = "Secret" value = "Tiger" />
</appSettings>
In my code
var secret = ConfigurationManager.AppSettings["Secret"];
// secret turns out to be null
It turns out that I was writing the path of the external file as the wrong path.
From the documentation on this page:
The path specified is relative to the main configuration file. For a Windows Forms application, this would be the binary folder (such as /bin/debug), not the location of the application configuration file. For Web Forms applications, the path is relative to the application root, where the web.config file is located.
I changed the path to the following at it worked:
<appSettings file = "..\..\secrets.config">
</appSettings>
Related
I've got a solution with 4 projects (Windows Service, Windows forms, Web and Shared code).
I would like all 4 apps to be able to write and read the database connection settings (server, db name, credentials) to an area where all apps can read it.
Currently it is being saved in the config file, but the Shared code app does not save it it's own config file, but in the app.config and web.config files respectively.
Is there a way that I can save it in a general space to be accessible by all?
Create another configuration file in the parent of all these apps in which you'll store the connection string.
Then customize the loading of the config file to take this file into account.
The appSettings element can contain a file attribute that points to an external file. I will change my web.config file to look like the following:
<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<compilation debug="false" strict="false" explicit="true" />
</system.web>
<appSettings file="../externalSettings.config"/>
</configuration>
Next, we can create the external file "externalSettings.config" and add an appSettings section with our connection information and any other settings that we want to use.
Reading connection string from external config file
You would also need to save the settings you want in that particular file
I want to use log4net for a WCF Service that is hosted in IIS.
But to change settings easily, I want to use a separate config file.
So I added to the Web.Config (and App.config of the WCF Service library)
<appSettings>
<add key="log4net_config" value="log4net.config" />
</appSettings>
But this leads to the current directory of IIS, which is
C:\Windows\System32\inetsrv
And there will never be my log4net.config file.
But I want to configure log4net something like
var configFile = ConfigurationManager.AppSettings["log4net_config"];
var fileInfo = new FileInfo(configFile);
XmlConfigurator.ConfigureAndWatch(fileInfo);
How can I configure a directory that fits this needs?
First of all you could put full path to your log4.net config file.
Secondly you can use System.Web.Hosting.HostingEnvironment.MapPath for example to resolve path like "~/log4net.config"
I always have doubt regarding how a path is formed whenever we run a windows app.
I have set a key like this in my app config
<add key="LogFilePath" value="..\Log\" />
When i run this from my local machine, it provides the path from where the windows app is run.
But when i run the same project from TFS, and when i try to create a file inside the Log folder , instead of the project mapped path it gives an entirely different path.
Can anyone tell me why this happens?
save the relative path in config and where you want to use it do it like this
string fullPath = Path.Combine(Application.StartupPath,configPath);
App Settings are very straight forward.
Add your properties to your App.Config app settings, e.g.
<appSettings>
<add key="LogFilePath" value="C:\Jaspreet_Files\LoadOrgInPortal.txt" />
</appSettings>
and read them, e.g.
var sqlConnectionString = System.Configuration.ConfigurationSettings.AppSettings["LogFilePath"];
I guess the problem is .. in the value:
<add key="LogFilePath" value="..\Log\" />
This seems to be a relative path, try to get full path first before writing and see where it is writing and where it should.
I have my Entity Connection String set in my Web.Config file. In the same solution, I have a Windows Application, and I want to access the Connection String of my Entity. How can I access the connection string that exist in my Web.Config file? Or I have to create a specific one in my Windows Application ?
If you're unfamiliar with adding a config file to a windows app, you must select an "Application Configuration File" from add new items, it will create a file named App.Config which will at compile time become YourProgramsName.exe.config but internally it works mostly just like a web.config with an appsettings section and connectionstrings sections and all the other normal sections.
The best way to share this data between Web and Win config files would be:
Create a separate file that looks like:
<connectionStrings>
<add name="Name"
providerName="System.Data.ProviderName"
connectionString="Valid Connection String;" />
</connectionStrings>
and reference it from both config files with:
<?xml version='1.0' encoding='utf-8'?>
<configuration>
<connectionStrings configSource="connections.config"/>
</configuration>
Then just add the connections.config (add existing item by link) to each project.
More reading: http://msdn.microsoft.com/en-us/library/ms254494.aspx
Put the connection string your windows application's App.config.
use this code:
string connectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
I have this C# project in Visual Studio (2010), and I'd like to refer to a file in my home directory in the <appSettings> section of the project's App.config file. That is, I use this syntax:
<appSettings>
<add key="Database" value="sqlite:///C:\Users\arvek\test.db3" />
</appSettings>
Is it possible to refer to my home directory (C:\Users\arvek) via a variable instead of hardcoding it directly? F.ex.: value="sqlite:///$HOME\test.db3".
The ConfigurationManager won't automatically expand anything in the app settings, since they are just free-form strings, but you can do so manually. Use the ExpandEnvironmentVariables method of Environment, which will expand variables of the form %VARIABLENAME% according to the current environment. So:
<appSettings>
<add key="Database" value="sqlite:///%APPDATA%\database\test.db3" />
</appSettings>
string path = Environment.ExpandEnvironmentVariables(ConfigurationManager.AppSettings["Database"]);
The root path to your "home" directory is in the %USERPROFILE% variable, though %APPDATA% is the traditional place to put the kind of thing you're talking about. There is also %ALLUSERSPROFILE% for system-wide data (though in Windows 7 that actuallypoints to a special system-wide data folder, not the "Public" profile.)
I find it easier to just get the app's directory with this simple line of code
string appBasePath = AppDomain.CurrentDomain.BaseDirectory;
That's how I always refer to it.