当我使用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>
答案 0 :(得分:1)
在app.config中,两个设置都将“Deirectory1”定义为键。用“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>