Is it possible to change My.Settings.SomeString
value of one targeted application from another application programmatically?
Example
Is it possible to change My.Settings.SomeString
value of one targeted application from another application programmatically?
Example
Yes, this is possible.
The My.Settings configurations are stored in a file that is located at the following location:
C:\ Users \ [your_username] \ AppData \ Local \ [app publisher] \ [name of the application] \ [app version #] \ user.config
I'm certain that you could programmatically load the XML of the user.config file for the target application into your application, adjust it, and then save it.
Upon opening the target application, the user.config file would be loaded like normal and any changes you made to the XML would be reflected.
The settings are saved in xml. By default the files is named Settings.settings and is located in Project Directory\My Project
.
In your example + another setting, the file could look like this
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings"
CurrentProfile="(Default)" GeneratedClassNamespace="My"
GeneratedClassName="MySettings" UseMySettingsClassName="true">
<Profiles />
<Settings>
<Setting Name="SomeString" Type="System.String" Scope="User">
<Value Profile="(Default)">This is some string</Value>
</Setting>
<Setting Name="SomeOtherString" Type="System.String" Scope="User">
<Value Profile="(Default)">This is some other string</Value>
</Setting>
</Settings>
</SettingsFile>
You can generate vb.net classes from the XML if you have VS 2012 or newer. I did that, and here are the generated classes. (If you don't have at least VS 2012, you can still use Microsoft's XML Schema Definition Tool).
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:="http://schemas.microsoft.com/VisualStudio/2004/01/settings"), _
System.Xml.Serialization.XmlRootAttribute([Namespace]:="http://schemas.microsoft.com/VisualStudio/2004/01/settings", IsNullable:=False)> _
Partial Public Class SettingsFile
Private profilesField As Object
Private settingsField() As SettingsFileSetting
Private currentProfileField As String
Private generatedClassNamespaceField As String
Private generatedClassNameField As String
Private useMySettingsClassNameField As Boolean
Public Property Profiles() As Object
Get
Return Me.profilesField
End Get
Set(value As Object)
Me.profilesField = value
End Set
End Property
<System.Xml.Serialization.XmlArrayItemAttribute("Setting", IsNullable:=False)> _
Public Property Settings() As SettingsFileSetting()
Get
Return Me.settingsField
End Get
Set(value As SettingsFileSetting())
Me.settingsField = value
End Set
End Property
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property CurrentProfile() As String
Get
Return Me.currentProfileField
End Get
Set(value As String)
Me.currentProfileField = value
End Set
End Property
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property GeneratedClassNamespace() As String
Get
Return Me.generatedClassNamespaceField
End Get
Set(value As String)
Me.generatedClassNamespaceField = value
End Set
End Property
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property GeneratedClassName() As String
Get
Return Me.generatedClassNameField
End Get
Set(value As String)
Me.generatedClassNameField = value
End Set
End Property
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property UseMySettingsClassName() As Boolean
Get
Return Me.useMySettingsClassNameField
End Get
Set(value As Boolean)
Me.useMySettingsClassNameField = value
End Set
End Property
End Class
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:="http://schemas.microsoft.com/VisualStudio/2004/01/settings")> _
Partial Public Class SettingsFileSetting
Private valueField As SettingsFileSettingValue
Private nameField As String
Private typeField As String
Private scopeField As String
Public Property Value() As SettingsFileSettingValue
Get
Return Me.valueField
End Get
Set(value As SettingsFileSettingValue)
Me.valueField = value
End Set
End Property
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property Name() As String
Get
Return Me.nameField
End Get
Set(value As String)
Me.nameField = value
End Set
End Property
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property Type() As String
Get
Return Me.typeField
End Get
Set(value As String)
Me.typeField = value
End Set
End Property
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property Scope() As String
Get
Return Me.scopeField
End Get
Set(value As String)
Me.scopeField = value
End Set
End Property
End Class
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:="http://schemas.microsoft.com/VisualStudio/2004/01/settings")> _
Partial Public Class SettingsFileSettingValue
Private profileField As String
Private valueField As String
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property Profile() As String
Get
Return Me.profileField
End Get
Set(value As String)
Me.profileField = value
End Set
End Property
<System.Xml.Serialization.XmlTextAttribute()> _
Public Property Value() As String
Get
Return Me.valueField
End Get
Set(value As String)
Me.valueField = value
End Set
End Property
End Class
With these classes, you can serialize and deserialize the xml settings file. Of course, you can read and write to the settings file with xml serialization.
This complete solution demonstrates how to edit a setting
' define the path to the settings file
Dim settingsPath = "C:\Users\username\AppData\Local\Temporary Projects\ConsoleApplication1\My Project\Settings.settings"
Dim serializer As New XmlSerializer(GetType(SettingsFile))
Sub Main()
Dim settingsObject As SettingsFile
' open the settings file for reading
Using sr As New StreamReader(settingsPath) ' deserialize the xml file to the settings object
settingsObject = CType(serializer.Deserialize(sr), SettingsFile)
End Using
Console.WriteLine("Available settings:") ' display the available settings
Console.WriteLine(
String.Join(Environment.NewLine, settingsObject.Settings.Select(
Function(s) String.Format("{0}: '{1}'", s.Name, s.Value.Value))))
Console.WriteLine("Enter a setting name to be modified...")
Dim settingNameToBeModified = Console.ReadLine()
Dim settingToBeModified =
settingsObject.Settings.
Where(Function(s) s.Name.ToUpper() = settingNameToBeModified.ToUpper()).Single()
Console.WriteLine(
"Enter a new value for {0} (old value: '{1}')",
settingToBeModified.Name, settingToBeModified.Value.Value)
Dim newValue = Console.ReadLine()
If EditSetting(settingToBeModified, newValue) Then
Console.WriteLine("Saved new setting!")
Else
Console.WriteLine("Couldn't find setting '{0}'", settingNameToBeModified)
End If
Console.WriteLine("Press enter to exit")
Console.ReadLine()
End Sub
Public Function EditSetting(settingObject As SettingsFileSetting, newValue As String)
Try
Console.WriteLine(
"Enter a new value for {0} (old value: '{1}')",
settingObject.Name, settingObject.Value.Value)
settingObject.Value.Value = newValue
Using sr As New StreamWriter(settingsPath) ' open the settings file again, for writing
serializer.Serialize(sr, settingObject) ' serialize the settings object to the xml file
End Using
Console.WriteLine("Saved new setting!")
Catch
Return False
End Try
End Function