-
Apr 8th, 2014, 10:01 AM
#1
Thread Starter
Member
App.Config and adding new values
I've got to the stage where I can manipulate most of the User Settings in my Appl.Config.
Right now I'm having a problem with adding New settings.
I tried
Code:
My.Settings("Setting ") = "New Value"
My.Settings.SettingName = "New Value"
Of course those generated an error (ReadOnly)
I've also tried
Code:
ConfigurationManager.AppSettings.Set(sSetting, sNewValue)
That doesn't error, but it doesn't appear to do anything either...
I found a VERY long bit of NameSpace code that changed the back and foreground of the Console but that is a little bit too complicated and not too easily understood what it was doing...
-
Apr 8th, 2014, 03:28 PM
#2
Re: App.Config and adding new values
What's their scope? User or Application? If it's scoped as Application, it's read-only and can only be changed at design time. Sounds like what you really want is user-scoped settings, which will allow you to have settings per user, and those are read/write...
-tg
-
Apr 8th, 2014, 07:35 PM
#3
Re: App.Config and adding new values
To expand on what tg said, settings with Application scope are stored in the primary config file, in the same folder as the EXE, while settings with User scope have their default value stored in that same primary config file but their current value is stored in a user config file, under each user's personal folder. Via My.Settings, User-scoped settings are read/write because every user has write privileges on their own personal folder but Application-scoped settings are read-only because many users will not be able to write to a file in the program folder. It is possible to edit the primary config file but not through My.Settings and the user must have appropriate privileges.
As tg suggests, you should generally use User-scoped settings if they need to be modified. If you must use Application-scoped settings for some reason, follow the CodeBank link in my signature and check out my thread on Protected Configuration to see how the config file can be edited in code.
-
Apr 9th, 2014, 04:16 AM
#4
Thread Starter
Member
Re: App.Config and adding new values
They are all User-scoped settings as they are pretty much all modifiable at runtime.
I'm beginning to think that I'm going to have to XML edit the User App.config file, and let the User know that they will see the changes next time they run the Application...
jmcilhinney: I'm just going to have a look at your CodeBank, see if I can find what I need in there...
-
Apr 9th, 2014, 04:29 AM
#5
Re: App.Config and adding new values
All User-scoped settings are read/write via My.Settings. If yours aren't then your project is broken.
-
Apr 9th, 2014, 07:49 AM
#6
Thread Starter
Member
Re: App.Config and adding new values
It's not really the existing settings I'm having a problem with, it's the Adding of new ones...
I'm currently testing adapted code from your example
Code:
Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
config.AppSettings.Settings.Add(key:=sFolder, value:=sNewFolder)
' save the amended config
config.Save(ConfigurationSaveMode.Full)
-
Apr 9th, 2014, 08:05 AM
#7
Thread Starter
Member
Re: App.Config and adding new values
OK, I've tested it, but although the code doesn't error, it also doesn't appear to do anything at all.
The new Setting is not in any of the .config files change, the Project Settings or in My.Settings.NewSetting, if I try to access the new setting I get the error:
Code:
'NewSetting' is not a member of 'WindowsApplication1.My.MySetting'.
' and
The settings property 'NewSetting' was not found.
What am I doing wrong? As I understand it the Settings.Add instruction should add the new Key/Value, but where is it adding it?
Last edited by JA12; Apr 9th, 2014 at 08:09 AM.
-
Apr 9th, 2014, 08:14 AM
#8
Re: App.Config and adding new values
It's likely adding it to the user config file... as it should... but it's not going to work through My.Settings since you're adding it outside of the designer. But if you;re accessing it through code at design time, then you know you need it, so why are you not adding it through the designer at the same time?
-tg
-
Apr 9th, 2014, 08:26 AM
#9
Thread Starter
Member
Re: App.Config and adding new values
Because these are dynamic settings, the user can edit and add to them as required.
Where can I find the user config file, because none of the config files in the project structure are being changed.
-
Apr 9th, 2014, 08:35 AM
#10
Re: App.Config and adding new values
It'll be in the {app name}.user.config file burried in their profile under appdata somewhere... I'm not sure why you'd expect the project files to change...
Here's how the whole thing works:
App starts up.
The Machine.Config file is loaded.
The {app name].config file is then loaded and overlaid on the Machine.Config settings... this allows any app settings which overlap the machine settings to override the machine config.
Then the {app name].user.config file is also loaded and it too is then overlaid over the other configurations. This is what allows the user-scoped settings to override the default values stored in the app config file.
But the short answer is that the user config file is stored in the user's profile.
-tg
-
Apr 9th, 2014, 09:02 AM
#11
Thread Starter
Member
Re: App.Config and adding new values
There is no such file as {app name}.user.config anywhere in the C:\ drive. There are several .config files, but they are all under the
Code:
C:\Users\usr\Documents\Visual Studio 2012\Projects\ClientMover\WindowsApplication1\*
folder and none of them are named *.user.config
This could explain why I'm having problems understanding how this works...
Thanks for the explanation.
Here are the config files I do have
Code:
C:\Users\usr\Documents\Visual Studio 2012\Projects\ClientMover\WindowsApplication1\App.config
C:\Users\usr\Documents\Visual Studio 2012\Projects\ClientMover\WindowsApplication1\bin\Debug\ClientMover.exe.config
C:\Users\usr\Documents\Visual Studio 2012\Projects\ClientMover\WindowsApplication1\bin\Debug\ClientMover.vshost.exe.config
Last edited by JA12; Apr 9th, 2014 at 09:05 AM.
-
Apr 9th, 2014, 10:32 AM
#12
Thread Starter
Member
Re: App.Config and adding new values
OK, the config file it seems to be using is the
Code:
{app name}.exe.config
in the debug folder
Code:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="{app name}.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<userSettings>
<{app name}.My.MySettings>
<setting name="TestMode" serializeAs="String">
<value>True</value>
</setting>
</{app name}.My.MySettings>
</userSettings>
</configuration>
The section in Red seem to be the only UserSettings that are used by the Application.
I still can't add to this section.
If I change the Settings in the Project Properties then the config file that is modfied is
the one in the root folder
Last edited by JA12; Apr 9th, 2014 at 10:36 AM.
-
Apr 9th, 2014, 12:17 PM
#13
Re: App.Config and adding new values
Sigh... okay... it's clear that there's a gap in your understanding of how this all works... so I'm going to see if I can lay this out in a clearer way so that you can see why it's doing what it's doing.
Yes... in the project file there is an app.config file... that is the DESIGNER file for your config settings. That's what is used by the IDE... just like you have a form.vb file that's the code for your form and a form.designer.vb that's the designer file for hte form... so too is the app.config file used by your project. When you COMPILE the project, the config file is packaged up and re-named to app.exe.config... that's the APPLICATION settings AND the DEFAULT user settings. If you NEVER change any of the user config settings, then it will always just read from the {app name}.config file... because the user.config file will never get created.
My.Settings.settingsname works because at compile time, the IDE includes a shared class that exposes all of the config items that you have defined in the settings. Because it's done at compile, if you later add a setting through code (and not the designer) then it's not going to be exposed, and the my.settings.propIadded isn't going to work. That's not to mean you can't add things at run time... you just simply cannot add them later, and expect My.Settings to work for it... which is what you seem to be doing here. If you add a setting via code, you're going to have to read it back through the same way... because it is not going to be exposed through My.Settings.
-tg
-
Apr 10th, 2014, 03:08 AM
#14
Thread Starter
Member
Re: App.Config and adding new values
OK, thanks for that. I'm a VB6 programmer, converting to VB.NET and I'm used to INI files but I'm trying to do this properly.
If I don't get this working soon, I'm going back to using an INI file, I know how that works!
-
Apr 10th, 2014, 06:42 AM
#15
Thread Starter
Member
Re: App.Config and adding new values
I've given up trying to add "New" settings to the config file, I'm now going down the route of having a single Setting "Folders" and this is a StringCollection.
This works pretty well as I just load all the XML values from the collection directly into the ListBox
Code:
ListBox.DataSource = My.Settings.Folders.Cast(Of String)().ToArray()
Then just use the My.Settings methods to manage those values
Code:
My.Settings.Folders.Add(sNewFolder)
My.Settings.Folders.Remove(sOldValue)
My.Settings.Folders.Item(i) = sOldValue
This seems to work fine.
I then started down the StringCollectionEditor route and am now blocked on the
Code:
Constructor on type system.string not found
I've done hours of searching and all the examples are C# and don't translate well to VB.NET.
So far I've got:
Code:
Public Class StringCollection
Inherits System.Collections.ObjectModel.Collection(Of String)
End Class
I've also got:
Code:
<Description("The list of Folders for Upload")> _
<Editor("System.Windows.Forms.Design.StringCollectionEditor, System.Design", "System.Drawing.Design.UITypeEditor, System.Drawing")> _
But where do I put it?
If I try to add this to the generated code in Settings.Designer.vb for the Folders Property
Code:
<Global.System.Configuration.UserScopedSettingAttribute(), _
Global.System.Diagnostics.DebuggerNonUserCodeAttribute(), _
Global.System.Configuration.DefaultSettingValueAttribute("<?xml version=""1.0"" encoding=""utf-16""?>" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "<ArrayOfString xmlns:xsi=""http://www.w3." & _
"org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & " <s" & _
"tring>C:\IFAC\autorec\</string>" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & " <string>C:\IFAC\cms10\files\</string>" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & " <str" & _
"ing>C:\IFAC\finish\</string>" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & " <string>C:\IFAC\ias\</string>" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & " <string>C:\IFAC" & _
"\ias\irs\</string>" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & " <string>C:\IFAC\irs\</string>" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "</ArrayOfString>")> _
Public Property Folders() As Global.System.Collections.Specialized.StringCollection
Get
Return CType(Me("Folders"), Global.System.Collections.Specialized.StringCollection)
End Get
Set(value As Global.System.Collections.Specialized.StringCollection)
Me("Folders") = value
End Set
End Property
it hates it.
I'm presuming that I have to create a property with the same name somewhere else, but when I try to do that as a Partial Property the compiler doesn't like it.
I tried doing this:
Code:
Namespace My
Partial Friend NotInheritable Class MySettings
Private pFolders As StringCollection
<Description("The list of Folders for Upload")> _
<Editor("System.Windows.Forms.Design.StringCollectionEditor, System.Design", "System.Drawing.Design.UITypeEditor, System.Drawing")> _
Public Overloads Property Folders() As StringCollection
Get
Return pFolders
End Get
Set(value As StringCollection)
pFolders = value
End Set
End Property
End Class
Public Class StringCollection
Inherits System.Collections.ObjectModel.Collection(Of String)
End Class
End Namespace
but it didn't like that either (differs only by return type)...
Plus how do I get the value back from the Editor to the Property?
Last edited by JA12; Apr 10th, 2014 at 07:19 AM.
-
Apr 10th, 2014, 07:47 AM
#16
Re: App.Config and adding new values
Ok... hold on... wait one moment... what's with the StringCollectionEditor? What is is EXACTLY you're trying to do here? What are you storing? I keep getting this feeling that you're making this way harder than it needs to be. There's nothing magical about it. It's a collection of stirngs... you .Add to it, you .Remove from it, you read the values... you can do a for each on it...
the StringCollectionEditor is for use at design time... not at run time. But it shouldn't matter since you already have the editor at design time through the settings editor.
as for the mark up and where to put it... it goes on your collection class...
-tg
-
Apr 11th, 2014, 03:17 AM
#17
Thread Starter
Member
Re: App.Config and adding new values
The collection editor gives the error "" when you click on the Add button. So you obviously have to tell it which editor to use.
And no, this definitely has to be done at Run time, and no, I'm not trying to make this complicated, I'm just trying to use the .NET stuff that's available. I've already written my own little "editor" that works, I'm just adding more knowledge to my understanding of VB.NET.
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
|