|
-
May 31st, 2007, 08:46 AM
#1
Thread Starter
Hyperactive Member
[2005] My.Settings Question
I'm using My.Settings to store configuration items but I need to assign the values to the machine not the the user. I can't use application as the scope because I need to allow the end user to set this configuration. I need every user at a sight to have the same settings. The problem is that when the scope is set to User, each user at a sight can have a different setting.
Is there a way to set the configuration at the machine/application level and still allow the user to have control?
-
May 31st, 2007, 08:51 AM
#2
Re: [2005] My.Settings Question
You can set default values so all users start out at the same values.
-
May 31st, 2007, 08:53 AM
#3
Fanatic Member
-
May 31st, 2007, 10:25 AM
#4
Thread Starter
Hyperactive Member
Re: [2005] My.Settings Question
circuits2 - I can set defaults but there's no guarantee that the data from all users will always be the same.
dminder - I found this link that describes your suggestion but it seems to me that if I have to decorate the property with either UserScopedSettingAttribute or ApplicationScopedSettingAttribute, I'm going to encounter the same problem.
Wait a minute - are you suggesting that I create a setting with an ApplicationScopedSettingAttribute and not define the property as readOnly? Let me give that a shot.
-
May 31st, 2007, 10:27 AM
#5
Re: [2005] My.Settings Question
You cannot remove the ReadOnly attribute of a setting with application scope.
-
May 31st, 2007, 10:30 AM
#6
Re: [2005] My.Settings Question
If you read your question again, it doesn't really make sense. You want all settings at a particular site to be the same, but you want to be able to change them. With this in mind, you still don't want to use User scope settings? Why not? Check out the My.Settings link in my signature.
-
May 31st, 2007, 10:54 AM
#7
Thread Starter
Hyperactive Member
Re: [2005] My.Settings Question
You cannot remove the ReadOnly attribute of a setting with application scope.
Well, you can remove it but the change gets overwritten as soon as you save the file.
I'm looking at the link fromyour sig now.
If you read your question again, it doesn't really make sense. You want all settings at a particular site to be the same, but you want to be able to change them. With this in mind, you still don't want to use User scope settings? Why not?
I'm developing code for training facilities. Which, of course, has numerous instructors. Each facility has different guidelines as to how they operate. Some have all instructors log on as "instructor" others provide a different user name for each instructor.
Because my application will be going to numerous facilities I have provided options (not hard coded) that should be set on a per facility basis. I can't allow instructor1 to operate differently from instructor2.
For example, I have an option that will automatically delete files if a certain criteria is met. If this option is set true, the file is gone as soon as you close the application. If it is set false the user will be promted as to whether the file should be deleted. This setting for this value is determined administratively by each facility so I have to give them the ability to set this value but I can't have instructor1 doing one thing and instructor2 doing something else.
In the case that all instructors log on as "instructor", all is well. It's the other case that I'm trying to prevent problems in. A co-worker wants to go back to an INI file and we can simply look at that one file to set our configuration.
Hope this makes a little more sense.
-
May 31st, 2007, 11:01 AM
#8
Re: [2005] My.Settings Question
I would use XML instead of INI. You could also password protect the settings menu.
-
May 31st, 2007, 01:23 PM
#9
Thread Starter
Hyperactive Member
Re: [2005] My.Settings Question
A settings provider defines the mechanism for storing configuration data used in the application settings architecture. The .NET Framework contains a single default settings provider, LocalFileSettingsProvider, which stores configuration data to the local file system. However, you can create alternate storage mechanisms by deriving from the abstract SettingsProvider class. The provider that a wrapper class uses is determined by decorating the wrapper class with the SettingsProviderAttribute. If this attribute is not provided, the default, LocalFileSettingsProvider, is used.
dminder - Is this what you were refering to? I'm looking at it now but this may be a little over my head.
-
May 31st, 2007, 07:33 PM
#10
Re: [2005] My.Settings Question
Settings with User scope are stored in a config file under the current user's Documents & Settings folder. Every user has write access to their own Documents & Settings folder so it is safe for User-scoped settings to be read/write via My.Settings.
Settings with Application scope are stored in a config file under the Program Files folder. Users with limited accounts do not have write access to the Program Files folder so Application-scoped settings are read-only via My.Settings.
This does not mean that you cannot edit Application-scoped settings. It just means that you cannot edit them via My.Settings. Look up the OpenExeConfiguration method for a code example of reading, editing and saving Application-scoped settings.
Note that you should always check that the current user has the required privileges first, or trying to save will throw an exception. You would use the My.User.IsInRole method for that.
-
Jun 18th, 2007, 11:53 PM
#11
Junior Member
Re: [2005] My.Settings Question
Campster, have you figure out a solution to this one? Cause I'm having the same problem. Others don't seem to understand this situation, the need of having a configuration settings regardless of users or rights (global settings just like INI files). They always insist on My.Settings or asking why the need to change settings. I researched about OpenExeConfiguration but haven't found a straightforward tutorial. I'm having headache about this, so I resort back to INI which at least did the job. What did you do about this?
-
Jun 19th, 2007, 05:31 AM
#12
Re: [2005] My.Settings Question
 Originally Posted by sinestro
