C# MVVM project targeting .NET Framework 4.6.2 with underlying SQL Server database.
We have a handful of user-writable settings in appSettings. These are stored in the PROGRAMNAME.exe.config in C:\Program Files\PROGRAMNAME
The problem is that our users typically do not have write permissions to the C:\Program Files\PROGRAMNAME directory and so cannot write changes to this file. The program either needs to be run as an administrator or we need to give write permissions to that directory. Neither is ideal.
I was under the impression that it should create a copy of the .exe.config in C:\Users\USERNAME\AppData\Local\VirtualStore\Program Files\PROGRAMNAME but this does not seem to happen, especially in our Citrix environments.
Is there a way to relocate these specific appSettings to a file in a user-writable location?
You can do it. Read the config file like this:
Configuration App_Config =
ConfigurationManager.OpenExeConfiguration("C:\Temp\PROGRAMNAME.exe");
By this, it will look for the config file PROGRAMNAME.exe.config in the specified path C:\Temp
Please notice the path is not specified as C:\Temp\PROGRAMNAME.exe.config
Get more info from Here
Related
I can copy files to the C:\Program Files (x86)\... ...but only with the administrator account.
This file is a configuration file like App.Config, but with a different name, and I want to put it into the application path's sub folder.
The scenario is like following.
Put the initial configuration file in the \Cfg\org.cfg
When start application program,myApp.exe, check if configuration file exists in the application path.
If it doesn't exist, copy org.cfg to the myApp.exe.config and load it.
I tried DirectoryInfo.SetAccessControl() but following error occur as well.
"Attempted to perform an unauthorized operation"
Program Files (x86) required administrative permission to write to. So does changing the permissions, which is why SetAccessControl fails. There's really no way around that. If you want to have your configuration file writable by the application, then you might consider changing the permission of the configuration file during installation of the program when you do have administrative access.
I would instead however recommend putting your configuration file somewhere in the user's profile, which is writable by default for all users, like in %LOCALAPPDATA%.
You can load configuration files from arbitrary locations as well, it doesn't have to be in the .config file next to the executable. For an example of that, look at this question.
I have a C# executable reading and updating it's configuration file (app.exe.config) at runtime, specifically, the "appSettings" section. After I make two changes to two key values and save them using the Configuration.Save() method, an "access to the path" error is thrown, but only on some users' machines (so far 2 reports out of 10,000). We have never seen this issue in-house, only in production.
The Configuration.Save() method seems to be the issue based on our program's log file. When a problem with this call happens a "ConfigurationErrorsException" occurs because the configuration file could not be written to -or- the configuration file has changed. The configuration file is in the same directory as the application. This is a Dot Net 4 application running on a Windows 7 PC.
The code is:
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
configuration.AppSettings.Settings["Last Updated Package"].Value = packageVersion;
configuration.AppSettings.Settings["Copy Updated Files"].Value = bool.TrueString;
configuration.Save(ConfigurationSaveMode.Minimal);
ConfigurationManager.RefreshSection("appSettings");
The error is:
An error occurred loading a configuration file: Acces to the path 'C:\Program Files (x86)\My Program\bin\guaixcax.tmp' is denied. (C:\Program Files (x86)\My Program\bin\MyProgram.exe.Config)
Is this a permissions issue, or can I add something to either the code or config file to fix this issue?
I have had similar issues with .NET program installations. It has always been a permissions issue. To test if this is your problem, you should try running your application as an unprivileged user - developers tend to have administrator rights...whenever they can manage it.
If it fails, edit the security of the config file (as an administrator) to allow the "Users" group write access to your "bin" folder, and try it again. If it works, you've identified the problem. Then you can figure out what the best solution is. Some options would be:
Set access rights to necessary files/folders from your installer to work for the users that will run the application
Run the application as a user with admin rights (NOT a good idea from a security best-practices point of view)
Instead of modifying the main app settings file, create a separate settings section referencing a file that has the data that can be changed:
<configuration>
<configSections>
...
<section name="MyChangableSettings" type="My.Namespace, ChangableSettingsClass"/>
</configSection>
...
<MyChangableSettings configSource="Path\To\Writable\StorageDir\mySettings.config"/>
</configuration>
I'm trying to access the user.config file of another application in C#. I understand that I can use ConfigurationManager.OpenExeConfiguration().
I am talking about the user.config that is stored under AppData\Local\MyApp, not the configuration file I find in the same folder as the application.
Is this even possible?
The full path of the application data is in this form:
C:\Users\markn\AppData\Local\MyCompany\MyApp.exe_Url_pn34t01n4nbvali2df02umn4jhetpy0c\5.25.4969.26783
I just hope there is a call in the .NET framework somewhere that will allow me access to this file, without my having to resort to recursing all the directories looking for the file.
Thanks a lot.
Mark
I have an SQL Server CE 4.0 (SDF) file that under the Users Application Data Folder (because due to Win7 UAC you cannot modify the DB if it is in the \Programs Files\ folder. But the problem remains that I need to now (on install) modifiy the .EXE.CONFIG file (ConnectionString) to point to the new path for the DB, however obviously I do not have access to modify the .EXE.CONFIG file either in the \Program Files\ folder ...
So how do people resolve this issue?
Can you deploy the .EXE.CONFIG file to the users Application Data Folder as well with the SDF file? If not how? I cannot beleive you need to grant special access rights to the \Program Files\Application folder to be able to have full access...
I am using VS2010 and using a standard Deployment Project (MSI) and I don't see anyway to deploy the .EXE in one place the the .EXE.CONFIG in another (can this be done? Is this the right solution?)
Thanks,
Use the %APPDATA% variable in the connection string and you won't have to modify the configuration file. This is also more flexible than hard-coding a path that contains a username, since it will allow all users of the computer to use your program and have their own .sdf files.
However, the environment variables are not automatically expanded in .NET, so you need to enforce it by using the Environment.ExpandEnvironmentVariable method:
// Example appSetting element:
// <add key="examplePath" value="%APPDATA%\example.txt" />
string path = System.Configuration.ConfigurationManager.AppSettings["examplePath"].ToString();
path = Environment.ExpandEnvironmentVariables(path);
System.IO.File.WriteAllText(path, "foobar");
For testing and debugging purposes, I just hard-coded the file paths for the text files that would be used by my application, e.g.
const string CONFIG_FILE_PATH = #"C:\myconfigfile.txt";
But I don't think it's a good idea to leave it as it is in the Beta/Release version.
So I am wondering, what would be the best location for saving these configuration files that will be used / read by the application? Any suggestions?
Many Thanks.
Why not save the strings in the Settings section of your project? GRight click on your project in Solution Explorer, select Properties, go to the Settings section and add a new string with your file path. Then, in your code, you can access it like this:
using ProjectName.Properties;
var path = Settings.Default.MySetting;
To change the setting:
Settings.Default.MySetting = newPath;
Settings.Default.Save();
In the same folder as the executable.
But you should consider using a Settings class (you can read more here). Visual Studio can automatically create a strongly typed wrapper around a section in the app.config file.
The settings are stored in the same file as the executable, but can be overridden (and saved from the application) in a matching file in the user profile for each user.
Another option: If the test config file is intended to sit along side your executable, you can "Add" "Existing Item..." to your project and then change its properties to "Copy always" or "Copy if newer". During debugging, the executable should be able to find the copy of the config in its current working directory.
This isn't such a useful solution when there's a lot of testing to do.
For settings I would certainly use the app.config file. This is proposed by Microsoft and apart from that it is pretty much the easiest way to handle application settings anyway.
For other files I'd recommend either the applications local or roaming settings path, depending on weather you only need the data local or not. For compact, local databases I tend to use this approach. The application directory, as suggested by Albin, is a bad idea. You cannot be sure that the user is actually allowed to write to that directory and/or files in that directory (i.e. the app was pre-installed by an admin with elevated rights).
To get the location of the local paths use
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal)
and for the roaming path
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming)
More information on Windows User Profiles (the actual paths in different versions of Windows for example, you can find here: http://msdn.microsoft.com/en-us/library/aa372123.aspx