请协助 - app.config和Program.cs出错 [英] Please assist - Error in app.config and Program.cs

查看:85
本文介绍了请协助 - app.config和Program.cs出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Guys,



我需要帮助解决下面我面临的2个错误,



1. XML错误:未声明配置元素



我的app.config部分,





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,


<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.项目下面program.cs部分的最后一行错误:



'Timer.Properties。 Settings'不包含'DefaultTimer'的定义,并且没有扩展方法'DefaultTimer'接受类型'Timer.Properties.Settings'的第一个参数(你是否缺少using指令或汇编引用?)




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,

推荐答案



我不是确定您要使用app.config文件完成的任务。提供所需值的app.config只需包含


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 version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="DefaultTimer" value="90" />
  </appSettings>
</configuration>



您必须在程序以及参考文献顶部的使用中包含 System.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.

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:



  • 不要声明辅助变量(例如,assm,assmName,ver等),除非你稍后会在应用程序。

  • 使用StringBuilder AppendFormat方法而不是多次调用StringBuilder Append方法。

  • 我不知道识别。代码中的属性。



希望有所帮助。


Hope that helps.


这篇关于请协助 - app.config和Program.cs出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