Application does not pick updated values after modifying App.Config file c# - c#

I have developed a winforms application which consists of a file path value (fetching crystal reports) in add key of app.config file.
I have installed the application and trying to change the file path (in app.config as well as exename.exe file) from old to new but the application doesnt take new crystal reports file path instead retrieves old data.
Am i missing any other file to be updated ? Kindly help to get it solved ASAP.Thanks

You probably don't want to change App.config, as that file will be copied to exename.exe.config when you build.
After installing your program, you'll need to change your exename.exe.config file to pick up your changes.

Verify that the setting you are trying to change in exename.exe.config file is set to User scope rather than Application.
From Visual Studio go to your Project properties and Choose Settings
You will find listed all your settings. From there verify that each setting you want to be change by user has scope User
Here you can find detailed information on the difference between User and Application scope

Related

How to open app.config in windows forms?

I need to open app.config with a click of a button. I've been trying to find how to do this with no luck. How can I open the file itself so I could edit it?
If you need to open the application configuration file itself you're doing something wrong.
If you need to access settings defined using Visual Studio's settings designer, use the automatic properties like
var value = Properties.Settings.Default.SettingName;
If you want to edit user settings, do the same thing and save:
Properties.Settings.Default.SettingName = Value;
Properties.Settings.Default.Save();
You can not modify application settings anyway. If the application is installed in Program Files you will not even be allowed to write the file back, as the Program Files folder is not writable to your application.
If the application is installed using ClickOnce you're also out of luck, as the settings file for user settings is named totally differently and is not even in the same location as your application's executable.
So this comes down to: Do it right and keep your hands off app.config!

Winforms showing different values from xml

I have a windows service project which contains a Test.xml file. In the properies it is changed to copy always.
I have created a setup for service and after installing the service the Test.xml is present in the C:\program files(86)\service folder.
Now I want to edit the Test.xml file throgh a UI. So i have created a small Winforms project 'EditXml'. The EditXml is accessing the Test.xml like this
document = XDocument.Load(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Test.xml"));
I have added the EditXml.exe also to the setup of services. So after installing the service the EditXml.exe is also present in the folder.
But when I edit using EditXml I cant see the changes in test.xml.
Why values are different from Test.xml? Is this a correct approach? Let me know if question is not clear.
I think that you have an issue with inssuficient permissions and windows file virtualization. When your installer creates a file inside Program Files folder, it usually sets permission only for read operations.
When you try to modify the file using an application running at user account that has now write permissions, windows makes an illusion that the file has been successfully written, but puts it in a virtual store - check if such directory/file exists in c:\users\CurrentUserName\Local\VirtualStore\Program Files (x86)\YourFolderName
Only your application can see modified file version, but all other application see original version written by installer.
To be able to modify the file using an application, you need to add write permissions to the file.

Store user settings into application folder

I'm using setting from my C# application like this:
String str = Properties.Settings.Default.SETTINGS_NAME;
When I save this settings, a settings file is created on
C:\Documents and Settings\<user name>\Local Settings\Application Data\<comp name>\Keb.exe_Url_pbs4eg1o2ija22omldrwjxhdam0jxxi5\1.0.0.0\user.config
Is there a way to change this path to Application.ExecutablePath\user.config, and use it next time so my application can be more portable ?
You can control the location of the user.config file by creating a custom SettingsProvider. Luckily for you, someone at CodeProject already did that.
See my answer here for all the details: How to make designer generated .Net application settings portable
If you want it to be Single user or in other way make the configuration of your application portable i will use a custom config file like an .ini file and keep it my app's root folder.
That way any one want to have those settings can just copy it in his own app's root folder on some other computer. When app runs it just loads the settings and behave accordingly.
save data in a fixed format like
[setting_name] = [Setting_value]\n
or in XML file, with Tag name for setting and value for... well... value :)
You can also go with registry setttings but user don't feel it trivial to copy and merge .reg files
This is the way i have seen some PC Games (for eg. i frequently changed Crysis and MassEffect settings) and Softwares save their config files.

C# project installer - where are user settings saved in an installation context?

I'm trying to set some user configurations in an installer. For instance, I'm using:
Properties.Settings.Default.mapURL = txtBoxMapURL.Text.Trim();
Properties.Settings.Default.Save();
in a Windows Form that the installer class calls. However, upon launching the application, the setting doesn't persist. The next time I try to configure the setting in the installer, it reads the correct value into the textbox. So it's saving the setting somewhere, I'm just having a hard time figuring out where.
It's not being saved in C:\program files\[manufacturer]\[product]\[product].exe.config, and also not in C:\Documents and Settings\[User]\Local Settings\Application Data\[Manufacturer][Product].exe\user.config.
Any idea where the installer is temporarily storing the setting, and is there a way to store a user setting during an installation?
The place they get saved to is the user.config... check: http://msdn.microsoft.com/en-us/library/aa730869(VS.80).aspx
Is the scope of your settings 'user' and not 'application' - as the application guys can't be saved.
Do check THIS out: How can I set application settings at install time (via installer class) ... seems like a similar issue.
The user.config file was being updated from the installer, but it is saving and reading the config (for my particular application) from:
C:\Documents and Settings\Long\Local Settings\Application Data\Microsoft_Corporation\DefaultDomain_Path_w551cnaciyzcylzfdpgyceaw05mmrhk0\3.1.4001.5512\user.config.
Unless there's another way to update the correct user.config file, it looks like I'll have to run a runonce type of thing when the application is first launched.

C# Settings xml file location

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?

Categories

Resources