Using appname.exe.config to store externally editable settings - c#

I have an application that is run from an application server. I want to beable to change the connection string that the application uses from outside of the application (I do not want the user to do this, and I do not want to use a command line argument, but I want to save the information without having to re-compile)
The application is .net 4.0
I notice that visual studio creates a file called appname.exec.config. Can I add a tagged entry to this file then read it from the .net (C#) application? I don't want the program to edit the file. Instead I want an administrator to edit the file and the program to read it.
I prefer not to use some custom xml file that gets packaged, perferring instead to use a file like appname.exe.config that is automatically deployed by Visual Studio.

Within Visual Studio you'll see under your project a 'Settings.settings' file. This is where things such as connection strings and other configuration info is stored. You can also add additional fields to this file. If your scope is set as 'Application' then these are settings that CANNOT be changed at runtime, which is exactly what you are looking for. Instead someone would need to open up the XML file and edit it directly to make a change for the next time your application runs.
http://msdn.microsoft.com/en-us/library/a65txexh%28v=vs.100%29.aspx

Related

How can I use Properties.Settings when I deploy my C# Winforms app with NSIS?

I have written a C# app using Winforms, and am trying to utilise the inbuilt Properties.Settings (User scope) to remember user state between launches of the program.
When the app is deployed via Visual Studio's inbuilt Build > Publish, apparently this file is put into the Local Appdata folder.
However, I want to use NSIS to create the executable of the program. I am able to place the ProgramName.exe.config file into Local AppData, via NSIS script, but my app does not seem to read from it. No user settings persist.
Is this even possible? Or should I use my own method (database or write my own settings xml) to store user settings?
OK, I got it working with NSIS.
The App.config file has a property called "Copy to Output Directory" which was set to "Do not Copy" by default. I found this information here: https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/
I found out where the user.config file was going to be created, with help from #Jimi by adding System.Configuration as a reference, and then getting the path using:
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
The user.config file is not created on application install, rather the first time that the following is called:
Properties.Settings.Default.Save();
There is nothing special that the developer has to do in NSIS.

Adding app.config (Application Configuration File) in Visual Studio Professional 2017

I can't get the Application Configuration File template to show up under my Add > New Item > Visual C#. I've run a repair. I've uninstalled and reinstalled. I've deleted and added Cache folders. I've run devenv.exe /InstallVSTemplates. Nothing is working.
If anyone can tell me specifically what installation elements I should be including, and specifically what type of Project I need to start that might help.
I want to create a simple .Net Web Form in .Net and C# and need to include an app.config file.
When you create a new project (depending on the type of project like a web application for example) it will automatically create a template app.config file.
Worst case just make a txt file in notepad and call it [Appname].config
Another way would be to go in the project properties, then the Settings tab. If there are no settings yet, a link should exist in the middle of the empty tab to create a default setting file. Add one Application or User setting and save. Among other things a config file will be created for you too.

Why wont my application read my DataAccessLayer.dll.config file?

I'm trying to use application settings with a C#.NET project I am working on. But I can't seem to get it to return anything other then the default values. Through the project properties I've added a single setting, DBConnectionString, and set its value to a connection string I want to use. Its scope is set to "application".
Doing this created a number of files including Settings.settings, Settings.Designer.CS, and app.config. The Settings class then has custom, type safe, properties that can be used to set and retrieve each setting. The app.config file is a XML file that stores the actual settings values.
When I build my project it looks like the app.config file is copied to the target directory as DataAccessLayer.dll.config. My program runs fine and is able to use the default connection string.
Next I tried to edit the DataAccessLayer.dll.config file to change the connection string. I ran my program again, but it continued to use the default value.
This project is a class library that use to a Web project and sometimes the connection string can changes.
Builds will output config files named after the dll however those aren't actually what's read on app start up. You could put the setting in the web apps config (example here Equivalent to 'app.config' for a library (DLL)), those are the settings you'll actually be running with in this case.
If you want your library to be portable you'll have to either; 1) make your own config class/file 2) Read your dll's app config manually (example in the answer I linked to above) or 3) Put your setting in the importing projects app.config
In this case I would just put your connection string data in the web apps config. If DataAccessLayer.dll is only for internal use, this is in my experience the most common pattern, and doesn't really have many cons. Ultimately I would have these values set during my build or by a deploy utility like Salt or Chef. If you're manually editing the web apps config on or after deploy then you're doing it wrong.

How can I publish a Windows Form app to include a csv file?

I'm creating a Windows Form application that is going to be used as a sign in system at my college. I was given a .csv file that includes student id numbers, names, teachers, and current courses. I have the app created and it works when I use it on my computer. However, when I publish the application and install it on a different computer, the streamReader that I use to access the .csv file is not able to use the .csv file because it's nowhere in the published application. I have tried adding the file to the root directory of the project, but I can't find out how to access it from there. I've read a few forums that talk about adding the file as a "resource," but I don't know enough about that to make it work for what I'm doing. Any assistance would be appreciated. Thanks!
In visual studio, add the file to your project (Add Existing after right click on the project). You'll need to add a build action as well, to tell the IDE to copy the file to the output directory. To do that, right click on the file you added, go to Properties, and look for BuildAction.

Different application settings per executable

I'm creating an application and I make use of Properties.Settings to store the settings.
However, let's say my application is on the desktop of the user and is called Program.exe, now, when the user copies this executable and places it somewhere else or even renames it, all settings are gone.
Why is C# doing this? Is there any way this can be turned off while pertaining the user scope? I don't wish to use the application scope since multiple users can be sharing the same computer.
The settings are likely stored in {appname}.exe.config which apparently is not being copied/renamed with the executable. Either copy the executable with the file, hard-code them in the application, or find another mechanism to get/set app settings (like the registry).
The application will look for these settings in a file titled {appname}.exe.config. If the executable is renamed (without renaming the .config file) or copied to another location without copying the .config file along with it, the application won't know where to look for their settings so they will be blank (unless you set a default value in the app).
You can "hard-code" default settings by putting a value in the Settings.settings "file" in Visual studio (which effectively adds the default value as an attribute to the setting).
Another option would be to hard-code the values directly in the source code. I'm NOT recommending this approach as it hinders the ability to change that value, but if you want to be able to deploy the app by just copying the EXE (and nothing else) then it is an option.
See http://msdn.microsoft.com/en-us/library/aa730869(v=vs.80).aspx for details.
The settings (and their location) when using Properties.Settings is determined by the Scope setting of the particular setting.
Settings that are Application scoped are in the app.config file (renamed at compile time to .exe.config - these settings are read only at run time), and settings that are User scoped are saved in a user.config that is tied to the user's profile on the machine (these settings are read/write at run time). By properly scoping your settings you should be able to avoid this type of problem.
Settings are stored in the user profile (under C:\Users[UserName]\AppData\Local), these settings are under folders that have the application name and also have an identifier of the location of the exe file they refer to, for example: MyApp.exe_Url_hpvvra0rj4y03ebpz3cfmzsrcpczat11, refers to the executable under Program Files, if i move or copy the exe and run it, it will create another folder with another URL (MyApp.exe_Url_vqzsq0spwewydv3wrnebtqji24nwuboe, for example), i haven't find a way to avoid this but a good workaround is to copy the settings from another config file, just note that this method is not foolproof as you have to choose the right file to copy the setting to and if you expect your exe to be copied or moved constantly this will be a hard task.
Another workaround is to simply create your custom settings class and put the file anywhere your application can find it and don't realy on the built in settings on .NET.

Categories