配置错误c#App config

时间:2016-09-28 09:46:37

标签: c#

我正在编写一个控制台应用程序,用于使用C#将集成文件从共享驱动器移动到Linux服务器。新应用程序正常工作并且正在完成其工作,最近我决定使用配置文件保存属性,如filepath。

由于我已经开始使用配置文件,我收到错误"配置系统无法初始化"没有进一步的细节。以下是我的配置文件

中的文字
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<Appsettings>

<Add key="UserName" value="my username" />
<Add key="HostName" value="Ip Add" />
<Add key="Password" value="MyPassword" />
<Add key="SshHostKeyFingerprint" value=" code" />
<Add key="sourcepath" value="file path" />
<Add key="destinationpath" value="file path" />
<Add key="MagentoDestinationpath" value="file path" />
<Add key="filestomove" value="*.csv" />
<Add key="Logfilepath" value="file path" />


</Appsettings>
</configuration>

我已经注释掉了以下所有代码以排除故障

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WinSCP;
using System.Threading;
using System.IO;
Using System.Configuration;
using System.Collections.Specialized;
namespace Magentogiftcard
{
 class Program
{
 static void Main(string[] args)

    {

        string Host = ConfigurationManager.AppSettings.Get("UserName");
        Console.WriteLine("Host");  

    }

    }

  }

但仍然遇到这个问题。我会饶有兴趣。

问候 Jahangir Khizer

1 个答案:

答案 0 :(得分:2)

<Appsettings>元素及其子元素区分大小写,因此<Add…<appSettings> <add key="destinationpath" value="file path" /> 导致错误。

将其更改为

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ComputerTimer
{
    public partial class MainWindow : Window
    {
        private DateTime startTime, endTime;
        private static System.Timers.Timer timer1;
        private bool running = true;

        public MainWindow()
        {
            InitializeComponent();

            startTime = DateTime.Now;

            //makes new timer with 200 milliseconds interval
            timer1 = new System.Timers.Timer(200);
            timer1.Elapsed += new ElapsedEventHandler(timer1_Tick);
            timer1.Interval = 200;
            timer1.Enabled = true;

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            while (running)
            {
                endTime = DateTime.Now;
                TimeSpan span = endTime - startTime; //gets difference between now and when the program was started
                Title.Content = span.ToString().Substring(0, 8); //gets first 8 characters (taking out milliseconds)
            }
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            //when button is pressed to stop timer
            running = false;
        }
    }
}

Intellisense说明了这一点:

enter image description here

相关问题