2017-04-17 66 views
1

是否可以通過編程從另一個應用程序更改一個目標應用程序的My.Settings.SomeString值?如何使用VB.Net從另一個應用程序更改一個應用程序的UserSettings值?

我想改變的價值: enter image description here

+0

應用程序設置被設計成一個應用程序將加載只有它的設置,所以你不會是能夠做到這一點直通'My.Setings'。它也可能因部署方法而複雜化(XCopy vs OneClick)對於共享的套件類型配置,您可以自己保存數據,以便其他應用程序(而不僅僅是您的)可以訪問它們。 – Plutonix

+0

你能否讓它更清楚一點......只是說出你說的幾個部分... :(#MyBad –

+0

並且請在你的解釋中加上這個簡單的答案......(是的,如果可能的話,否則,如果不是的話) –

回答

0

是的,這是可能的。

配置存儲在該位於以下位置的文件的My.Settings:

C:\ Users \ [用戶your_username] \應用程序數據\本地\ [應用發佈商] \ [應用程序的名稱] \ [app version#] \ user.config

我確定您可以以編程方式將目標應用程序的user.config文件的XML加載到您的應用程序中,對其進行調整並保存。

打開目標應用程序後,user.config文件將像正常一樣加載,並且您對XML所做的任何更改都會反映出來。

0

設置保存爲xml。默認情況下,文件名爲Settings.settings,位於Project Directory\My Project

在你的榜樣+其他設置,該文件可能看起來像這樣

<?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> 

您可以generate vb.net classes from the XML如果你有VS 2012或更高版本。我做到了,這裏是生成的類。 (如果你至少沒有VS 2012,你仍然可以使用微軟的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 

有了這些類,您可以序列化和反序列化XML設置文件。當然,您可以使用xml序列化讀取和寫入設置文件。

這個完整的解決方案演示瞭如何編輯設置

' 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 
相關問題