2016-05-17 87 views
0

我的WinForm程序保存了用户设置的xml副本,我遇到的问题是将其重新加载回来。从XML中加载用户设置并将其设置为对象。未将对象设置为对象实例

我已经从博客复制此代码:Code Snippet

错误发生的历史,其中发生在下面的代码注释。

无法导入设置。对象引用未设置为对象的实例 。

[更新]我刚才注意到,它运行在Visual Studio 2013程序时的实际工作,但是并没有从Windows资源管理器运行时。

[Update2]我想是因为这是我第一次从桌面运行这个,我是一个不同的用户,我的用户设置配置文件尚未创建,这是问题。

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); 
} 

下面是XML文件:

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

回答

1

看来你是,如果你从VS或从桌面运行取决于不同的用户。

当我第一次从桌面运行程序时,还没有创建user.config文件。

解决方案:添加了一个检查,看是否已创建user.config,如果不执行用户设置的保存,将创建它。

新代码:

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

我已经改变了这种解决方案一点,看来你至少必须初始化保存工作之前的设置之一。 – Hank

相关问题