Campster, have you figure out a solution to this one? Cause I'm having the same problem. Others don't seem to understand this situation, the need of having a configuration settings regardless of users or rights (global settings just like INI files). They always insist on My.Settings or asking why the need to change settings. I researched about OpenExeConfiguration but haven't found a straightforward tutorial. I'm having headache about this, so I resort back to INI which at least did the job. What did you do about this?
You can simply define your own class and serialise it to XML wherever you like, the All Users application data folder for instance.
As for a tutorial on the OpenExeConfiguration method, that's hoping for a bit much. It's one method and its help topic has a code example. What do you need to know that that help doco doesn't provide?
-
Jun 19th, 2007, 06:33 AM
#13
Thread Starter
Hyperactive Member
Re: [2005] My.Settings Question
What did you do about this?
I found this class that uses an XML file as your INI. I guess that I could have written something myself but I was in a hurry. I'll probably go back and customize this class when I get a chance (like thats's going to happen).
I create the XMLINI in the same directory that the executable is being run. If the file does not exist (which it shouldn't upon initial release) I create the file with default settings. After that I create a form to allow the user to update/edit the configuration as they like.
Hope this helps.
-
Jun 19th, 2007, 06:59 AM
#14
Junior Member
Re: [2005] My.Settings Question
Thanks, this really helps.
-
Jul 15th, 2007, 05:47 PM
#15
Lively Member
Re: [2005] My.Settings Question
 Originally Posted by jmcilhinney
Settings with User scope are stored in a config file under the current user's Documents & Settings folder. Every user has write access to their own Documents & Settings folder so it is safe for User-scoped settings to be read/write via My.Settings.
Settings with Application scope are stored in a config file under the Program Files folder. Users with limited accounts do not have write access to the Program Files folder so Application-scoped settings are read-only via My.Settings.
This does not mean that you cannot edit Application-scoped settings. It just means that you cannot edit them via My.Settings. Look up the OpenExeConfiguration method for a code example of reading, editing and saving Application-scoped settings.
Note that you should always check that the current user has the required privileges first, or trying to save will throw an exception. You would use the My.User.IsInRole method for that.
If you use Application Scope can admin accts edit through My.Settings?
-
Jul 16th, 2007, 09:39 AM
#16
Lively Member
Re: [2005] My.Settings Question
Ok I found an answer here:
http://www.codeproject.com/vb/net/Ap...51&msg=2131651
Credit to Levan Midodashvili.
I changed his code slightly to use parameters...
You must use the following imports
Code:
Imports System.Configuration
Imports System.Xml
and add reference System.Configuration.
Create your settings as normal, through propertybinding if you like but change the scope to application in the property grid window of project properties.
Also disable vshost in the Debug part of your project properties, not sure but I think this makes it look at two different files and the settings do not hold.
My code could probably be improved to serialize objects such as font, color etc but I'm a newbie so I just convert everything to string on my own before the call.
Add this procedure and call it whenever you want to change an application setting. Remember both arguments are strings.
Code:
Public Sub ChangeMyAppScopedSetting(ByVal AppSettingName As String, ByVal newValue As String)
Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
Dim xmlDoc As New XmlDocument()
' Load an XML file into the XmlDocument object.
Try
xmlDoc.PreserveWhitespace = True
xmlDoc.Load(config.FilePath.Trim)
Catch ex As Exception
MsgBox(ex.Message)
End Try
Dim i, j, k, l As Int32
Try
For i = 0 To xmlDoc.GetElementsByTagName("applicationSettings").Count - 1
For j = 0 To xmlDoc.GetElementsByTagName("applicationSettings").Item(i).ChildNodes.Count - 1
For k = 0 To xmlDoc.GetElementsByTagName("applicationSettings").Item(i).ChildNodes.Item(j).ChildNodes.Count - 1
If xmlDoc.GetElementsByTagName("applicationSettings").Item(i).ChildNodes.Item(j).ChildNodes.Item(k).Name = "setting" Then
If xmlDoc.GetElementsByTagName("applicationSettings").Item(i).ChildNodes.Item(j).ChildNodes.Item(k).Attributes.Item(0).Value = AppSettingName.ToString Then
For l = 0 To xmlDoc.GetElementsByTagName("applicationSettings").Item(i).ChildNodes.Item(j).ChildNodes.Item(k).ChildNodes.Count - 1
If xmlDoc.GetElementsByTagName("applicationSettings").Item(i).ChildNodes.Item(j).ChildNodes.Item(k).ChildNodes.Item(l).Name = "value" Then
' MsgBox(xmlDoc.GetElementsByTagName("applicationSettings").Item(i).ChildNodes.Item(j).ChildNodes.Item(k))
xmlDoc.GetElementsByTagName("applicationSettings").Item(i).ChildNodes.Item(j).ChildNodes.Item(k).ChildNodes.Item(l).InnerText = newValue
End If
Next l
End If
End If
Next k
Next j
Next i
xmlDoc.Save(config.FilePath.Trim)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
my saving a font code is hairy and probably could be improved with serialization:
Code:
Dim fontstr As String
fontstr = frm2.ListView1.Font.FontFamily.ToString & "," & frm2.ListView1.Font.SizeInPoints & _
"pt ," & "style=" & frm2.ListView1.Font.Style.ToString
' myFont is the name of my Application setting variable
ChangeMyAppScopedSetting("myFont", fontstr)
for colordialog I create an RGB string and pass that (i.e "255, 0, 255")
Last edited by jeffnyc; Jul 16th, 2007 at 06:50 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|