Skip to content

Instantly share code, notes, and snippets.

@chaojian-zhang
Last active January 16, 2020 03:54
Show Gist options
  • Save chaojian-zhang/f990b0aa42853433790981e2071d2401 to your computer and use it in GitHub Desktop.
Save chaojian-zhang/f990b0aa42853433790981e2071d2401 to your computer and use it in GitHub Desktop.
A C# server that reads local files as input and output new total numbers based on input.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace CSharpServer
{
class Program
{
private static bool Close = false;
private static readonly double Total = 1e10;
static void Main(string[] args)
{
Console.WriteLine("Start server.");
// Infinite main loop
FileSystemWatcher watcher = new FileSystemWatcher("C:\\Users\\zhangc2\\Desktop\\MyServer");
watcher.Changed += Watcher_Changed;
watcher.Created += Watcher_Created;
watcher.EnableRaisingEvents = true;
while (!Close)
Thread.Sleep(1000);
}
private static void Watcher_Created(object sender, FileSystemEventArgs e)
{
Console.WriteLine("File created.");
if (e.Name == "Exit.txt")
Close = true;
}
private static void Watcher_Changed(object sender, FileSystemEventArgs e)
{
Console.WriteLine("File changed.");
string inputName = "Input.txt";
if (e.Name == inputName)
{
double input = Double.Parse(File.ReadAllText(inputName));
double result = Total - input;
File.WriteAllText("Output.txt", result.ToString());
}
Console.WriteLine("File created.");
if (e.Name == "Exit.txt")
Close = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment