How to get path to solution directory from inside app.config - c#

I have a file: AppSetting.config in the root folder of my solution.
In my projects App.Config I want to do the following:
<AppSettings file="<PathToSolutionRoot>\AppSetting.config />
Is there a way to get the <PathToSolutionRoot> somehow and stick it in the AppSettings so it points to the solution root folder?

From the documentation:
The path that is specified is relative to the local configuration file. The runtime ignores the attribute, if the specified file cannot be found.
So, this should work:
<AppSettings file="AppSetting.config" />
Now, if you run this in debug mode, it won't work. The reason is you need to add a post-build event so the appsettings.config file is copied to the bin\debug folder:
copy "$(ProjectDir)AppSettings.config" "$(TargetDir)AppSettings.config"
The double quotes are intentional as you may have spaces in the path.
So when you deploy your app, make sure you have this file copied to the root where the executable lives and it should work.

Related

How to set a relative path in web.config to file in another project within same solution?

Okay in my web applications root folder I have two projects (projectA, projectB)
In projectA's web.config I need to reference a file in projectB. I need to use a relative path so not everyone needs to change it each time they pull down my code.
(projectA's web.config)
<add key="File_Path" value="C:\Users\User1\source\repos\SolutionRoot\projectB\folder1\file.html" />
Doing something like this doesnt work.
value="~\projectB\folder1\file.html"
Can this be done or does it HAVE to be an absolute path everytime.

AppConfig file not found in bin directory

I have a App.Config file that users can go to and set some settings to run the program.
But when I go to bin\release folder of my program to copy and give them the exe,DLLs, etc I can't find the App.Config file.
What should I do?
It is a ConsoleApp.
your App.Config willbe converted to ApplicationName.exe.config
so if your application name is SampleApplication.exe then you need to check for the following filename
SampleApplication.exe.config.
FROM MSDN : App.Config
When you develop in Visual Studio, place the source configuration file
for your app in the project directory and set its Copy To Output
Directory property to Copy always or Copy if newer. The name of the
configuration file is the name of the app with a .config extension.
For example, an app called myApp.exe should have a source
configuration file called myApp.exe.config.
Is the App.config file set to copy on build? If you right-click on the App.config in VS, and go to Properties, it should be set to 'Content' and 'Copy Always'/'Copy if newer'.
Here is a screenshot of the Properties screen:

Configuration File in C# default location?

I have just created a WPF application and just been wondering where is my app.config file?
I looked in the bin/Debug and bin/Release directories and there is just one file "WpfApplication1.vshost.exe" and could not find any app.config file.
All I can do is Add->New Configuratin File and this too, doesn't come in debug or Release folder.
Can anyone guide me whether I should manually copy and paste it in the debug or Release Folder?
So finally: the ANSWER
If app.config does not exist, then try creating a new one by "Add->New Item->Application Configuration File" and create a file named "App.config". After this, try rebuilding your application, the .config file specific to your project should appear in bin/Debug & bin/Release folder.
It should be getting copied into the Debug/Release folders automatically. However, you could try explicitly setting the "Copy to output directory" in the properties to Copy always and see if it appears.
After creating simple WPF project my solution look like this. App.config lies there, which is the configuration file itself
When you build your solution, which in my case is named as WpfApplication1, config file lies at WpfApplication1.exe.config in \WpfApplication1\bin\Debug\ folder.
When you build your application,it automatically gets copies to debug/Release Folder.just look for *.config file in those folders.
If you build your project you should have a file named foo.bar.exe.config in you binary directory. The app.config file itself should be visible within your solution directory.

Why won't the appSettings file attribute allow a path to the parent directory?

I'm trying to deploy a project with multiple executables, some of which use a common config file. I'd like to have this common config file in its own directory. The problem is that it keeps getting ignored. I have the tag:
<appSettings file="..\Common Configs\Common.config">
in some of the executable-specific config files. If I copy the Common.config file to the same directory as the specific config file, and remove the path from the tag, everything works. It even works if I do something like:
<appSettings file="..\App1\Common.config">
where App1 is the folder the executable lives in.
This page suggests that the ConfigSource doesn't allow paths to the parent, but the file attribute does.
To further complicate matters, I'm deploying to a file share, so I can't create hard links in the exe folders.
Does anyone know why this doesn't work? How else can I deploy this with a shared config file?
The folder structure has the following form:
Solution
Common Configs
Common.config
App1
App1.exe
App1.exe.config
App2
App2.exe
App2.exe.config
If all of the projects are in the same solution, you can add the configuration file to the Solution Items area. This file can then be shared with all of the exes within the solution.
You will need to select each project that needs the configuration file and choose to Add Existing Item. From there, choose the file, then Add as Link.
This will allow you to maintain the one configuration file and have it deployed side by side with each exe.
Then when you reference it via the appSettings section, you can just reference the file directly.

Can a Console Application reference its .exe.config if the .config is in another folder?

Here is the task I have been given at work. We have a Web Application for which I created a Console Application that can be executed by the Scheduled Tasks on a daily basis. The task I have be presented with is to discover if we can place the ConApp.exe and the ConApp.exe.config in two different directories (folders) in our application. We would like to place the .exe file in the bin folder with all the .dll's and place the .exe.config file in a central configurations folder. I have been looking around in the properties and such with in Visual Studio and I do not see any options that will allow me to specify to the ConApp.exe the location of the ConApp.exe.config.
Is there a way to place these two files in separate folders or do they need to be in the same folder and have the .exe.confing reference a central .config file?
Thanks, :)
You should be able to use ConfigurationManager.OpenExeConfiguration to do that. You can add a setting in the console application's config file that points out the path and filename to the config file, and pass that value to OpenExeConfiguration (granted that the console runs as an account that has read access to the location where the config file is stored).
Note that if your console app contains statements like ConfigurationManager.AppSettings["somekey"], these will need some rewriting so that they use the Configuration object returned by the OpenExeConfiguration method.
The automatic discovery of the .exe.config works only if the files are in the same folder. But you gave the answer yourself IMHO: have the .exe.config reference another .config file in the desired central location.
No, you cannot tell your app to look in another directory for its main app.config.
What you can do is externalize certain configuration sections to external files in another directory:
<configuration>
<appSettings configSource="config\appSettings.config" />
<connectionStrings configSource="config\connections.config" />
</configuration>
This works - even though in the Visual Studio designer there will be complaints about this - on any .NET configuration section (but not on section groups, e.g. you cannot externalize the entire <system.web> or <system.serviceModel> at once - you need to do it by their sub-elements.
So with help from Fredrik Mork I was able to figure out this solution. First of all when you create your Console Applications, DO NOT, create any setting in the projects properties window. This will create an app.config file in your project which I believe the executable will try to look for and crash if it doesn't find it. When you first create the Console Application and then Build it before writing any code. Visual Studio create the Debug folder with just the executable file and a few supporting files. I then placed this code in the "main" function:
Dim fileMap As ExeConfigurationFileMap = New ExeConfigurationFileMap()
fileMap.ExeConfigFilename = "....../AppName.config"
Dim externalConfig As Configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None)
Dim appS As AppSettingsSection = externalConfig.Sections("appSettings")
Dim reportURL As String = appS.Settings("URL").Value
Console.Writeline(reportURL)
In the 2nd line "fileMap.ExeConfigFilename = "....../AppName.exe.config" then "......" represent the full pathname to the config file and AppName is the name of your file. Now I also tried to copy the code from my original Console Application and run it but it still crashed. I think it is due to the .dlls that I am using which make calls to Stored Procedures on the database. I believe these dlls are looking for the .config file to be in the same folder since that is the way they were built. However, if you are careful when you begin writing your application you can utilize Web.config information like I did above.

Categories