我用 C# 制作了一个 Windows 服务,现在把它变成了一个 Windows 窗体。(别担心,我进行了必要的更改以使其正常工作)
所以现在突然有些功能不起作用。该表格没有给我一个错误,但它只是没有做它应该做的事情。这是一些相关代码:
private void Start_Click(object sender, EventArgs e)
{
this.fileSystemWatcher1 = new System.IO.FileSystemWatcher();
((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).BeginInit();
fileSystemWatcher1.Path = source;
fileSystemWatcher1.Created += new FileSystemEventHandler(fileSystemWatcher1_Created);
fileSystemWatcher1.Changed += new FileSystemEventHandler(fileSystemWatcher1_Changed);
fileSystemWatcher1.Deleted += new FileSystemEventHandler(fileSystemWatcher1_Deleted);
fileSystemWatcher1.Renamed += new RenamedEventHandler(fileSystemWatcher1_Renamed);
this.fileSystemWatcher1.EnableRaisingEvents = true;
this.fileSystemWatcher1.IncludeSubdirectories = true;
((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).EndInit();
logger("Service started " + DateTime.Now);
}
public static void logger(String entry)
{
String logfile = ConfigurationManager.AppSettings[@"log"];
if (File.Exists(@logfile))
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@logfile, true))
{
file.WriteLine(entry);
}
}
else
{
string[] lines = { entry };
System.IO.File.WriteAllLines(@logfile, lines);
}
}
//some more functions as the logger.
private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
{
cut_copy = ConfigurationManager.AppSettings[@"cutter"];
logger("File created> " + e.FullPath + " -Date:" + DateTime.Now);
filepath = Path.Combine(source, e.Name);
name = Path.GetFileNameWithoutExtension(filepath);
extension = Path.GetExtension(e.FullPath);
size = e.Name.Length;
strSelectCmd = "INSERT INTO" + tablepostgresql + " (" + column1 + "," + column2 + "," + column3 + "," + column4 + ") VALUES('" + name + "','" + size + "',now(),'" + extension + "')";
readquery = "select * from " + tablemysql + " where name='" + name + "'";
Mysql();
postgresql();
Record();
if (string.IsNullOrEmpty(filename) == false)
{
if (Directory.Exists(e.FullPath))
{
copyfolder();
Directory.CreateDirectory(target);
}
else
{
if (WaitForFileAvailable(e.FullPath, TimeSpan.FromSeconds(30)))
{
var file = Path.Combine(source, e.Name);
var copy_file = Path.Combine(target, e.Name);
var destination = Path.Combine(target, Path.ChangeExtension(source, Path.GetExtension(source)));
if (File.Exists(file))// Check to see if the file exists.
{ //If it does delete the file in the target and copy the one from the source to the target.
File.Delete(copy_file);
}
File.Copy(e.FullPath, copy_file);
}
else // The file failed to become available within 10 seconds.
{
logger("Copy has failed reason: File is being used by another program");
}
}
}
else
{
query = "INSERT INTO " + tablemysql + " (name,size,last_edit,extension) VALUES('" + name + "','" + size + "',now(),'" + extension + "')";
Mysql();
}
variable_reset();
}
我的问题是,按钮中的所有功能都可以正常工作,比如logger("Service started " + DateTime.Now);
,但是fileSystemWatcher1_Created
不起作用(它什么都不做)。这可能只是一个非常愚蠢的问题,但我很困惑,自从我从事这个工作以来已经很长时间了。