2016-05-17 4 views
0

Mein WinForm-Programm speichert eine XML-Kopie der Benutzereinstellungen, das Problem, das ich habe, lädt es wieder ein.Laden von Benutzereinstellungen aus XML mit settings.Object nicht auf eine Instanz des Objekts festgelegt

Ich habe diesen Code aus einem Blog kopiert: Code Snippet

Der Fehler auftreten geschieht, wo in dem unten stehenden Code kommentiert.

Einstellungen konnten nicht importiert werden. Objektreferenz wurde nicht auf eine Instanz von eines Objekts gesetzt.

[Update] Ich habe gerade festgestellt, dass es tatsächlich funktioniert, wenn das Programm von Visual Studio 2013 ausgeführt wird, jedoch nicht der Fall ist, wenn sie von Windows Explorer ausführen.

[Update2] Ich denke, weil es das erste Mal war, dass ich dies vom Desktop ausgeführt habe, bin ich ein anderer Benutzer und meine Benutzereinstellungen Konfigurationsdatei wurde noch nicht erstellt, das ist das Problem.

try{ 
    var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal); 

    // returns "[MyApplication].Properties.Settings"; 
    string appSettingsXmlName = Properties.Settings.Default.Context["GroupName"].ToString(); 
    // Open settings file as XML 
    var import = XDocument.Load(filename); 
    // Get the whole XML inside the settings node 
    var settings = import.XPathSelectElements("//" + appSettingsXmlName); 

    //***Error occurs in the following code*** 
    config.GetSectionGroup("userSettings") 
     .Sections[appSettingsXmlName] 
     .SectionInformation 
     .SetRawXml(settings.Single().ToString()); 

    config.Save(ConfigurationSaveMode.Modified); 
    ConfigurationManager.RefreshSection("userSettings"); 

    appSettings.Reload(); 
} 
catch (Exception ex) 
{ 
    MessageBox.Show("Could not import settings. " + ex.Message); 
} 

Hier ist die XML-Datei:

<?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="DropLib.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" /> 
     </sectionGroup> 
    </configSections> 
    <userSettings> 
     <DropLib.Properties.Settings> 
      <setting name="ORG_SETTINGS" serializeAs="Xml"> 
       <value> 
        <ArrayOfString xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
         <string>ORG1-||-server1-||-Proj 94-||[email protected]|||-Server-|-Server-|-Folder$-|-http://server1/proj-|-c:\inetpub\wwwroot\proj\</string> 
         <string>ORG2-||-server2-||-Proj 94-||[email protected]|||-Server-|-Server-|-Folder$-|-http://server2/proj-|-c:\inetpub\wwwroot\proj\</string> 
        </ArrayOfString> 
       </value> 
      </setting> 
     </DropLib.Properties.Settings> 
    </userSettings> 
</configuration> 

Antwort

1

Es scheint, dass Sie eine andere Benutzer sind abhängig davon, ob Sie von VS oder vom Desktop laufen.

Wenn ich das Programm zum ersten Mal vom Desktop aus ausgeführt habe, wurde noch keine Datei user.config erstellt.

Lösung: Eine Prüfung hinzugefügt, um festzustellen, ob die user.config erstellt wurde, wenn nicht eine der Benutzereinstellungen speichern, die es erstellen wird.

Neuer Code:

try 
{ 
    var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal); 
    //Check user.config exists 
    if (!File.Exists(config.FilePath)) 
    { 
     [EDIT] 
     DropLib.Properties.Settings.Default.MY_SETTING = ""; 
     [/EDIT] 
     DropLib.Properties.Settings.Default.Save(); 
     config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal); 
    } 

    // returns "[MyApplication].Properties.Settings"; 
    string appSettingsXmlName = Properties.Settings.Default.Context["GroupName"].ToString(); 

    // Open settings file as XML 
    var import = XDocument.Load(filename); 

    // Get the whole XML inside the settings node 
    var settings = import.XPathSelectElements("//" + appSettingsXmlName); 

    config.GetSectionGroup("userSettings") 
     .Sections[appSettingsXmlName] 
     .SectionInformation 
     .SetRawXml(settings.Single().ToString()); 

    config.Save(ConfigurationSaveMode.Modified); 
    ConfigurationManager.RefreshSection("userSettings"); 

    appSettings.Reload(); 
} 
catch (Exception ex) 
{ 
    MessageBox.Show("Could not import settings. " + ex.Message); 
    appSettings.Reload(); // from last set saved, not defaults 
} 
+0

ich diese Lösung ein wenig verändert haben, wie es scheint Sie haben mindestens eine der Einstellungen zu initialisieren, bevor die Arbeiten speichern. – Hank

Verwandte Themen