windows service example code
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.IO;
namespace file_delete
{
public partial class file_delete : ServiceBase
{
public file_delete()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
}
private void deleteFile(string folder)
{
System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(folder);
System.IO.FileInfo[] fileNames = dirInfo.GetFiles("*.*");
foreach (System.IO.FileInfo fi in fileNames)
{
fi.Delete();
}
How can I call the deleteFile(string folder) from windows forms?
You can use the OnCustomCommand override, but this only takes an integer as an argument and does not support passing strings to the service.
The other options would be to create a WCF service or use Remoting to pass the information you need to the service and call the delete method.
EDIT: to respond to a question in the comments about how to use OnCustomCommand in a very strange way is as follows.
In the service you would need something like this.
In the windows form application you would have something like this
THIS IS NOT TESTED and only a proof of concept. Again, I DO NOT recommend doing it this way and the above example is only to show how NOT to do this type of communications between desktop applications and services.