How to create the settings file from default options? - c#

File.Exists(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile) is false.
How to make it true?

The Configuration file and the Settings are not the same thing. The configuration file is the one named [Appname].exe.config that should be in the same location as the application.
The settings are stored in the user store separately, and I'm not aware of any method to check if they exist by a file name anyway.

Related

How to read and write custom sections in app.config?

I want to read and write the users settings in the app.config file of my project.
I want to add the user name as a section and adding its setting, for each user if he is not exist in the app.config.
And I want to read it at the beginning of the application.
Can you help me please?
.Net already has this class ConfigurationManager you can use to read app.config and web.config for web scenarios. You could use something like this
var temp = ConfigurationManager.AppSettings["data"];
for more info please check the MSDN ConfigurationManager

GetContentStream alternative in asp.net

In my asp.net application there is a menu built from an xml file. The menu uses a XmlDataSource with the DataFile = "~/App_Data/SideMenu.xml". That file has properties "Build Action"=Content and "Copy to Output Directory"="Do not copy".
Now I want to use this file in my own code, and I tried to open it with GetContentStream, but that doesn't seem to be possible in asp.net (problem with Application object). I am a bit reulctant to change the settings for the file.
What are my options? (XmlDataSource obviously does it)
The first step as you already mentioned is to set Build Action = Content for the file.
Then to read the file you can use HttpServerUtilityBase.MapPath to get the physical file path. From within an ASP.NET MVC controller you can simply use the Server property:
var xml = XDocument.Load(Server.MapPath("~/App_Data/SideMenu.xml"));
Or access it from the Server property on the global HttpContext.Current object.

Store user settings file in executables directory

