I have an application that's dealing with a required list of files it has to manipulate when run. I wanted to add a couple of files to this list. The list of files is stored in a setting. The scope of the setting is "Application". I added my two files to the list (via project properties -> settings), and saved everything. My files now appear there. My files are showing up in the app.config for this project under ApplicationSettings.
But when I run the application this line of code:
StringCollection requiredFiles = mySettings.UpdatePackageRequiredFiles;
Does not include my files.
mySettings is the right type; it's the project where the settings exist. Further, there is nowhere in the project UpdatePackageRequiredFiles is being written to and you can't write to an Application scope setting at run-time anyway (or so I understand).
To spice up the mystery a little bit, there are 3 files appearing in the list whose existences are a complete mystery. They do not appear when viewing the settings in the designer, or in the source file. So what am I missing?
It turns out there is a configuration file with an identically named setting that the application was getting the list of files from.
Related
So I went in to the project and right click > Add and created a new Config.settings file and Login.settings file.
I then added a couple of inputs in each of those settings files.
How do go about accessing the settings values in those settings files?
I tried Properties.Default... but then it seems to not recognize Properties class as it shows a curly red underline in its name.
Do I have to import some name space?
I then try adding a new file called "Setting.settings" file and its still the same. I clean and rebuilt my project too.
Try right clicking on .settings file and selecting run custom tool option.
Also, sometimes it helps when you manually remove bin and obj files from project. Finally, close the solution and open again.
For user overrides and how settings are working refer to microsoft docs: https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/using-application-settings-and-user-settings
So, despite a lot of info out there being difficult to understand and find on the latest Visual Studio settings file, I got that figured out. On your executable project, right-click and select properties, then left-click on the settings tab on the left, create a settings file (if needed), and fill in some settings names, types, and values in the table. If you want these to be permanent defaults that are embedded in the executable and read-only, set the scope to "application", but if you leave it at the default scope of "user", then you can save new values to a special user.config file in the system AppData folder which will be read from every time your app is launched, overriding the embedded defaults. This basically allows every user to have a config file automatically stored by the system saving some config settings, such as last window location, etc.
After setting up this, you can directly access the settings as follows:
var variable1 = Properties.Settings.Default.Variable1;
var variable2 = Properties.Settings.Default.Variable2;
and so forth. To set the properties to a new value is equally easy:
Properties.Settings.Default.Variable1 = variable1;
Properties.Settings.Default.Variable2 = variable2;
And then, if you want to save the special user file with the current values into something like C:\users\username\AppData\Local\ApplicationName.exe.xxxx\user.config, then just use:
Properties.Settings.Default.Save();
Here is the issue - I'd like to access those same settings from all files in my solution, but the auto-generated methods are marked internal, so they can only be accessed from my main exe project. I can create a class to essentially export the settings, but this only seems to work if the startup executable is always the same. When I run unit tests (with xunit 2.0), I find that the settings are not set - if I use the library to set them and save, they show up in a Microsoft folder in AppData, that appears to correspond with Visual Studio.
Does anyone know a way I can reliably access the Properties.Settings.Default values of a project across an entire solution, or am I stuck going with a straight file at a known location and all the I/O and parsing that entails?
If so, is there a standard C# config file library that is used for this task outside of the built-in Visual Studio settings system?
Edit - Update:
The settings file appears to be based on the following:
The Company Name in the Assembly Information (Properties/AssemblyInfo.cs or Project Properties/Application/Assembly Information/Company)
If the Company is empty, it appears to use the Default namespace from (/Application/Default namespace), which matches the namespace used in Properties/Settings.settings/Settings.Designer.cs
The Executable Name (i.e. app.exe)
This is strictly the executable name at runtime, including extension, so if you rename the file, that will change the settings file name and location in AppData
The Assembly version
The folder within AppData/Local will be #1 above and then a nested folder will have the executable name (#2 above) with some other characters appended, and then a final nested folder will be the version (#3 above), and the file inside that will be user.config.
So, what appears to be happening is that when the unit test runner executes, the company of the runner is used for #1 (the built-in VS runners will use "Microsoft", and the Resharper runner will use "JetBrains", etc.), the exe name will still start off with the Namespace of the tests, and then the version number is also from the test runner (I think).
This means when running unit tests it will always look for and save to a different settings file than the main executable - so when you access the Properties.Settings.Default parameters from any assembly, it will look for the file in a different location (and save it to a different place) when running unit tests than from running the main executable.
You can always expose objects marked as internal by adding the following to the AssemblyInfo.cs [assembly: InternalsVisibleTo("UnitTestProject1")] in the main exe project. You should then have access to the internal objects from the test project.
https://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute(v=vs.110).aspx
In several C# projects I have been using an app.config file to pass various settings to my program, settings like connectionstrings, power levels etc.
However sometimes I have come in situations where the settings aren't updated as expected and I have concluded that I am not that well informed with the proper use of app.config file.
Example:
Replacing the .exe file with a new version (where settings are different) to the output directory without changing the exe.config, results in the program seeing the hard-coded settings and not the settings of the existing .exe.config
So my Questions are:
What is the exact role of exe.manifest file
Every time I create a new .exe do I have to paste in the output folder anything else except the .exe file?
Whats the difference in obtaining the setting value by: ConfigurationManager.'settingName'... rather than:
Properties.Settings.Default.'settingName'?
What is the role of app.config build action?
Sorry If I am asking too much in a single Question.
The app.config file is a file that gets a special treatment when running the associated application. If you executable is named MyApp.exe the app.config file should be named MyApp.exe.config. The role of the app.config build task is to copy the file named app.config in your project to MyApp.exe.config in the output directory.
.NET provides ways to read the contents of the file (it is in XML format) and various parts of .NET will look for different sections in this XML to provide configuration.
A widely used section is the settings section you refer to as Properties.Settings.Default. This section plays together with Visual Studio that provides an editor for application settings. The settings are accessed in the code by using a generated class. Adding a setting will add a property to this class and this property is initialized from a value in the app.config file.
In the Visual Studio editor you can set a value for the setting and you can think of this as a default value for the setting. However, if you overwrite the value in the app.config file the later will take precedence. This allows you to modify the app.config file after installation and rerun the application with a modified setting.
You can also access application settings the app.config file using other methods, but in my oppinion using the Visual Studio editor and the code generated class is the best way to do that.
I am not sure I fully understand the problem that you experience. If you update MyApp.exe and leave MyApp.exe.config intact you should not see a change in the settings used by the application (unless of course you have renamed or changed some settings).
The manifest file provides information about side-by-side assemblies and can be used to request elevated privileges among other things. This is not related to the app.config file.
There quite a few resources about that.
See http://msdn.microsoft.com/en-us/library/ms229689%28v=vs.90%29.aspx
and the (better) overview: http://msdn.microsoft.com/en-us/library/k4s6c3a0%28v=vs.110%29.aspx
app.config is a very powerful tool. It addresses many issue like versioning, migration, upgrading etc. but this requires some in-depth reading from the links above.
Maybe one thing you could do, if you want to copy only .exe file every time you build your app, is make a settings.ini or settings.txt file, put your parameters in this file (that are not secret of course) and read all your settings from there when you start your app.
You can put all your connection string logic in your login form if you have one...
I am trying to save Name Value pair in settings dynamically. I can see the new saved values dynamically. But, when I access the settings page from the property window , it shows the same old value. Here is the code :
Properties.Settings.Default.Name = textBox1.Text;
Properties.Settings.Default.Age = textBox2.Text;
Properties.Settings.Default.Save();
Any Suggestions?
Assuming you are testing your application with Visual Studio, your problem happens because, when you are changing the setting of your application you aren't changing the original setting file. When Visual Studio starts running an application, it creates a folder inside the directory where your code is named "obj/Debug" or "obj/Release" and copies all your DLLs and resources into that folders, including settings files.
This means that changes to settings will be reflected in your "obj/Debug/yourappname.exe.config" and not in the original file. If you open this file, for example, with a text editor you'll see the contents have changed. Remember that every time you recompile your application in Visual Studio and start running this file will be replaced by the original, losing your new settings.
You can manually run your .exe application inside that folder and validate if your settings have persisted.
After compile the settings file gets deployed into a yourapplication.exe.config file. This is the file you are changing (/Debug/app.exe.config). If you want to see changes in your "property window" you have to manually open the .settings file and edit the xml.
Note: after changing the .config file the changes are persistent .. but only until you compile your application again.
The solution->Properties->Settings is where you set the default values. User settings can be changed at run-time, and will persist with your application, but they are not written back to your visual studio project.
In your example, if you run the program again on subsequent times (* not by hitting debug / rebuild in visual studio), your settings that are saved in your snippet will have been persisted.
Imagine that your program has been deployed on user's pc's - there shouldn't be a mechanism for that to change your visual studio project settings file.
I have a project that reads in a file based on a value in a C# Setting class. This value however changes from machine to machine and Id rather not ask the user the first time the program is run as its a corporate environment, instead Id like it to be set in the installer, but where is the file located? Is there an easier method?
This is a visual studio addin not a standalone program
From your post it appears you have a windows application? , you can store an initial value in the application config, you can make an installer in Visual Studio and write custom actions that can write values to the file on first install in you install project.
The configure file is the easiest way to do what you are asking, however it is NOT the best. In fact it is recommended Not to use .config files for such cases.
whenever users install an 'update', there is a risk of overwriting their existing changes.
some businesses might have policy restrictions on the .config files.
the user cannot easily move his settings from one PC to another.
From my own experience, using XML, MS-Access, Registry or text files to store user settings has proven more useful than using the .config files.
I believe you are talking about designer-generated settings (the .settings file)?
The exact path usually contains some sort of a Hash (check this link). I usually have my own settings class which I serialize to and from xml using XmlSerializer, which gives me more freedom (I think C# settings files don't allow you to add custom enums, for example, or they make it a bit harder to do it than simply adding them to the .settings file).
However, maybe there is no need to set values during installation? For example, you could add a FirstStartup setting (set to true initially), which you can read when your App is started for the first time, and then set it to false. That way you can set your default settings when you detect a "first startup".
You will certainly need some sort of custom action at the end of your installer. You haven't mentioned what technology you're using to deploy your application so I will refrain from giving any specific guidance.
I recommend setting the value in your App.config. This is an xml file which will be named MyApplication.exe.config in the same directory as your application. Add it to your installer if it is not there already. In your installer, add a new setting:
<configuration>
<appSettings>
<add key="MySetting" value="My Value"/>
</appSettings>
</configuration>
In your code, retrieve the setting:
String setting = ConfigurationSettings.AppSettings["MySetting"];
If this is a machine-wide setting, this installer is the right place to set this. If you do it on the first execution you will run into a host of problems with permissions on the files and directories if the first person to run the app is a user with limited permissions.
Registry or an INI file or a XML file whatever suits you best.
Best option would be registry. Istalling the application will require Admin access so writing to the registry during installation will not be an issue.
More over if some one accidently deletes the ini or settings file then the program might stop working.
You said that this is for a corporate environment. You can create an administrative template for group policy to set the default in the registry. Then, your program can just query that one registry value if the default hasn't already been set.
I personally would never use web.config or app.config. Just read your own xml file, have a look at my post on this:
http://www.picnet.com.au/blogs/Guido/post/2009/09/10/XML-Settings-Files-No-more-webconfig.aspx
Thanks
Guido
To answer the "Where is the file located" bit of the original question for those interested (I'm assuming that the "settings file" in question is the "Settings File" item available from the "Add New Item" dialogue).
In Win7 (and probably all the other ones) the settings file will generate under the path:
C:\Users\[user]\AppData\Local
The folder structure from here on out depends on your AssemblyInfo and build information, the "AssemblyCompany" field (if populated) is used for the top-most folder, beneath this is a folder copying the application executable name, followed by the build and then finally the settings file (named "*.config").
In my case, the complete path is as follows:
C:\Users\[user]\AppData\Local\[company_name]\Application.exe_StrongName_zx0v1qxyoass4d0z3mtwe3vbrhaehkjg\0.0.5812.21662\user.config
My case is different, my project is a class library (dll) which have the app.config file. I decided to create 4 application settings variables (title, url, limit, rate). To create this, i right-click ont he project --> Properties --> Settings. this values you can retrieve in code using this command --> Settings.Default.title, ...etc
the problem is let say i instantiate 5 objects (same object for example MyProject.MyClass) from this library, i want the instance be able to have its own settings. i mean each of the instance may have their xml setting file. can this be done?