none
Class Project - Getting & Saving to project settings RRS feed

  • Question

  • Hey

    probably a mundane question.. i have done this plenty of times in the past but for some reason, its not working on this class project.

    Synopsis

    • Class project is an Inventor addin tool that loads with Inventor
    • Project written in VB.Net
    • Using project settings to store rudimentary information

    OK, i am saving two strings to the project settings as string types, scope = user

    • sFilePath = \\MYSERVER\Inventor\2020\Z Templates\CNC-METRIC.idw
    • sTemplateName = CNC-METRIC

    as my form loads, i an trying to get the values

    txtTemplateName.Text = My.Settings.sTemplateName
    txtFilePath.Text = My.Settings.sFilePath

    This isnt getting the values from the project settings

    On a button Click, i am trying to populate the project settings like this

    If XtraOpenFileDialog1.ShowDialog = DialogResult.OK Then
    
                Dim P As String = XtraOpenFileDialog1.FileName
                Dim oGetDoc As Inventor.DrawingDocument = AddinGlobal.InventorApp.Documents.Open(P, False)
                Dim s As String = oGetDoc.DisplayName
    
                My.Settings.TemplateFilepath = P
                My.Settings.TemplateFriendlyName = s
                My.Settings.Save()
    
                txtTemplateName.Text = s 
                txtFilePath.Text = P
                oGetDoc.Close()
                oGetDoc = Nothing
            End If

    This isnt populating the project settings either.

    So, is the problem relating to it being a Class project vs a standalone?

    is the settings file not being updated and i am in fact looking at an older version somewhere where the settings dont exist?

    i would understand not writing to the settings if its a debug thing but i cant even get the values from the settings

    Whats the deal?

    Thanks :)

    Nacho


    Nacho is the derivative of Nigel - True fact! I am self taught in VB.Net. 50% of the time, I am right 100% of the time!


    • Edited by NachoShaw Thursday, October 24, 2019 6:31 AM
    Thursday, October 24, 2019 6:31 AM

