Skip to content

Commit

Permalink
added service soucecode
Browse files Browse the repository at this point in the history
  • Loading branch information
rich-pel committed Mar 31, 2020
1 parent afd76b8 commit c66bde0
Show file tree
Hide file tree
Showing 34 changed files with 3,525 additions and 0 deletions.
25 changes: 25 additions & 0 deletions service/FileMover.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.572
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileMover", "FileMover\FileMover.csproj", "{50D3C54E-C8DD-46AB-AE35-CFCB1FE07ED5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{50D3C54E-C8DD-46AB-AE35-CFCB1FE07ED5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{50D3C54E-C8DD-46AB-AE35-CFCB1FE07ED5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{50D3C54E-C8DD-46AB-AE35-CFCB1FE07ED5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{50D3C54E-C8DD-46AB-AE35-CFCB1FE07ED5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {23E144B8-FD76-4EA6-AA94-E7804D34438D}
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions service/FileMover/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
69 changes: 69 additions & 0 deletions service/FileMover/Check.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Topshelf;

namespace DirCheck
{
class Check
{
private FileSystemWatcher watcher;
private string sourceFolder;
private string destinationFolder;

// custom constructor
public Check(string sourceFolder, string destinationFolder)
{
this.sourceFolder = sourceFolder;
this.destinationFolder = destinationFolder;

watcher = new FileSystemWatcher(sourceFolder);
watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.LastWrite;
}

public void Start()
{
watcher.Created += OnChanged;
watcher.Changed += OnChanged;
watcher.Renamed += OnChanged;

watcher.EnableRaisingEvents = true;
watcher.IncludeSubdirectories = true;

Console.WriteLine("The service Check Directory started");
}

private void OnChanged(object sender, FileSystemEventArgs e)
{
Console.WriteLine("OnChangeEvent has is called");

string[] files = Directory.GetFiles(sourceFolder, "*.*", SearchOption.AllDirectories);
int count = files.Length;

if (count > 100)
{
foreach (string file in Directory.GetFiles(sourceFolder, "*.*", SearchOption.AllDirectories))
{
FileInfo fi = new FileInfo(file);

if (fi.LastAccessTime < DateTime.Now.AddMinutes(-60))
{
Console.WriteLine(file + " should move to: " + Path.Combine(destinationFolder, Path.GetFileName(file)));
File.Move(file, Path.Combine(destinationFolder, Path.GetFileName(file)));
}
}
}
}

public void Stop()
{
Console.WriteLine("The Service Check Directory stopped");
watcher.EnableRaisingEvents = false;
}
}
}
60 changes: 60 additions & 0 deletions service/FileMover/FileGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using DirCheck;
using Timer = System.Timers.Timer;

namespace FileMover
{

/// <summary>
///
/// </summary>
class FileGenerator
{
public void Run()
{
var timer = new System.Threading.Timer(
e => MyMethod(),
null,
TimeSpan.Zero,
TimeSpan.FromMinutes(0.1));
}

static void MyMethod()
{
int width = 40, height = 20;

//bitmap
Bitmap bmp = new Bitmap(width, height);

//random number
Random rand = new Random();

//create random pixels
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
//generate random ARGB value
int a = rand.Next(256);
int r = rand.Next(256);
int g = rand.Next(256);
int b = rand.Next(256);

//set ARGB value
bmp.SetPixel(x, y, Color.FromArgb(a, r, g, b));
}
}

//save (write) random pixel image
bmp.Save(Program.sourceFolder + "\\RandomImage"+ DateTime.Now.Millisecond + ".bmp");
Console.WriteLine("image saved! at " + DateTime.Now);
}
}
}
62 changes: 62 additions & 0 deletions service/FileMover/FileMover.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{50D3C54E-C8DD-46AB-AE35-CFCB1FE07ED5}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>FileMover</RootNamespace>
<AssemblyName>FileMover</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Configuration.Install" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.ServiceProcess" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="Topshelf, Version=4.2.0.194, Culture=neutral, PublicKeyToken=b800c4cfcdeea87b, processorArchitecture=MSIL">
<HintPath>..\packages\Topshelf.4.2.0\lib\net452\Topshelf.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Check.cs" />
<Compile Include="FileGenerator.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
53 changes: 53 additions & 0 deletions service/FileMover/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FileMover;
using Topshelf;

namespace DirCheck
{
class Program
{
//public static string sourceFolder = @"C:\Users\Richard\Desktop\source";
// fore tests: public static string sourceFolder = @"C:\Users\Hermeler\Desktop\source";
public static string sourceFolder = @"C:\ATS\Images";
// public static string destinationFolder = @"C:\Users\Richard\Desktop\destination";
// public static string destinationFolder = @"C:\Users\Hermeler\Desktop\destination";
public static string destinationFolder = @"E:\ATS\Images";



static void Main(string[] args)
{
// FileGenerator fileGenerator = new FileGenerator();
// fileGenerator.Run();

var exitCode = HostFactory.Run(x =>
{
x.Service<Check>(s =>
{
s.ConstructUsing(check => new Check(sourceFolder, destinationFolder));
s.WhenStarted(check => check.Start());
s.WhenStopped(check => check.Stop());
});

x.StartAutomatically(); // Start the service automatically

// TODO:
// x.AddCommandLineDefinition("path", v => path = v);
// x.AddCommandLineDefinition("fileAmount", v => fileAmount = Int32.Parse(v));

x.RunAsLocalSystem();
x.SetServiceName("DirCheck");
x.SetDisplayName("Directory Check");
x.SetDescription("Check directory and move files \nMAKE SURE 'E:/ATS/Images' IS AVAILABLE");
});


int exitCodeValue = (int)Convert.ChangeType(exitCode, exitCode.GetTypeCode());
Environment.ExitCode = exitCodeValue;
}
}
}
36 changes: 36 additions & 0 deletions service/FileMover/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("FileMover")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("FileMover")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("50d3c54e-c8dd-46ab-ae35-cfcb1fe07ed5")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Binary file added service/FileMover/bin/Debug/FileMover.exe
Binary file not shown.
6 changes: 6 additions & 0 deletions service/FileMover/bin/Debug/FileMover.exe.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
Binary file added service/FileMover/bin/Debug/FileMover.pdb
Binary file not shown.
Binary file added service/FileMover/bin/Debug/Topshelf.dll
Binary file not shown.
Loading

0 comments on commit c66bde0

Please sign in to comment.