none
Why can't MyApp save to MyApp.exe.config? RRS feed

  • Question

  • I work in VB a few times per year writing apps to automate various business processes for my company.

    When I have the need, I have always included .ini files and managed them using readily available code snippets found on the web. For my current project (we'll call it "MyApp"), I decided to use the builtin App.config functions.  All is well except that it seems my app can only save settings to MyApp.exe.config if I start MyApp in "Run as Administrator" mode.

    To be clear, my logged in account has full administrative privileges.  Even more confusing, I tried changing the Security on the MyApp.exe.config file to exlicitly give full read/write/mofify permissions to "Everyone" and, still, MyApp seems unable to save changes to MyApp.exe.config unless I start it with "Run as Administrator".

    What am I missing here?  MyApp.exe.config resides in the c:\Program Files(x86)\MyApp\ folder, along with the exe file (I actually built an InstallShield installer for the app, if that matters).

    Tuesday, May 8, 2018 3:11 PM

All replies

  • When you go to My Project -> Settings tab, what is the scope of the defined setting variables?  Only "user" scoped variables are read/write (though you should get a design time error if you try to set an application scoped setting variable).

    Also, how you are attempting to set the value?  What code are you using?  My.Settings.<variable>?


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, May 8, 2018 4:35 PM
    Moderator
  • I started out with this (What?  I can't insert a link?) as a guideline and am saving parameters like this:

       

        Function AddUpdateAppSettings(key As String, value As String)
            Try
                Dim configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
                Dim settings = configFile.AppSettings.Settings
                If IsNothing(settings(key)) Then
                    settings.Add(key, value)
                Else
                    settings(key).Value = value
                End If
                configFile.Save(ConfigurationSaveMode.Modified)
                ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name)
                AddUpdateAppSettings = True
            Catch e As ConfigurationErrorsException
                AddUpdateAppSettings = False
            End Try
        End Function
    MsgBox(AddUpdateAppSettings("defaultPath", TextBox1.Text))

    The MsgBox reports "False" if I run MyApp normally and "True" if I run it as administrator.

    Tuesday, May 8, 2018 5:06 PM
  • Is there a specific reason that you use Configuration manager instead of My.Settings?

    I rarely work with settings in this way so would have to really dig into it to try to reproduce the problem and find a solution.  If using My.Settings will work for you, it could get you up and running a lot more quickly.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, May 8, 2018 5:14 PM
    Moderator
  • Also, links and pictures are restricted until you get a few points to your name.  Its just an anti-spam measure.  Annoying, I know, but so are spam posts. :P

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, May 8, 2018 5:15 PM
    Moderator
  • No particular reason.  Like I said, I have always used .ini files in the past but thought I would try something new.  All of my searches turned up Configuration Manager, so that's what I went with.

    I'll try searching out My.Settings and see where that leads.  Thanks for the tip.

    For links and such, I've actually been a member for many, many years, but haven't posted in a very long time.

    Tuesday, May 8, 2018 5:59 PM
  • What am I missing here?  MyApp.exe.config resides in the c:\Program Files(x86)\MyApp\ folder, along with the exe file (I actually built an InstallShield installer for the app, if that matters).

    Yeah it makes a difference, because Program Files and the x86 are protected areas on the O/S starting with Vista and the O/S (s),  and they are not going to let a program write to a file in protected areas.

    If suggest that you make a folder for the program and its config file that not part of the  protected area and place the files there with needed permissions on the folder. 

    Wednesday, May 9, 2018 9:03 AM
  • No particular reason.  Like I said, I have always used .ini files in the past but thought I would try something new.  All of my searches turned up Configuration Manager, so that's what I went with.

    I'll try searching out My.Settings and see where that leads.  Thanks for the tip.

    For links and such, I've actually been a member for many, many years, but haven't posted in a very long time.


    The app.exe.config is located in the application program folder on the deployment system and the program folder is write protected now in windows as the others have mentioned. 

    The config file is simply the application default startup values you have secified for your application settings. When the users changes a setting then the user setting is saved in the user area of windows where the user application has permission to write the settings. The easy way to do this is with My.Settings as others have mentioned.

    Ini files are out of date now and you should change there are better ways ie my.settings.

    My.Setttings saves the user settings in the users local area where the user has permission on the system (in windows 7) this is the folder:

          C:\Users\username\AppData\Local\application name

    where username and application name are specific for the system and app. That way someone else does not change your settings on the system when they run the app. Every user on the system has their own settings.

    So you should switch to My.Setttings and use the methods built into Visual Studio to save yourself a lot of headaches.

    BTW there is a Setup and Deploy forum for these type of questions.

    • Proposed as answer by Cor Ligthert Wednesday, May 9, 2018 11:54 AM
    Wednesday, May 9, 2018 11:50 AM