1

当我使用 app.config 作为路径时,它不会观看任何内容但是如果我手动填写路径,它确实可以工作。我不明白它如何与手动路径一起使用,但不适用于 appconfig 路径

请求帮助。!

using System;
using System.IO;
using System.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace ChaloSync
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private bool pause = false;
    String source = ConfigurationManager.AppSettings[@"Directory1"];
    String target = ConfigurationManager.AppSettings[@"Directory2"];


    static void config()
    {
        foreach (string key in ConfigurationManager.AppSettings)
        {
            string value = ConfigurationManager.AppSettings[key];
            MessageBox.Show(value);
        }
    }

    static void database_query()
    {
        var connection = new MySqlConnection(
          "server=localhost;user id=robin;password=***;database=destination;");
        connection.Open();

        string query = "INSERT INTO files (name,size,last_edit,extension) VALUES('query3',20000,'2013-6-19','.voorbeeld')";
        MySqlCommand cmd = new MySqlCommand(query, connection);
        cmd.ExecuteNonQuery();
    }

    private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e)
    {
        if (!pause)
        {
            listBox1.Items.Add("File changed> " + e.FullPath + " -Date:" + DateTime.Now);
        }

    }

    private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
    {

            listBox1.Items.Add("File created> " + e.FullPath + " -Date:" + DateTime.Now);


    }

    private void fileSystemWatcher1_Deleted(object sender, System.IO.FileSystemEventArgs e)
    {
        if (!pause)
        {
            listBox1.Items.Add("File deleted> " + e.FullPath + " -Date:" + DateTime.Now);
        }
    }

    private void fileSystemWatcher1_Renamed(object sender, System.IO.RenamedEventArgs e)
    {
        if (!pause)
        {
            listBox1.Items.Add("File renamed> " + e.FullPath + " -Date:" + DateTime.Now);
        }
    }

    private void Start_Click(object sender, EventArgs e)
    {
        fileSystemWatcher1.Path = source;
        if (!pause)
        {
            pause = true;
            Start.Text = "Pause";
            fileSystemWatcher1.EnableRaisingEvents = true;   
        }
        else
        {
            pause = false;
            Start.Text = "Start";

        }

    }

}
}

app.config 代码

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="Directory1" value="C:\Users\*****\Desktop\dir 1"/>
    <add key="Directory1" value="C:\Users\*****\Desktop\dir 2"/>
  </appSettings>
</configuration>
4

1 回答 1

1

在您的 app.config 中,这两个设置都将“Deictory1”定义为键。用“Directory2”替换第二个,它将起作用:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
     <add key="Directory1" value="C:\Users\d.brock\Desktop\dir 1"/>
     <add key="Directory2" value="C:\Users\d.brock\Desktop\dir 2"/>
  </appSettings>
</configuration>
于 2013-06-19T13:34:22.860 回答