I'm developing a software in .NET 4.0, which reads and writes application settings. On finish the software will be stored on a file server. No local instances will be executed.
By default the user settings XML file is stored in every users AppData\... directory, but I want to change the file location to the same directory the executable is stored.
This means, that all users should use the same XML user-settings file with modified contents.
I've read this question and answeres where a user describes how to realize that in JSON format.
Question:
Isn't there any other (simple) way to tell the Settings class, where to read from and write to user settings?
The following has been discussed:
Users will always have enough access rights to modify the settings file.
Modifications on settings should be picked up by other users.
Users will start the application from different network computers.
I could implement my own XML file handled by the application (I'll keep this in mind).
I'm not sure if you can get the functionality that you want with the standard Settings class. But I do think that the end result your searching for can be accomplished.
If you want changes in settings user 1 makes to be instantly enforced for user 2, you should look at storing the settings in a database. Your application can then be periodically check this table for changes. For instance if user 1 changes the color of a control, then every time user 2 loads a screen with that control you check the database for the color.
Or, if you want the settings to be applied on start-up of you application. Use a datacontract + xml serializer to write settings to a file of your choosing on a network accessible path/folder. Then make sure you can handle read/write locking of this file.
These are just general ideas that I think you should consider. I dont claim these are your only options though. If you whish to pursue any of these things there are a bunch of blogs and stackoverflow pages with all the information you need.
good luck!
I added a custom Application Configuration File called AppSettings.config to my project and set its Copy to output property to Copy if newer (this copies the config file into the executables folder on building the project.
My settings must be classified as appSettings, not applicationSettings (non-editable) or userSettings (stored in the users AppData folder). Here's an example:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<!-- single values -->
<add key="Name" value="Chris" />
<add key="Age" value="25" />
<!-- lists, semi-colon separated-->
<add key="Hobbies" value="Football;Volleyball;Wakeboarding" />
</appSettings>
</configuration>
Look at the code below, how to access and modify them:
Configuration AppConfiguration =
ConfigurationManager.OpenMappedExeConfiguration(
new ExeConfigurationFileMap {ExeConfigFilename = "AppSettings.config"}, ConfigurationUserLevel.None);
var settings = AppConfiguration.AppSettings.Settings;
// access
var name = settings["Name"].Value;
var age = settings["Age"].Value;
var hobbies = settings["Hobbies"].Value.Split(new[]{';'});
// modify
settings["Age"].Value = "50";
// store (writes the physical file)
AppConfiguration.Save(ConfigurationSaveMode.Modified);
I used the built-in app.config for default application settings and wanted to separate them from the editable global settings in AppSettings.config. That's why I use a custom config file.
That's not directly an answer to the question, but it solves the problem on how to store and share editable application settings in custom configuration files.
Hope it helps any other users! :-)

add app settings in ConfigurationManager without saving

I am picking my application settings from database.
I want to add these setting to my application at runtime. I do not want to use config file because i do not want that anyone can see my settings.
if i use :
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
and then set the settings. the problem is that i have to save and refresh the settings which saves the configuration physically in file.
If i directly use ConfigurationManager.Appsettings["key"] it also requires to have key(i put them without value) in app settings in advance.
I just want to maintain my global settings in memory so that user or anyone can't see settings. What is the solution ?

Storing application settings in C#

What is the best practice to store application settings (such as user name and password, database location ...) in C# ?
Hint: I am new to .net and C#
Application Configuration Settings that are application wide (non-user specific) belong in either app.config (for Desktop apps) or web.config (for Web apps).
Encrypting sections of a web.config file is quite simple as outlined in this Super Simple Example.
If you need to store User specific settings (like application settings, etc.) or Application wide settings not related to application configuration you can use a Settings file as described here:
User Settings in C#
I'm not sure what version of .net/Visual Studio it was introduced in, but you can right click on your project, choose 'Add New Item' and select 'Settings File' from the "Add New Item" window. This provides your project with a (named by default) Settings.settings file that you can configure all the settings you want to expose in.
You can define settings that you create to be either Application or User which means you can use this single interface to control global and user settings. Once you've created a setting in the Settings.settings file using the editor that Visual Studio provides, you can access it in code like this:
// Get a Setting value
var valueOfSetting1 = Settings1.Default.Setting1;
// Modify and save a Setting value
Settings1.Default.Setting1 = "New Value";
Settings1.Default.Save();
First option is the registry. It is easy, but it is not too safe for passwords. Another option is using a file that you create. This too isn't safe, unless you want to implement cryption.
Next option is using the Application Settings. This is also quite simple, but there are a few catches. First, right click on your project and go to Properties. There, under the Settings tab, you can store variables to which you can access from your program by
string password = Properties.Settings.Default.Password
You can also change them the same way, but ONLY IF the scope is set the User. WHen the scope is application-wide, VS does not allow you to change these variables for some odd reason. To save changes, you must call Save() as follows:
Properties.Settings.Default.Save();
These are saved in the User Data folder under C:\Documents and Settings\'Current User'\Local Settings\Application Data\
Another option would be to include them in your database, but since you are also storing your database location, this might not work for you.
I think app.config (non web app) or web.config (web app).
These sorts of settings usually land in Application Configuration Files (web.config, app.config).
http://www.devasp.net/net/articles/display/679.html
If you are storing passwords, you might also need to encrypt the configuration section in question.
http://msdn.microsoft.com/en-us/library/53tyfkaw.aspx
http://msdn.microsoft.com/en-us/library/ff650304.aspx
Note if you use app.config, you will see it get renamed to ..config, depending on if your output produces a DLL or an EXE.
As with the above replies suggest, app.config or the web.config is the best place for app settings.
If you need a more robust way of xml style tags for database, server settings and the like, you can use the configurationSection and create custom sections.
http://www.codeproject.com/KB/cs/CustomConfigurationSectio.aspx
For database passwords, the way i do it is have an encrypted string in the xml tag value and decrypt then when reading them, that way you dont expose the passwords.
appsettings config file, ini file(nini), embeddable database(sqlite,berkelydb/etc..),whatever method you like, it depends on your application size/performance consideration and design.

Categories

Resources