Set File Path configuration on Start up

  • Thread starter Thread starter Priya Bange
  • Start date Start date
P

Priya Bange

Guest
Hi Experts,

I have a windows service which currently runs on fixed source & destination path , file format as depicted in the below code. Is it possible to set them to read from SQL Server table only on startup/ restart once and remain persistent for the duration the service in running..

Please support..

Thanks

Priya

CREATE TABLE Config
(Source_Path VARCHAR(100),
Destination_Path VARCHAR(100),
file_type VARCHAR(10)
)

Insert INTO Config
VALUES ('C:\in','C:\out','.txt')

Server : LocalHost
-----------------------

-- Program.Cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Topshelf;

namespace FileConverterService
{
class Program
{
static void Main(string[] args)
{
HostFactory.Run(serviceConfig =>
{
serviceConfig.Service<ConverterService>(serviceInstance =>
{
serviceInstance.ConstructUsing(
() => new ConverterService());

serviceInstance.WhenStarted(
execute => execute.Start());

serviceInstance.WhenStopped(
execute => execute.Stop());
});

serviceConfig.SetServiceName("FileMove");
serviceConfig.SetDisplayName("File Mover");
serviceConfig.SetDescription("File Mover Service");

serviceConfig.StartAutomatically();
});
}
}
}
-----------------------------------------------------------
using System.IO;
using System.Runtime.Caching;
using static System.Console;

namespace FileConverterService
{
class ConverterService
{
private FileSystemWatcher _watcher;
private static readonly string InProgressDirectoryName = "processing";

public bool Start()
{
_watcher = new FileSystemWatcher(@"C:\in", "*.txt*"); // Read from SQL Server

_watcher.Created += FileCreated;

_watcher.IncludeSubdirectories = false;

_watcher.EnableRaisingEvents = true;
_watcher.InternalBufferSize = 32768; // 32 KB


/ return true;
}


private const int NumberOfRetries = 3;
private const int DelayOnRetry = 1000;



private void FileCreated(object sender, FileSystemEventArgs e)
{
string DestinationDirectoryPath = (@"C:\out"); // Read from SQL Server


string inputFileName = Path.GetFileName(e.FullPath);
string inProgressFilePath =
Path.Combine(rootDirectoryPath, InProgressDirectoryName, inputFileName);
for (int i = 1; i <= NumberOfRetries; ++i)
{
try
{
File.Move(e.FullPath, inProgressFilePath);
break;
}
catch (IOException) when (i <= NumberOfRetries)
{
Thread.Sleep(DelayOnRetry);
}
}
}

public bool Stop()
{
_watcher.Dispose();

return true;
}
}
}

Continue reading...
 
Back
Top