Wednesday, 28 August 2013

c# first windows service, startup issue (maybe issue's)

c# first windows service, startup issue (maybe issue's)

This is my first service it was origionally a form application but i
decided to make it a service for practice. it is a filesystemwatcher that
watches specific directory for changes and copy's the files that change or
are created to a second directory.
The thing is i know how to install the service and it works but it does
nothing(btw it worked in the form). And i dont know why it doesnt do
anything if you can help it is much appreciated
protected override void OnStart(string[] args)
{
base.OnStart(args);
this.fileSystemWatcher1 = new System.IO.FileSystemWatcher();
((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).BeginInit();
fileSystemWatcher1.Path = source;
this.fileSystemWatcher1.EnableRaisingEvents = true;
((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).EndInit();
LogMessageToFile("ChaloSync Test Started");
}
protected override void OnStop()
{
base.OnStop();
fileSystemWatcher1.Dispose();
//TODO: clean up any variables and stop any threads
}
public static void Copy(string sourceDirectory, string targetDirectory)
{
DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);
CopyAll(diSource, diTarget);
}
public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
// Check if the target directory exists, if not, create it.
if (Directory.Exists(target.FullName) == false)
{
Directory.CreateDirectory(target.FullName);
}
// Copy each file into it's new directory.
foreach (FileInfo fi in source.GetFiles())
{
fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
}
// Copy each subdirectory using recursion.
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
{
DirectoryInfo nextTargetSubDir =
target.CreateSubdirectory(diSourceSubDir.Name);
CopyAll(diSourceSubDir, nextTargetSubDir);
}
}
public static void copyfolder()
{
String source = ConfigurationManager.AppSettings[@"Directory1"];
String target = ConfigurationManager.AppSettings[@"Directory2"];
Copy(@source, @target);
}
try
{
NpgsqlConnection objConn = new NpgsqlConnection(conn);
objConn.Open();
NpgsqlCommand cmd1 = new NpgsqlCommand(strSelectCmd, objConn);
cmd1.ExecuteNonQuery();
objConn.Close();
}
catch (Exception )
{
//postgres error
}
/*create table files (name varchar(50),size
int,last_edit timestamp,extension varchar(5));*/
}
static void config()
{
foreach (string key in ConfigurationManager.AppSettings)
{
string value = ConfigurationManager.AppSettings[key];
}
}
private void fileSystemWatcher1_Changed(object sender,
System.IO.FileSystemEventArgs e)
{
LogMessageToFile("File changed> " + e.FullPath + " -Date:" +
DateTime.Now);
filepath = Path.Combine(source, e.Name);
name = Path.GetFileNameWithoutExtension(filepath);
extension = Path.GetExtension(e.FullPath);
size = e.Name.Length;
query = "update files set size=" + size + ", last_edit=now(),
extension='" + extension + "' where name='" + name + "'";
strSelectCmd = "update files set size=" + size + ", last_edit=now(),
extension='" + extension + "' where name='" + name + "'";
querry();
}
private void fileSystemWatcher1_Created(object sender,
System.IO.FileSystemEventArgs e)
{
LogMessageToFile("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;
query = "INSERT INTO files (name,size,last_edit,extension) VALUES('" +
name + "','" + size + "',now(),'" + extension + "')";
strSelectCmd = "INSERT INTO files (name,size,last_edit,extension)
VALUES('" + name + "','" + size + "',now(),'" + extension + "')";
querry();
}
private void fileSystemWatcher1_Deleted(object sender,
System.IO.FileSystemEventArgs e)
{
filepath = Path.Combine(source, e.Name);
LogMessageToFile("File deleted> " + e.FullPath + " -Date:" +
DateTime.Now);
name = Path.GetFileNameWithoutExtension(filepath);
File.Delete(target + e.Name);
query = "delete from files where name='" + name + "'";
strSelectCmd = "delete from files where name='" + name + "'";
querry();
}
private void fileSystemWatcher1_Renamed(object sender,
System.IO.RenamedEventArgs e)
{
filepath = Path.Combine(source, e.Name);
String oldfilepath = Path.Combine(source, e.OldName);
LogMessageToFile("File renamed> " + e.FullPath + " -Date:" +
DateTime.Now);
Console.WriteLine(e.OldName + "=>" + e.Name);
extension = Path.GetExtension(e.FullPath);
size = e.Name.Length;
name = Path.GetFileNameWithoutExtension(filepath);
String oldfilename = Path.GetFileNameWithoutExtension(oldfilepath);
query = "update files set name='" + name + "' where name='" +
oldfilename + "'";
strSelectCmd = "update files set name='" + name + "' where name='" +
oldfilename + "'";
querry();
}
} }

No comments:

Post a Comment