Results 1 to 11 of 11

Thread: How can I Get the Path to My Programs User Setting Config File?

  1. #1

    Thread Starter
    Lively Member ThatSamiam's Avatar
    Join Date
    Apr 2007
    Posts
    125

    How can I Get the Path to My Programs User Setting Config File?

    It will be something like this:
    C:\Users\MyUsername\AppData\Local\MyCompany\MyProject.vshost.exe_Url_2bs02quk0ykxx0zsyuqtxcemq1w4kyq x\1.0.0.0\user.config

    These don't do it, but it is as close as I got.
    Dim s1 As String = Path.GetDirectoryName(System.Windows.Forms.Application.LocalUserAppDataPath)
    Dim s2 As String = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)

    Does anyone know how to find this?

    Thanks.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: How can I Get the Path to My Programs User Setting Config File?

    Why exactly do you want to do that? I can't think of a good reason that you'd need to so there's a very good chance that what you're trying to do is misguided.

  3. #3

    Thread Starter
    Lively Member ThatSamiam's Avatar
    Join Date
    Apr 2007
    Posts
    125

    Re: How can I Get the Path to My Programs User Setting Config File?

    Quote Originally Posted by jmcilhinney View Post
    Why exactly do you want to do that? I can't think of a good reason that you'd need to so there's a very good chance that what you're trying to do is misguided.
    I am aware of the risk, although I do appreciate the warning.

    This is the location of various configuration settings (com ports, IP addresses, file paths, etc.) for a test platform that will be used at overseas contract manufacturer sites. All the settings are read in at the beginning of the process, so there is little risk of change-on-the-fly affecting the process in progress. At least it is a risk I am willing to take.
    My intent is to simply display the location so the local Engineer can see, and perhaps change the settings. I do have an interface to change the settings that is not accessible while the tests are running. They already know where to access the Application config file; I want to show where the User config file is.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: How can I Get the Path to My Programs User Setting Config File?

    Quote Originally Posted by ThatSamiam View Post
    I am aware of the risk, although I do appreciate the warning.

    This is the location of various configuration settings (com ports, IP addresses, file paths, etc.) for a test platform that will be used at overseas contract manufacturer sites. All the settings are read in at the beginning of the process, so there is little risk of change-on-the-fly affecting the process in progress. At least it is a risk I am willing to take.
    My intent is to simply display the location so the local Engineer can see, and perhaps change the settings. I do have an interface to change the settings that is not accessible while the tests are running. They already know where to access the Application config file; I want to show where the User config file is.
    That still makes no sense at all. Anything in the user config file is a User-scoped setting accessible via My.Settings in code. All you should be doing is accessing My.Settings in code and, if appropriate, providing a UI for the user to change those values. What you do is up to you but there's no good reason to actually access that file yourself so I'm afraid I don't feel inclined to help you find a way.

  5. #5
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,758

    Re: How can I Get the Path to My Programs User Setting Config File?

    I would agree with jm about not accessing the file directly, but there are cases that you may want the functionality of the My.Setting class and I want to know exactly were on the disk it is (for settings backup, portability, etc) and want to access the file directly (to hide certain settings from the users yet still be able to change them).

    To get the best of both worlds, you need to create your own mySetting class, declare it globally and add save and load functions to serialize and deserialize it to xml.
    Last edited by kebo; Nov 5th, 2014 at 05:23 PM.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  6. #6

    Thread Starter
    Lively Member ThatSamiam's Avatar
    Join Date
    Apr 2007
    Posts
    125

    Re: How can I Get the Path to My Programs User Setting Config File?

    Ok. I respect your opinions.

    JM: I have read your posts for several years now. I think you will secretly figure it out just to do it.

    Seriously, thanks for considering my question.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: How can I Get the Path to My Programs User Setting Config File?

    Quote Originally Posted by kebo View Post
    I would agree with jm about not accessing the file directly, but there are cases that you may want the functionality of the My.Setting class and I want to know exactly were on the disk it is (for settings backup, portability, etc) and want to access the file directly (to hide certain settings from the users yet still be able to change them).

    To get the best of both worlds, you need to create your own mySetting class, declare it globally and add save and load functions to serialize and deserialize it to xml.
    I would suggest that you still wouldn't need to actually know where the file is. The ConfigurationManager.OpenExeConfiguration method lets you create a Configuration object and load the user config data into it. You can manipulate that object and it has methods for saving its contents to a file.

  8. #8
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,758

    Re: How can I Get the Path to My Programs User Setting Config File?

    I would suggest that you still wouldn't need to actually know where the file is.
    No, the whole idea is to be able to get to the file and edit it outside of the app and you can't do that without knowing where it is. At least that's how understand what the OP wants to do.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  9. #9
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,758

    Re: How can I Get the Path to My Programs User Setting Config File?

    So I got reading about this a bit and found this page with this on it....
    Quote Originally Posted by msdn
    The user.config file is created in the <c:\Documents and Settings>\<username>\[Local Settings\]Application Data\<companyname>\<appdomainname>_<eid>_<hash>\<verison>. Where:
    <c:\Documents and Settings> is the user data directory, either non-roaming (Local Settings above) or roaming.
    <username> is the user name.
    <companyname> is the CompanyNameAttribute value, if available. Otherwise, ignore this element.
    <appdomainname> is the AppDomain.CurrentDomain.FriendlyName. This usually defaults to the .exe name.
    <eid> is the URL, StrongName, or Path, based on the evidence available to hash.
    <hash> is a SHA1 hash of evidence gathered from the CurrentDomain, in the following order of preference:
    StrongName
    URL
    If neither of these is available, use the .exe path.
    <version> is the AssemblyInfo's AssemblyVersionAttribute setting.
    An example path would look like the following:
    C:\Documents and Settings\Emad.BROKENOAK\Local Settings\Application
    Data\MySettingsDemo\MySettingsDemo_9cfe5ef1\1.0.0.0
    That should tell you were to find what you are looking for.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

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

    Re: How can I Get the Path to My Programs User Setting Config File?

    Quote Originally Posted by kebo View Post
    No, the whole idea is to be able to get to the file and edit it outside of the app and you can't do that without knowing where it is. At least that's how understand what the OP wants to do.
    I guess the OP can confirm either way but the fact that they know where the file is but were trying to write code to find it suggested otherwise to me, although re-reading post #3 seems a bit ambiguous.

  11. #11
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: How can I Get the Path to My Programs User Setting Config File?

    Quote Originally Posted by kebo View Post
    I would agree with jm about not accessing the file directly, but there are cases that you may want the functionality of the My.Setting class and I want to know exactly were on the disk it is (for settings backup, portability, etc) and want to access the file directly (to hide certain settings from the users yet still be able to change them).

    To get the best of both worlds, you need to create your own mySetting class, declare it globally and add save and load functions to serialize and deserialize it to xml.
    I agree ... bad practice ...

    I actually agree with this method ... kind of ... I actually implemented something like this a while back ... but used the built in settings class ... and created my own object to serialize that to a different file ...

    Here is my class:
    vb Code:
    1. 'i00 File Settings
    2. '©i00 Productions All rights reserved
    3. 'Created by Kris Bennett
    4. '----------------------------------------------------------------------------------------------------
    5. 'All property in this file is and remains the property of i00 Productions, regardless of its usage,
    6. 'unless stated otherwise in writing from i00 Productions.
    7. '
    8. 'i00 is not and shall not be held accountable for any damages directly or indirectly caused by the
    9. 'use or miss-use of this product.  This product is only a component and thus is intended to be used
    10. 'as part of other software, it is not a complete software package, thus i00 Productions is not
    11. 'responsible for any legal ramifications that software using this product breaches.
    12.  
    13.  
    14. Imports System.Xml.Serialization
    15. Imports System.IO
    16. Imports System.Text.RegularExpressions
    17.  
    18.  
    19. Namespace Settings
    20.  
    21.     Namespace AppSettings
    22.  
    23.         <Serializable()> _
    24.         Public Class clsSettings
    25.             Public Setting As New Generic.List(Of clsSetting)
    26.  
    27.             <Serializable()> _
    28.             Public Class clsSetting
    29.                 Public Key As String
    30.                 Public Value As Object
    31.                 Public Desc As String
    32.             End Class
    33.         End Class
    34.  
    35.         <Serializable()> _
    36.         Public Class sFont
    37.             Public FontName As String
    38.             Public Size As Single
    39.             Public Style As Integer
    40.         End Class
    41.  
    42.         <Serializable()> _
    43.         Public Class sColor
    44.             Public A As Byte
    45.             Public R As Byte
    46.             Public G As Byte
    47.             Public B As Byte
    48.         End Class
    49.  
    50.     End Namespace
    51.  
    52.     Public Class XMLSettings
    53.         Private WithEvents SettingsBase As System.Configuration.ApplicationSettingsBase
    54.         Private File As String
    55.  
    56.         Public Sub New(ByVal SettingsBase As System.Configuration.ApplicationSettingsBase, ByVal File As String)
    57.             Me.SettingsBase = SettingsBase
    58.             Me.File = File
    59.             userOverride_SettingsLoaded(Nothing, Nothing)
    60.         End Sub
    61.  
    62.         Public Function GetDictionaryOfSettings() As Dictionary(Of String, Object)
    63.             Dim Dict As New Dictionary(Of String, Object)
    64.             For Each appProperty As System.Configuration.SettingsProperty In SettingsBase.Properties
    65.                 Dict.Add(appProperty.Name, SettingsBase.Item(appProperty.Name))
    66.             Next
    67.             Return Dict
    68.         End Function
    69.  
    70.  
    71.         Private Function GetSettingsFile() As String
    72.             Return System.IO.Path.GetFullPath(System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, File))
    73.         End Function
    74.  
    75.         Private Sub MySettings_SettingChanging(ByVal sender As Object, ByVal e As System.Configuration.SettingChangingEventArgs) Handles SettingsBase.SettingChanging
    76.             If SettingsHaveLoaded = False Then
    77.                 userOverride_SettingsLoaded(Nothing, Nothing)
    78.             End If
    79.         End Sub
    80.  
    81.         Dim SettingsHaveLoaded As Boolean
    82.  
    83.         Public Sub userOverride_SettingsLoaded(ByVal sender As Object, _
    84.                 ByVal e As System.Configuration.SettingsLoadedEventArgs) _
    85.             Handles SettingsBase.SettingsLoaded
    86.             SettingsHaveLoaded = True
    87.             If FileIO.FileSystem.FileExists(GetSettingsFile) Then
    88.                 Try
    89.  
    90.                     Dim deser As XmlSerializer = New XmlSerializer(GetType(AppSettings.clsSettings), New Type() {GetType(Color), GetType(System.Drawing.Size)})
    91.                     Dim streamrd As New StreamReader(GetSettingsFile)
    92.                     Dim clsSettings As AppSettings.clsSettings
    93.                     clsSettings = deser.Deserialize(streamrd)
    94.                     streamrd.Close()
    95.  
    96.                     For Each Item As AppSettings.clsSettings.clsSetting In clsSettings.Setting
    97.                         Try
    98.                             If TypeOf Item.Value Is System.Array Then
    99.                                 If TypeOf Item.Value(0) Is System.Xml.XmlAttribute Then
    100.                                     Dim ValueType As System.Xml.XmlAttribute = Item.Value(0)
    101.                                     Select Case ValueType.Value
    102.                                         Case "sColor"
    103.                                             Dim sColor As Color = Color.FromArgb(Item.Value(1).innertext, Item.Value(2).innertext, Item.Value(3).innertext, Item.Value(4).innertext)
    104.                                             SettingsBase.Item(Item.Key) = sColor
    105.                                         Case "sFont"
    106.                                             Dim FS As FontStyle = Item.Value(3).innertext
    107.                                             Dim sFont As New Font(CStr(Item.Value(1).innertext), CSng(Item.Value(2).innertext), FS)
    108.                                             SettingsBase.Item(Item.Key) = sFont
    109.                                     End Select
    110.                                 End If
    111.                             Else
    112.                                 SettingsBase.Item(Item.Key) = Item.Value
    113.                             End If
    114.                         Catch ex As Exception
    115.  
    116.                         End Try
    117.                     Next
    118.                 Catch ex As Exception
    119.  
    120.                 End Try
    121.             End If
    122.  
    123.         End Sub
    124.  
    125.         Private Sub userOverride_SettingsSaving(ByVal sender As Object, _
    126.                 ByVal e As System.ComponentModel.CancelEventArgs) _
    127.             Handles SettingsBase.SettingsSaving
    128.  
    129.             Dim clssettings As New AppSettings.clsSettings
    130.  
    131.             For Each appProperty In (From xItem In SettingsBase.Properties.OfType(Of System.Configuration.SettingsProperty)() Order By xItem.Name)
    132.                 Dim clsSetting As New AppSettings.clsSettings.clsSetting
    133.                 clsSetting.Key = appProperty.Name
    134.                 If TypeOf SettingsBase.Item(appProperty.Name) Is Font Then
    135.                     Dim OrigValue As Font = SettingsBase.Item(appProperty.Name)
    136.                     Dim ValueItem As New AppSettings.sFont
    137.                     ValueItem.FontName = OrigValue.OriginalFontName
    138.                     ValueItem.Style = OrigValue.Style
    139.                     ValueItem.Size = OrigValue.Size
    140.                     clsSetting.Value = ValueItem
    141.                 ElseIf TypeOf SettingsBase.Item(appProperty.Name) Is Color Then
    142.                     Dim OrigValue As Color = SettingsBase.Item(appProperty.Name)
    143.                     Dim ValueItem As New AppSettings.sColor
    144.                     ValueItem.A = OrigValue.A
    145.                     ValueItem.R = OrigValue.R
    146.                     ValueItem.G = OrigValue.G
    147.                     ValueItem.B = OrigValue.B
    148.                     clsSetting.Value = ValueItem
    149.                 Else
    150.                     clsSetting.Value = SettingsBase.Item(appProperty.Name)
    151.                 End If
    152.                 Dim Desc = From xItem As System.Collections.DictionaryEntry In appProperty.Attributes _
    153.                            Where TypeOf xItem.Value Is System.Configuration.SettingsDescriptionAttribute _
    154.                            Select CType(xItem.Value, System.Configuration.SettingsDescriptionAttribute)
    155.                 If Desc.Count > 0 Then
    156.                     'save the description
    157.                     clsSetting.Desc = Desc(0).Description
    158.                 End If
    159.  
    160.                 clssettings.Setting.Add(clsSetting)
    161.             Next
    162.             Dim serializer As XmlSerializer = New XmlSerializer(GetType(AppSettings.clsSettings), New Type() {GetType(Color), GetType(System.Drawing.Size), GetType(AppSettings.sFont), GetType(AppSettings.sColor)})
    163.             Dim tw As TextWriter = New StreamWriter(GetSettingsFile)
    164.             serializer.Serialize(tw, clssettings)
    165.             tw.Close()
    166.  
    167.         End Sub
    168.     End Class
    169. End Namespace

    Make all of your settings PER USER for your project... they can still be per machine (AND EDITABLE! ) ... ill explain later ... but either way ALL settings will be per user per Machine ultimately (no mixture of per user and per machine) using my method without modification.

    ... to use my class just call:
    vb Code:
    1. Dim settings As New Settings.XMLSettings(My.Settings, FILENAME_HERE)
    ...as the first line when you start your project... it will load the settings from the specified file, if it exists and is valid, if not it will load the last good settings ... or create default settings (if it hasn't been run before)... settings will be saved automatically when the project is closed (if this option is set in project properties), or when calling My.Settings.Save

    ... as I said settings can be per machine and effectively EDITABLE ... it all depends where you save and load your settings file from / to ... just remember that the user will need to have write access

    Kris

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