All replies

  • Hello,

    Check to see if the user.config file exists and if so when it was modified. If it does exists try deleting the entire folder and run your code again.

    Rather than hunting down the path, use this code (requires a reference to System.Configuration).

    Code module.

    Imports System.Configuration
    Imports System.IO
    
    Namespace My
        Partial Class MySettings
            Public ReadOnly Property ConfigurationFolder() As String
                Get
                    Dim config = ConfigurationManager.
                            OpenExeConfiguration(ConfigurationUserLevel.
                                                    PerUserRoamingAndLocal)
                    Return config.FilePath.Replace("\user.config", "")
                End Get
            End Property
            Public ReadOnly Property ConfigurationFolderExists() As Boolean
                Get
                    Return Directory.Exists(ConfigurationFolder)
                End Get
            End Property
            Public Function ConfigurationFile() As String
                Return Path.GetFileName(
                    ConfigurationManager.
                        OpenExeConfiguration(
                            ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath)
            End Function
        End Class
    End Namespace
    

    Run it

    If My.Settings.ConfigurationFolderExists Then
        Process.Start(My.Settings.ConfigurationFolder)
    End If


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, October 24, 2019 10:02 AM
    Moderator
  • Hi

    Thanks for the reply. Having done what you suggested, i get the following-

    user.config path is

    C:\Users\Nacho\AppData\Local\Autodesk,_Inc\DefaultDomain_Path_cm3lld1kyhtx1jfbtwpmtv0lns5501xq\Autodesk_Inventor_2020

    Where should this file be located? should it be local to the application? I did delete it and when i recompiled, it reappeared in the same place

    Next, it was combined with a bunch of other xml information pertaining to other parts of the application, now the new one only has MySettings

    lastly, i have noticed that even though some of my settings were added as Boolean, they are serialized as string. Thats incorrect right?

    Lastly, there are 2 settings missing. They are in the Project/Properties/Settings page (as highlighted) but not in the user.config file

    Thanks

    Thanks


    Nacho is the derivative of Nigel - True fact! I am self taught in VB.Net. 50% of the time, I am right 100% of the time!

    Thursday, October 24, 2019 3:50 PM
  • Hi Nacho,

    Keep in mind in general you have the settings saved in a folder when your run the project in visual studio. Then you have the settings for the installed app.exe in a different folder they are not the same. So you need to look at the settings file for the .exe you are running.

    Click the Synchronize button on the Project Settings page to delete the settings saved for the visual studio version (that's the .exe in your bin folder). After deleting the saved settings the next time you run the .exe the newest app.config file settings get copied to the user settings for the .exe you are running - the visual studio version or the installed app version.

    If you add or remove settings in visual studio then when you run if you did not delete the old settings you may get errors when your app uses the old settings. ie you added setting 43 and 44 but when you run those were old 42 and 43 so one setting is wrong the other is missing.

    If you use installer you need to consider the install setup it has an option to replace or maintain the old settings for the app.exe.

    But the visual studio settings are separate from the installed app and you must use syncronize button to delete old visual studio settings (or delete the folder manually if you can find it).

    Thursday, October 24, 2019 4:08 PM
  • Hi

    user.config in not part of my project scope bit app.config is. I set the 'Copy to Output Directory' as Copy always, removed all files from the build directory and rebuild the class project. I am not running a .exe file, i am running a .dll file inside inventor.

    I clicked Synchronise an got an error message "No user.config files were found in any of the following locations:" - there are no locations listed

    opening the newly created app.config thats been copied to the build folder, i can open it and see that the settings values are present and as required.

    so, if the project had settings with values i have entered and during a build, the app.config is created in the right place, why can i still not get any values? the value i get back for TemplateFriendlyName is FALSE

    when i check the past of app.config in my project files list, its in the root project folder so what gives?

    I guess i could remove app.config from the project and then add the one from the build folder but im not sure if the Project settings would hook up to the newly referenced one?

    thanks


    Nacho is the derivative of Nigel - True fact! I am self taught in VB.Net. 50% of the time, I am right 100% of the time!

    Thursday, October 24, 2019 6:11 PM
  • Nacho,

    I dont know how the .dll will work.

    "its in the root project folder so what gives?"

    Thats what is suppose to happen. The app.config goes with the app.exe when installed in the program folder

      C:\Program Files (x86)\CompanyName\appfolder\appname.exe

    and then when the app.exe starts somehow if there is not already a user.config the system make a copy of the app.config settings in a new user.config in the user data area:

    this is the visual studio file:

       C:\Users\username\AppData\Local\companyname\appname.vshost.exe_Url_gi5xvbxdxldn04yvyaaqffztumndymct ...

    PS installed app file

    C:\Users\username\AppData\Local\companyname\appname.exe_Url_gv3j4ske3drcltilrofpvf4ewq0jmka1\10.2.5.0\user.config

    So you dont have or dont access the user config apparently? It may not be updating? May not be saved? I dont know wha you need for the dll? What did you expect to happen? Why not use my.settings??

    Since you have a dll the settings is happening somewhere else in the .exe? I dont know?  Can you use the app my.settings now if you ref them to your dll??? Or how were you expecting it to work.

    So you should be using my.settings.TemplateFriendlyName, correct?

    I dont understand can you use my.settings in the .dll?

    What are you expecting to happen? Are you just reading the app.config file? App.config never changes after installation with the app.exe. You need to read user.config file. But even more you should have them referenced in your dll and use my.settings.myvalue???

    Thursday, October 24, 2019 6:42 PM
  • Hi

    to try and answer each line, i have copied your reply and added my comments directly below each line

    Thats what is suppose to happen. The app.config goes with the app.exe when installed in the program folder

    i would expect the app.config file to be copied and go with the compiled project

      C:\Program Files (x86)\CompanyName\appfolder\appname.exe

    and then when the app.exe starts somehow if there is not already a user.config the system make a copy of the app.config settings in a new user.config in the user data area:

       C:\Users\username\AppData\Local\companyname\appname.vshost.exe_Url_gi5xvbxdxldn04yvyaaqffztumndymct

    So you dont have or dont access the user config apparently? It may not be updating? May not be saved? I dont know wha you need for the dll? What did you expect to happen? Why not use my.settings??

    the need for a dll is because its an addin for Autodesk Inventor. We basically write a winform type application but compiled as a DLL so that Invnetor can load it at runtime. I expect that any settings i create in the project settings should be accessible by using My.Settings

    Since you have a dll the settings is happening somewhere else in the .exe? I dont know?  Can you use the app my.settings now if you ref them to your dll??? Or how were you expecting it to work.

    I am trying to get value from the settings by usng My.Settings.MySetting but the values always return as FALSE, not the values that are in the settings. See images in previous reply

    So you should be using my.settings.TemplateFriendlyName, correct?

    i should, but for whatever reason i cannot

    I dont understand can you use my.settings in the .dll?

    i though that i should be able to but i cannot. I have populated the setting in the class project but i cannot get them from the settings

    What are you expecting to happen? Are you just reading the app.config file? App.config never changes after installation with the app.exe. You need to read user.config file. But even more you should have them referenced in your dll and use my.settings.myvalue???

    there isnt a user.config file in my project folder, i have checked. i ran the code from Karen and it lead me to user.config file that was buried somewhere deep in appdata\local

    C:\Users\Nacho\AppData\Local\Autodesk,_Inc\DefaultDomain_Path_cm3lld1kyhtx1jfbtwpmtv0lns5501xq\Autodesk_Inventor_2020

    This file appears to have the same data that i am collecting i.e. FALSE. It must be referenced somehow because the setting names are the same as my project settings names. question then is, how do i reference this back to My.Settings so that i can get/set the values from this file? Even though it appears i may be getting the values from this file, when i use My.Settings.Save(), the values are not saved here, they are saved in app.config...



    Nacho is the derivative of Nigel - True fact! I am self taught in VB.Net. 50% of the time, I am right 100% of the time!

    Thursday, October 24, 2019 7:01 PM
  • Nacho,

    "there isnt a user.config file in my project folder, i have checked."

    What project folder are you talking about? Visual Studio? Installed application folder?

    "question then is, how do i reference this back to My.Settings so that i can get/set the values from this file? "

    Yeah. That is all correct.

    In a nice world there should be a way to ref the my.settings from the "project" to the dll and then you would

      my.settings.value1 = theValue
      My.settings.save

    Is there an event you can handle in your class my.settings changed or etc. I dont know I should stop guessing.

    PS the user.config should be in the path Karen's variable gives:

      C:\Users\Nacho\AppData\Local\Autodesk,_Inc\DefaultDomain_Path_cm3lld1kyhtx1jfbtwpmtv0lns5501xq\Autodesk_Inventor_2020

    that is only part of the path you show.


    Karen and the others will have to tell us how it works with the .dll.

    :)


    Thursday, October 24, 2019 7:23 PM
  • Hi

    see points below

    "there isnt a user.config file in my project folder, i have checked."

    What project folder are you talking about? Visual Studio? Installed application folder?

    this project folder

    "question then is, how do i reference this back to My.Settings so that i can get/set the values from this file? "

    Yeah. That is all correct.

    i dont understand

    In a nice world there should be a way to ref the my.settings from the "project" to the dll and then you would

    the My.Settings are in the dll. Its a class project. I am not referencing a dll and trying to get those settings, i am building a dll and want to use the settings inside the class project

      my.settings.value1 = theValue
      My.settings.save

    Is there an event you can handle in your class my.settings changed or etc. I dont know I should stop guessing.

    Karen and the others will have to tell us how it works with the .dll.

    to try  and make it even more clear-

    • I have created a Class project (DLL) from scratch
    • i have added my form & controls etc and the code i need for Inventor in the project
    • i want to utilise the settings inside the project i am building

    These are the settings that i cannot get values from. i should be able to do something simple like

    Dim GetTemplatePath As String = My.Settings.TemplateFilePath

    but all i get is a return value of False

    in the same project, i expended My Project / Settings.settings / Settings.Designer.vb and get this code which appears to have all the right settings & values so why am i unable to access them?

    '------------------------------------------------------------------------------
    ' <auto-generated>
    '     This code was generated by a tool.
    '     Runtime Version:4.0.30319.42000
    '
    '     Changes to this file may cause incorrect behavior and will be lost if
    '     the code is regenerated.
    ' </auto-generated>
    '------------------------------------------------------------------------------
    
    Option Strict On
    Option Explicit On
    
    
    Namespace My
        
        <Global.System.Runtime.CompilerServices.CompilerGeneratedAttribute(),  _
         Global.System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "15.9.0.0"),  _
         Global.System.ComponentModel.EditorBrowsableAttribute(Global.System.ComponentModel.EditorBrowsableState.Advanced)>  _
        Partial Friend NotInheritable Class MySettings
            Inherits Global.System.Configuration.ApplicationSettingsBase
            
            Private Shared defaultInstance As MySettings = CType(Global.System.Configuration.ApplicationSettingsBase.Synchronized(New MySettings()),MySettings)
            
    #Region "My.Settings Auto-Save Functionality"
    #If _MyType = "WindowsForms" Then
        Private Shared addedHandler As Boolean
    
        Private Shared addedHandlerLockObject As New Object
    
        <Global.System.Diagnostics.DebuggerNonUserCodeAttribute(), Global.System.ComponentModel.EditorBrowsableAttribute(Global.System.ComponentModel.EditorBrowsableState.Advanced)> _
        Private Shared Sub AutoSaveSettings(sender As Global.System.Object, e As Global.System.EventArgs)
            If My.Application.SaveMySettingsOnExit Then
                My.Settings.Save()
            End If
        End Sub
    #End If
    #End Region
            
            Public Shared ReadOnly Property [Default]() As MySettings
                Get
    
    #If _MyType = "WindowsForms" Then
                                   If Not addedHandler Then
                                        SyncLock addedHandlerLockObject
                                            If Not addedHandler Then
                                                AddHandler My.Application.Shutdown, AddressOf AutoSaveSettings
                                                addedHandler = True
                                            End If
                                        End SyncLock
                                    End If
    #End If
                    Return defaultInstance
                End Get
            End Property
            
            <Global.System.Configuration.UserScopedSettingAttribute(),  _
             Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
             Global.System.Configuration.DefaultSettingValueAttribute("Part No. - ")>  _
            Public Property PartNumber() As String
                Get
                    Return CType(Me("PartNumber"),String)
                End Get
                Set
                    Me("PartNumber") = value
                End Set
            End Property
            
            <Global.System.Configuration.UserScopedSettingAttribute(),  _
             Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
             Global.System.Configuration.DefaultSettingValueAttribute("Description - ")>  _
            Public Property Description() As String
                Get
                    Return CType(Me("Description"),String)
                End Get
                Set
                    Me("Description") = value
                End Set
            End Property
            
            <Global.System.Configuration.UserScopedSettingAttribute(),  _
             Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
             Global.System.Configuration.DefaultSettingValueAttribute("BOM ID - ")>  _
            Public Property Identifier() As String
                Get
                    Return CType(Me("Identifier"),String)
                End Get
                Set
                    Me("Identifier") = value
                End Set
            End Property
            
            <Global.System.Configuration.UserScopedSettingAttribute(),  _
             Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
             Global.System.Configuration.DefaultSettingValueAttribute("Qty - ")>  _
            Public Property Quantity() As String
                Get
                    Return CType(Me("Quantity"),String)
                End Get
                Set
                    Me("Quantity") = value
                End Set
            End Property
            
            <Global.System.Configuration.UserScopedSettingAttribute(),  _
             Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
             Global.System.Configuration.DefaultSettingValueAttribute("Material - ")>  _
            Public Property Material() As String
                Get
                    Return CType(Me("Material"),String)
                End Get
                Set
                    Me("Material") = value
                End Set
            End Property
            
            <Global.System.Configuration.UserScopedSettingAttribute(),  _
             Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
             Global.System.Configuration.DefaultSettingValueAttribute("(Flipped Over)")>  _
            Public Property Flipped() As String
                Get
                    Return CType(Me("Flipped"),String)
                End Get
                Set
                    Me("Flipped") = value
                End Set
            End Property
            
            <Global.System.Configuration.UserScopedSettingAttribute(),  _
             Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
             Global.System.Configuration.DefaultSettingValueAttribute("True")>  _
            Public Property SeparateByMaterial() As Boolean
                Get
                    Return CType(Me("SeparateByMaterial"),Boolean)
                End Get
                Set
                    Me("SeparateByMaterial") = value
                End Set
            End Property
            
            <Global.System.Configuration.UserScopedSettingAttribute(),  _
             Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
             Global.System.Configuration.DefaultSettingValueAttribute("True")>  _
            Public Property FlatPatternUnfold() As Boolean
                Get
                    Return CType(Me("FlatPatternUnfold"),Boolean)
                End Get
                Set
                    Me("FlatPatternUnfold") = value
                End Set
            End Property
            
            <Global.System.Configuration.UserScopedSettingAttribute(),  _
             Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
             Global.System.Configuration.DefaultSettingValueAttribute("False")>  _
            Public Property IncludeSingle() As Boolean
                Get
                    Return CType(Me("IncludeSingle"),Boolean)
                End Get
                Set
                    Me("IncludeSingle") = value
                End Set
            End Property
            
            <Global.System.Configuration.UserScopedSettingAttribute(),  _
             Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
             Global.System.Configuration.DefaultSettingValueAttribute("\\MySERVER\Inventor\2020\Z Templates\CNC-METRIC.idw")>  _
            Public Property TemplateFilepath() As String
                Get
                    Return CType(Me("TemplateFilepath"),String)
                End Get
                Set
                    Me("TemplateFilepath") = value
                End Set
            End Property
            
            <Global.System.Configuration.UserScopedSettingAttribute(),  _
             Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
             Global.System.Configuration.DefaultSettingValueAttribute("CNC-Metric")>  _
            Public Property TemplateFriendlyName() As String
                Get
                    Return CType(Me("TemplateFriendlyName"),String)
                End Get
                Set
                    Me("TemplateFriendlyName") = value
                End Set
            End Property
        End Class
    End Namespace
    
    Namespace My
        
        <Global.Microsoft.VisualBasic.HideModuleNameAttribute(),  _
         Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
         Global.System.Runtime.CompilerServices.CompilerGeneratedAttribute()>  _
        Friend Module MySettingsProperty
            
            <Global.System.ComponentModel.Design.HelpKeywordAttribute("My.Settings")>  _
            Friend ReadOnly Property Settings() As Global.Cxxxxxs.My.MySettings
                Get
                    Return Global.Cxxxxxs.My.MySettings.Default
                End Get
            End Property
        End Module
    End Namespace
    


    Nacho is the derivative of Nigel - True fact! I am self taught in VB.Net. 50% of the time, I am right 100% of the time!

    Thursday, October 24, 2019 7:45 PM
  • Nacho,

    Hmm. I am not sure what that file is but I dont think you need to look at it.

    You would look in the user.config file if you had to. But I dont think you need to look. It either works or not as my.settings...

    It just seems some simple misunderstanding. Have you used my.settings before in other projects?

    I suggeest you make a simple one form example project and make sure you understand how the my.settings work if you need to.


    Karen et al? Do you see something?

    Thursday, October 24, 2019 8:55 PM
  • Hey

    thanks for the reply back. Yes ive used My.Settings before many times in the exact same way which is why its confusing.

    UPDATE

    i deleted all of the settings completely and added in new ones with different names & values. Now, it sort of half works....

    sTemplatePath = \\MyServer\Inventor\2020\Templates\MyTemplate.idw

    sTemplateName = MyTemplate

    Now, i can do the following

    txtTemplateName.Text = My.Settings.sTemplateName (Returned value = MyTemplate)

    txtTemplatePath.Text = My.settings.sTemplatePath (returned value = MyTemplate)

    i get 1 correct value and 1 incorrect value from a different setting

    so, I then changed My.Settings.sTemplateName to a new value (MyNewTemplate)

    but the results were even more strange

    txtTemplateName.Text = My.Settings.sTemplateName (Returned value = MyNewTemplate)

    txtTemplatePath.Text = My.settings.sTemplatePath (returned value = MyTemplate)

    The value returned from sTemplatePath was the old value of sTemplateName even thought that value no longer exists in the settings...


    Nacho is the derivative of Nigel - True fact! I am self taught in VB.Net. 50% of the time, I am right 100% of the time!

    Thursday, October 24, 2019 9:11 PM
  • Hey

    thanks for the reply back. Yes ive used My.Settings before many times in the exact same way which is why its confusing.

    UPDATE

    i deleted all of the settings completely and added in new ones with different names & values. Now, it sort of half works....

    sTemplatePath = \\MyServer\Inventor\2020\Templates\MyTemplate.idw

    sTemplateName = MyTemplate

    Now, i can do the following

    txtTemplateName.Text = My.Settings.sTemplateName (Returned value = MyTemplate)

    txtTemplatePath.Text = My.settings.sTemplatePath (returned value = MyTemplate)

    i get 1 correct value and 1 incorrect value from a different setting

    so, I then changed My.Settings.sTemplateName to a new value (MyNewTemplate)

    but the results were even more strange

    txtTemplateName.Text = My.Settings.sTemplateName (Returned value = MyNewTemplate)

    txtTemplatePath.Text = My.settings.sTemplatePath (returned value = MyTemplate)

    The value returned from sTemplatePath was the old value of sTemplateName even thought that value no longer exists in the settings...


    Nacho is the derivative of Nigel - True fact! I am self taught in VB.Net. 50% of the time, I am right 100% of the time!

    Nacho,

    A couple more ideas to watch for.

    Where and how the do the my.settings get saved in your code after you set one variable in your app code as you show?
     
    You dont somehow go back to the previous settings when saved upon app exit or etc? ie if the settings are reloaded or set to default and then saved again it looses the values you set before saving.

    You say you recreated the settings and they worked... for a while?

    Did you click the syncronize button as I mentioned?

    It still sounds like the old settings are being mixed/used with new app.config settings that dont get updated from new app.config in old user.config.

    I have this do funny things like you describe if the old user settings file user.config is not deleted before updating. Sometimes my customers have old minor versions from years ago and then if you try running a new version on top without deleting the old user.config you get this kind of problems.

    Again how do you install? Is the problem realated to that? Or is it only happening in design mode right now ie running within visual studio?

    Friday, October 25, 2019 7:31 AM