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
ない (何もしない) ことです。これは非常にばかげた質問かもしれませんが、私は非常に混乱しており、これに取り組んでからかなり長い時間が経ちました。