Adding reference/ Point to an external file from a protected folder - c#

I am working on an existing client application. When this application is deployed by the deployment team it has a generic exe.config file.
I have created an application which gives user choices and based on their choices it creates the correct exe.config file. Now I just need to replace the generic exe.config file with my specific exe.config file and then launch the application.
My application works fine if I have write access to the folder where I have all the dlls and exes of the client application. I was replacing the generic config file with the newly created config file and then launching the application and it all worked fine.
Now the deployments team is planning create the folder inside Program Files folder to which my program wont have write permission to.
In this case I am looking for ideas how to point the generic config file to the specific config file which my program has created.
Anu ideas will be appreciated.
Thanks in advance.

Perhaps you can code the generic config file to reference the specific config file with the file or configSource attributes?
For Example:
generic config file
<configuration>
<appSettings file="..\..\specific.config"></appSettings>
<configuration>
specific config file
<appSettings>
...
</appSettings>
More info on file and configSource here: http://msdn.microsoft.com/en-us/library/vstudio/ms228154(v=vs.100).aspx

Related

c# installer project with external file

I have created an application in which every installation is differed by the configuration file.
Currently the configuration file (settings.setting) is part of the installer itself.
Is there a way to create an installer without the settings.setting embedded inside it, so will have the setup.exe and a separate settings.setting file?
(So will have 1 installation build, and the installation will copy the setting file to the relevant location as done if it is part of the installation build)
Thanks,
Yoav
Maybe you should try to extract the config file to another file and link it from default config file.
MyApp.exe.config would containt a line like this:
<appSettings configSource="pathtoconfig\MyExternalAppSettings.config" />
Here's a good blog post on this subject.

Is Web.Config the same as app.exe.config? [duplicate]

when building a desktop app in wpf can you read documentation of problems and safely subsititute 'app.config' when people's answer's refer to 'web.config'?
if so are there any glaring GOTCHAS you have to look out for?
tnx
Read the Documentation:
Web.config and App.config
The choice of the
configuration file name is determined by the hosting environment you
choose for the service. If you are using IIS to host your service, use
a Web.config file. If you are using any other hosting environment, use
an App.config file.
In Visual Studio, the file named App.config is used to create the
final configuration file. The final name actually used for the
configuration depends on the assembly name. For example, an assembly
named "Cohowinery.exe" has a final configuration file name of
"Cohowinery.exe.config". However, you only need to modify the
App.config file. Changes made to that file are automatically made to
the final application configuration file at compile time.
In using an App.config, file the configuration system merges the
App.config file with content of the Machine.config file when the
application starts and the configuration is applied. This mechanism
allows machine-wide settings to be defined in the Machine.config file.
The App.config file can be used to override the settings of the
Machine.config file; you can also lock in the settings in
Machine.config file so that they get used. In the Web.config case, the
configuration system merges the Web.config files in all directories
leading up to the application directory into the configuration that
gets applied.
Web.Config is used for asp.net web projects / web services.
App.Config is used for Windows Forms, Windows Services, Console Apps and WPF applications
Your question isn't providing all the information as to where the gotcha's may lie for you.
Can you give us more info on what you are trying to do in terms of these config files?
Here's a link...
Problems with Web.config and App.config

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.

using external config file for a .NET windows service

I have an app.config file for a windows service which includes database connection strings and appsettings. when I install the windows service, it gets installed in "C:\program files\" folder and the settings are copied to a file called ".exe.config" in the same folder, which makes it difficult to change the settings after it is deployed to test environment. can I have all settings stored in an external file somewhere on shared network drive, instead of the same folder where the service is installed? is that possible? only alternative I can think of is to create an xml file and read it using the .NET XML API, another issue is I need to watch for the changes in the file and reload the settings in the service.
Linq to XML makes it fairly easy to read an external settings file. You can also use a FileSystemWatcher to watch for changes in the settings file.
You can push some of your config settings out of your core config file, e.g.:
<connectionStrings configSource="connectionStrings.config" />
And:
<?xml version="1.0" encoding="UTF-8"?>
<connectionStrings>
</connectionString>
That way, as long as you don't deploy the connectionStrings.config file, you can safely deploy the app and maintain those settings separately. The good thing about this approach, is you don't need to create a whole new method of getting config data, this is built into the configuration system, just read these settings how you do currently.

Categories