0

I am changing the app.config file section in Visual Studio 2013. The program language is C#, .net framework is 4.0, the program type is Windows Service.

This is my code:

        #region ChangeConfig
        /// <summary>
        /// 
        /// </summary>
        /// <param name="createdTime"></param>
        [TestCase("2015-06-10 14:37:59")]
        public void ChangeConfiguration(string createdTime)
        {                
            string assemblyConfigFile = Assembly.GetEntryAssembly().Location;
            string appDomainConfigFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);  
            AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
            appSettings.Settings.Remove("queryTime");
            appSettings.Settings.Add("queryTime", createdTime);                
            config.Save();
            ConfigurationManager.RefreshSection("configuration");
        }
        #endregion

The problem is: when the program run to

string assemblyConfigFile = Assembly.GetEntryAssembly().Location

there is an error:

object reference not set to instance of object

How could this be?

PS:I am debugging under NUnit,the version is 2.6.4.

And this is my App.config content:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="interval" value="5"/>
    <add key="queryTime" value="2015-06-10 14:37:59"/>
    <add key="_TimerInterval" value="5000"/>
  </appSettings>
</configuration>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Dolphin
  • 29,069
  • 61
  • 260
  • 539
  • Have you created an instance of `Assembly`? – Ben Jun 15 '15 at 01:27
  • 1
    This is a static function,no need to do like this – Dolphin Jun 15 '15 at 01:29
  • 1
    It seems you are having the same problem as described in this question: http://stackoverflow.com/questions/4337201/net-nunit-test-assembly-getentryassembly-is-null – vesan Jun 15 '15 at 01:32
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Jun 15 '15 at 03:45

0 Answers0