Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Guys,

I need help for the 2 errors which i face below,

1. XML error: "The Configuration element is not declared"

My app.config section,


XML
<configuration>
  <configsections>
    <sectiongroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System.Version=2.0.0.0, Culture=neutral, PublicToken=b23547293">

      <section name="Timer.Properties.Settings" type="System.Configuration.ApplicationSettingsGroup, System.Version=2.0.0.0, Culture=neutral, PublicToken=b23547293" requirepermission="false" />
    </sectiongroup>
  </configsections>

  <applicationsettings>
    <timer.properties.settings>
      <settings name="DefaultTimer" serializeas="String">
        <value>90</value>
      </settings>
    </timer.properties.settings>
  </applicationsettings>
  <startup>
    <supportedruntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>



2. Error in the last before line of the below program.cs section of my project":

'Timer.Properties.Settings' does not contain a definition for 'DefaultTimer' and no extension method 'DefaultTimer' accepting a first argument of type 'Timer.Properties.Settings' could be found (are you missing a using directive or an assembly reference?)
namespace Timer
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            if (Environment.CommandLine.Contains("?"))
            {
                var sb = new StringBuilder();
                Assembly assem = Assembly.GetEntryAssembly();
                AssemblyName assemName = assem.GetName();
                Version ver = assemName.Version;

                sb.AppendLine("Timer v" + ver.ToString());
                sb.AppendLine("");
                sb.AppendLine("Command Line Options:");
                sb.AppendLine("/Timer:<timeinminutes>");
           
                sb.AppendLine("Current value is: " + .Properties.Settings.Default.DefaultTimer.ToString());
                sb.AppendLine("");
		}

Please assist, i have tried looking at most of the blogs. no luck.

Cheers,
Posted
Updated 29-Jun-14 1:25am
v3
Comments
Mohan Subramani 29-Jun-14 5:41am    
Not able to post my XML code here .. replacing < > with ? ?


?configuration?

?configSections?
?sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System.Version=2.0.0.0, Culture=neutral, PublicToken=b77a5c5619348089"?

?section name="Timer.Properties.Settings" type="System.Configuration.ApplicationSettingsGroup, System.Version=2.0.0.0, Culture=neutral, PublicToken=b77a5c5619348089" requirePermission="false"/?
?/sectionGroup?
?/configSections?

?applicationSettings?
?Timer.Properties.Settings?
?settings name="DefaultTimer" serializeAs="String"?
?value?90?/value?
?/settings?


?/Timer.Properties.Settings?
?/applicationSettings?
?startup?
?supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /?
?/startup?
?/configuration?
George Jonsson 29-Jun-14 6:30am    
Revise your question and add the XML code there instead.
George Jonsson 29-Jun-14 6:31am    
What does your Settings.Designer.cs file look like?
George Jonsson 29-Jun-14 6:32am    
use <pre lang="xml"> xml data </pre>

1 solution


I'm not sure what you are trying to accomplish with your app.config file. An app.config that provides the value you want need only contain


XML
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="DefaultTimer" value="90" />
  </appSettings>
</configuration>


You must include System.Configuration in the usings at the top of the program and and in the References.



The following is a rewrite of your program.


C#
using System;
using System.Configuration;
using System.Reflection;
using System.Text;

namespace Timer
    {
    static class Program
        {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main ( )
            {

            if ( Environment.CommandLine.Contains ( "?" ) )
                {
                StringBuilder sb = new StringBuilder ( );

                sb.AppendFormat (
                    "Timer v {0} " +
                    "Command Line Options: /Timer:<timeinminutes> " +
                    "Current value is: {1}",
                    Assembly.GetEntryAssembly ( ).
                             GetName ( ).Version.ToString ( ),
                    ConfigurationManager.AppSettings [ 
                                             "DefaultTimer" ] );
                // do something with sb.ToString ( )
                }
            }
        }
    }


Some thoughts:



  • Do not declare auxiliary variables (e.g., assm, assmName, ver, etc.) unless you will use them later in the application.
  • Use the StringBuilder AppendFormat method rather than invoking the StringBuilder Append method multiple times.
  • I do not recognize .Properties in your code.


Hope that helps.

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900