This solution was built to supply a third party application developed in a legacy solution called PFX.
That simple Windows application, runs every day, calling a command line that exports the PFX application to Text Files.
======================================================================
--// AutoSureExportPFX
======================================================================
using System;
using System.Diagnostics;
using System.IO;
using System.ServiceProcess;
using System.Timers;
using Timer = System.Timers.Timer;
namespace AutoSureExportPFX
{
public partial class AutoSureExportPFX : ServiceBase
{
public AutoSureExportPFX()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
Timer timer = new Timer(); // name space(using System.Timers;)
WriteToFile("AutoSure ExportPFX Loading Service Started at " + DateTime.Now);
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
timer.Interval = 8640000; //number in Miliseconds (24 hours)
timer.Enabled = true;
}
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
WriteToFile("Service is recall at " + DateTime.Now);
try
{
System.Environment.CurrentDirectory = @"F:\verop";
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + "pfln.exe /sf:\\verop\\run\\pfx.ini sw99 AUTO");
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
// wrap IDisposable into using (in order to release hProcess)
using (Process process = new Process())
{
process.StartInfo = procStartInfo;
process.Start();
// Add this: wait until process does its work
process.WaitForExit();
}
WriteToFile("AutoSure ExportPFX Loading Service Success Exported at " + DateTime.Now);
}
catch (Exception error)
{
Trace.WriteLine(error.Message);
WriteToFile("AutoSure ExportPFX Loading Service Failed at " + DateTime.Now);
}
}
protected override void OnStop()
{
WriteToFile("AutoSure ExportPFX Loading Service Stopped at " + DateTime.Now);
}
public void WriteToFile(string Message)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "\\Logs";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string filepath = AppDomain.CurrentDomain.BaseDirectory + "\\Logs\\ServiceLog_" + DateTime.Now.Date.ToShortDateString().Replace('/', '_') + ".txt";
if (!File.Exists(filepath))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(filepath))
{
sw.WriteLine(Message);
}
}
else
{
using (StreamWriter sw = File.AppendText(filepath))
{
sw.WriteLine(Message);
}
}
}
}
}
======================================================================
--// AutoSureExportPFX.Designer
======================================================================
namespace AutoSureExportPFX
{
partial class AutoSureExportPFX
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = "AutoSure Export PFX - TDW Loading";
}
#endregion
}
}
namespace AutoSureExportPFX
{
partial class AutoSureExportPFX
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = "AutoSure Export PFX - TDW Loading";
}
#endregion
}
}