Results 1 to 16 of 16

Thread: [2005] My.Settings Question

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    [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?

  2. #2
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: [2005] My.Settings Question

    You can set default values so all users start out at the same values.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  3. #3
    Fanatic Member
    Join Date
    Aug 2006
    Location
    In my head
    Posts
    913

    Re: [2005] My.Settings Question

    You can add a custom settings file. Right click on your project and then Add -> New Item. There is a resources option and a settings option. You put your settings in there and access them as you would a class.

    Good Luck!!

    D
    Platforms of choice: Visual Studio 2005/2008 Professional : Visual Studio 2010 Enterprise : PHP - Notepad++/WAMP

    Please Rate If I helped you.
    Please remember to mark threads as closed if your issue has been resolved.

    Reserved Words in Access | Connection Strings

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    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.

  5. #5
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: [2005] My.Settings Question

    You cannot remove the ReadOnly attribute of a setting with application scope.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  6. #6
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    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.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    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.

  8. #8
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: [2005] My.Settings Question

    I would use XML instead of INI. You could also password protect the settings menu.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    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.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    Junior Member
    Join Date
    Sep 2006
    Posts
    16

    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?

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] My.Settings Question

    Quote 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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    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.

  14. #14
    Junior Member
    Join Date
    Sep 2006
    Posts
    16

    Re: [2005] My.Settings Question

    Thanks, this really helps.

  15. #15
    Lively Member
    Join Date
    Jun 2007
    Posts
    115

    Re: [2005] My.Settings Question

    Quote 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?

  16. #16
    Lively Member
    Join Date
    Jun 2007
    Posts
    115

    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
  •  



Click Here to Expand Forum to Full Width