V
vtxr1300
I've followed 2 very similar tutorials on creating a windows service
and an installer. But, after successfully compiling it, I try to do
the InstallUtil command, but I get a badimageformatexception. I tried
to do some searching on that exception, but all I could find was a
small blurb on MSs site about C++ which I'm not using. Can anyone see
a problem in this code? Thanks.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
namespace FaxWatcher
{
class Stalker : IDisposable
{
#region members
private string _path = string.Empty;
private string _filter = string.Empty;
private string _savepath = string.Empty;
private string _logfile = string.Empty;
private DateTime dt = DateTime.Now;
FileSystemWatcher fsw = null;
#endregion
#region cstor
public Stalker(string stalkpath, string savepath, string
filter)
{
_path = stalkpath.Trim();
_filter = filter.Trim();
_savepath = savepath.Trim();
_logfile =
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
fsw = new FileSystemWatcher();
fsw.Path = _path;
fsw.Filter = _filter;
fsw.NotifyFilter = NotifyFilters.CreationTime;
fsw.EnableRaisingEvents = true;
fsw.Changed += new FileSystemEventHandler(fsw_Changed);
}
#endregion
#region directory changed event
void fsw_Changed(object sender, FileSystemEventArgs e)
{
string filepath = e.FullPath;
string filename = e.Name;
string tofolder = dt.ToShortDateString().Replace("/",
string.Empty);
if (!Directory.Exists(_savepath + "/" + tofolder))
Directory.CreateDirectory(_savepath + "/" + tofolder);
if (File.Exists(_savepath + "/" + tofolder + "/" +
filename))
GetNewFileName(ref filename, _savepath + "/" +
tofolder);
try
{
//do we need to do some renaming here?
File.Copy(filepath, _savepath + "/" + tofolder + "/" +
filename);
File.Delete(filepath);
}
#region error occurred, log to text file
catch (Exception exc)
{
try
{
Stream st = null;
if (!File.Exists(_logfile + "/log.txt"))
st = File.Create(_logfile + "/log.txt");
else
st = File.Open(_logfile + "/log.txt",
FileMode.Append);
TextWriter tw = new StreamWriter(st);
tw.WriteLine(filepath + " | " + exc.ToString());
tw.Close(); tw.Dispose();
st.Dispose(); st = null;
}
catch (Exception ex) { }
}
#endregion
}
#endregion
#region GetNewFileName
private void GetNewFileName(ref string filename, string path)
{
int i = 1;
string tmpName = filename;
while (File.Exists(path + "\\" + tmpName))
{
tmpName = String.Concat(filename.Substring(0,
filename.LastIndexOf(".")), i,
filename.Substring(filename.LastIndexOf(".")));
i++;
}
filename = tmpName;
}
#endregion
#region properties
public string StalkPath
{
set { _path = value.Trim(); }
get { return _path; }
}
public string StalkFilter
{
set { _filter = value.Trim(); }
get { return _filter; }
}
#endregion
#region cleanup
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
if (fsw != null)
fsw.Dispose(); fsw = null;
}
~Stalker()
{
Dispose(false);
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceProcess;
namespace FaxWatcher
{
class StalkerService : ServiceBase
{
Stalker s = null;
public StalkerService()
{
this.ServiceName = "Stalker";
this.CanStop = true;
this.CanPauseAndContinue = false;
this.AutoLog = false;
}
public static void Main()
{
System.ServiceProcess.ServiceBase.Run(new
StalkerService());
}
protected override void OnStart(string[] args)
{
if (args.Length > 2)
s = new Stalker(args[0], args[1], args[2]);
}
protected override void OnShutdown()
{
if (s != null)
s.Dispose();
base.OnShutdown();
}
}
}
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;
namespace FaxWatcher
{
[RunInstaller(true)]
public class StalkerServiceInstaller : Installer
{
private ServiceProcessInstaller processInstaller;
private ServiceInstaller serviceInstaller;
public StalkerServiceInstaller()
{
processInstaller = new ServiceProcessInstaller();
serviceInstaller = new ServiceInstaller();
processInstaller.Account = ServiceAccount.LocalSystem;
serviceInstaller.StartType = ServiceStartMode.Manual;
serviceInstaller.ServiceName = "Stalker";
Installers.Add(serviceInstaller);
Installers.Add(processInstaller);
}
}
}
and an installer. But, after successfully compiling it, I try to do
the InstallUtil command, but I get a badimageformatexception. I tried
to do some searching on that exception, but all I could find was a
small blurb on MSs site about C++ which I'm not using. Can anyone see
a problem in this code? Thanks.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
namespace FaxWatcher
{
class Stalker : IDisposable
{
#region members
private string _path = string.Empty;
private string _filter = string.Empty;
private string _savepath = string.Empty;
private string _logfile = string.Empty;
private DateTime dt = DateTime.Now;
FileSystemWatcher fsw = null;
#endregion
#region cstor
public Stalker(string stalkpath, string savepath, string
filter)
{
_path = stalkpath.Trim();
_filter = filter.Trim();
_savepath = savepath.Trim();
_logfile =
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
fsw = new FileSystemWatcher();
fsw.Path = _path;
fsw.Filter = _filter;
fsw.NotifyFilter = NotifyFilters.CreationTime;
fsw.EnableRaisingEvents = true;
fsw.Changed += new FileSystemEventHandler(fsw_Changed);
}
#endregion
#region directory changed event
void fsw_Changed(object sender, FileSystemEventArgs e)
{
string filepath = e.FullPath;
string filename = e.Name;
string tofolder = dt.ToShortDateString().Replace("/",
string.Empty);
if (!Directory.Exists(_savepath + "/" + tofolder))
Directory.CreateDirectory(_savepath + "/" + tofolder);
if (File.Exists(_savepath + "/" + tofolder + "/" +
filename))
GetNewFileName(ref filename, _savepath + "/" +
tofolder);
try
{
//do we need to do some renaming here?
File.Copy(filepath, _savepath + "/" + tofolder + "/" +
filename);
File.Delete(filepath);
}
#region error occurred, log to text file
catch (Exception exc)
{
try
{
Stream st = null;
if (!File.Exists(_logfile + "/log.txt"))
st = File.Create(_logfile + "/log.txt");
else
st = File.Open(_logfile + "/log.txt",
FileMode.Append);
TextWriter tw = new StreamWriter(st);
tw.WriteLine(filepath + " | " + exc.ToString());
tw.Close(); tw.Dispose();
st.Dispose(); st = null;
}
catch (Exception ex) { }
}
#endregion
}
#endregion
#region GetNewFileName
private void GetNewFileName(ref string filename, string path)
{
int i = 1;
string tmpName = filename;
while (File.Exists(path + "\\" + tmpName))
{
tmpName = String.Concat(filename.Substring(0,
filename.LastIndexOf(".")), i,
filename.Substring(filename.LastIndexOf(".")));
i++;
}
filename = tmpName;
}
#endregion
#region properties
public string StalkPath
{
set { _path = value.Trim(); }
get { return _path; }
}
public string StalkFilter
{
set { _filter = value.Trim(); }
get { return _filter; }
}
#endregion
#region cleanup
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
if (fsw != null)
fsw.Dispose(); fsw = null;
}
~Stalker()
{
Dispose(false);
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceProcess;
namespace FaxWatcher
{
class StalkerService : ServiceBase
{
Stalker s = null;
public StalkerService()
{
this.ServiceName = "Stalker";
this.CanStop = true;
this.CanPauseAndContinue = false;
this.AutoLog = false;
}
public static void Main()
{
System.ServiceProcess.ServiceBase.Run(new
StalkerService());
}
protected override void OnStart(string[] args)
{
if (args.Length > 2)
s = new Stalker(args[0], args[1], args[2]);
}
protected override void OnShutdown()
{
if (s != null)
s.Dispose();
base.OnShutdown();
}
}
}
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;
namespace FaxWatcher
{
[RunInstaller(true)]
public class StalkerServiceInstaller : Installer
{
private ServiceProcessInstaller processInstaller;
private ServiceInstaller serviceInstaller;
public StalkerServiceInstaller()
{
processInstaller = new ServiceProcessInstaller();
serviceInstaller = new ServiceInstaller();
processInstaller.Account = ServiceAccount.LocalSystem;
serviceInstaller.StartType = ServiceStartMode.Manual;
serviceInstaller.ServiceName = "Stalker";
Installers.Add(serviceInstaller);
Installers.Add(processInstaller);
}
}
}