Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
144 views
in Technique[技术] by (71.8m points)

c# - How do I get around application scope settings being read-only?

What use are they if they cannot be altered from their default values anyway? Rhetorical question.

First, what's the best way to circumvent the Settings system and write to the application scope settings file directly (security issues in the next paragraph)? XmlReader/XmlWriter?

IIRC, if an application tries to write to its Program Files folder, Windows Vista and 7 does some magic to prevent that. I suppose I need to call UAC to elevate and I must put that fancy shield icon on my application's Options window's OK button and so on. Link to a quick how-to?

Alternatively, you may tell me that what I'm trying to do is stupid and I should store my alterable application scope settings somewhere else altogether.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you change them to "user" settings they can be changed in code, and when you call Save() they will be saved to a user.config file in the current users local settings folder.

Obviously, this means they can be different for every user. Generally, global application settings that are the same for every user aren't modified in code because a change that one user makes will effect everyone else (hence the app settings are read only).

If you don't want them to be user scoped, take a look at the ConfigurationManager class. This will allow you to manually read and write to .config files. Remember though that the c:program files folder is protected and normal users won't have access to it (this will result in UAC prompts or failure in vista/win7). Consider carefully how you will handle it, and remember that any change to an app.config will affect all users.

There is no location in windows that all users are guaranteed to have write access to.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...