开发者

C# -> Updating an AppSettings.config file on Win7/Vista

I can't see anything on here but I do remember being told that If you want an application to update a config file then it needs to be under ...

**C:\Users\Ibrar Mumtaz\AppData**

Well somewhere there, the reason being is that the user should have permisions to update a config file here and not under the applications install folder. This is the impression that I am under and I'm fairly certain that this is definately the case. As I think I read that on here = p

My question is, is there anybody on here that can shine some light on this as this is the last feature I want to implement before I give my appl开发者_开发百科ication out to test.

1) First thing is, an installer is needed to set up the folder and then drop my apps config file into it. I already am using the visial studio installer so I have my app packaged up but this point is throwing me off? How do I do this then? I just need someone to show how to do this and I should be O.K reconfiguring my app to look for the new home of the config file.

2) I should be able to work out how to find the folder and locate the config file found within it. As once I know the installer is chucking the config file out into the right folder where the user has permissions then it should be straight forward from there.

Thanks for reading.

UPDATE:

It was pretty straight foward, as the VS Installer has an option to add a special folder so all that was left was to access the folder programmatically and read and write to the config file. ONE PROBLEM? The ConfigurationManager class which I have used to create my config file for my application expects my config file to be local to the application and not miles away in a completey different part of the local FileSystem? Errr help here Plz?

Ibrar


If you are using the VS Settings file to create application setting keys, and have values that the user might want to change in runtime, and save his preferences, just set the scope of those settings to "User" instead of "Application".
That way you will have a setter method for them, and you can edit the Settings.Default instance, and when you are done call the Save() method to persist them to disk.

The file will be saved in the user's "AppData" folder, wherever it is, under some cryptic folder. But you needn't worry about it's location most of the time, since it will be read automatically on the next execute, and persisted to the same location on subsequent runs.


Afaik the installer can be extended with classes that do things.

On INSTALL-action to do could be to

var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                    "My app name");
if (!Directory.Exists(path)) Directory.CreateDirectory(path);

And vice-versa on uninstall.

App.config files are related to where the physical assembly is located I think.


Actually, if your app is running on the user's machine- it will have whatever permissions that user has. So most likely, you can expect to be able to write anywhere on the file system.

However it is possible the user would be running under a restricted acct, and thus not have the permissions. So you could just use the registry to store where your config file is (install folder), then when you try to update it, if it fails for permissions, ask the user to grant it.

Or you could use the Windows standard folders, as you were getting at, because doing so also separates out user data from application data.

Use the Environment.GetFolderPath () method to get the 'special folder' paths in your app.

http://www.programmersheaven.com/2/Les_CSharp_15_p2

http://msdn.microsoft.com/en-us/library/14tx8hby.aspx


If you are talking about application settings found on project Properties -> Settings tab, then there're two different types of settings: user-level and application-level.

If you need to change any settings in run-time, these would be user-level settings (http://msdn.microsoft.com/en-us/library/cftf714c.aspx) and all changes would be buried somewhere in the private folder in your user profile.

If you want to update application-level settings, your best shot would be to do that during software installation. In this case you don't need to look for the configuration file (YourApp.exe.config) anywhere but in the application folder. More likely you would need to create some sort of post-install event handler in your setup package and run some script or another application which would update data in YourApp.exe.config. Everything in the setup package will be executed with elevated priviledges and thus that configuration file would be writeable. BTW, this scenario applies to 2000 and XP, if the user is using limited user account type priviledges.


Because I did not technically find the answer I was looking for, after 6 months I have come back to my application and have managed to produce a solution that does not break my current architecture.

If you are implementing an application to make use of some of the features on offer by the ConfigurationManager then it offers a static method called:

ConfigurationManager.OpenMappedExeConfiguration(); // takes two arguements.

It can be used like this:

        ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
        fileMap.ExeConfigFilename = returnUsersAppDataFolderPath();

        Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

e.g

fileMap.ExeConfigFileName = @"some file path external to your applications install folder."

remember to use '@' symbol in front to allow the compiler to literally treat the string on as is basis.

If the config file can be conveniently locally located then just use the:

ConfigurationManager.OpenExeConfiguration(string exePath)

Above is what you would typically use but for me i needed my config file to located under the users AppData folder so the first option is what I needed. And it does indeed work.

I hope this helps others as it does for me as I want to deploy my application to Win7 and vista environments therefore this question needed asking if I was to stick to using the ConfigurationManager it's a shame the method of choice in the end never really stood out in the first place = ).

If you want to read from your config file then leave a comment and I will show you how I managed to do that also.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