Potrzebuje zrobić usługę która co minutę będzie wykonywała sprawdzanie która jest godzina: z systemu i internetu -> przez aplikację konsolową nie da rady bo wykonuje się raz i zamyka - nie działa w tle, do tego jeśli usługę nazwę svchost to nikt się nie dokopie jak ten program się uruchamia itd. Czy może ktoś pobrać sobie sharpdevelop lub vs jeśli jest kompatybilny z sharpdevelop i przerobić moją aplikację konsolową na usługę i kod źrodłowy podesłać mi linkiem ? Wiem że instaluje się przez cmd komendą installutil ale najpierw trzeba dobrze napisać usługę.
using System;
using System.IO;
using System.Net;
using System.Net.Cache;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace KontrolaSiedzeniaPrzedKompem
{
class Program
{
static bool po, wt, sr, cz, so, ni, czaswtygodniu, czaswweekend;
public static void Main(string[] args)
{
CzasSystemowy();
Wykonaj();
CzasZInternetu();
Wykonaj();
}
static void CzasSystemowy()
{
string ds = DateTime.Now.DayOfWeek.ToString();
int hs = DateTime.Now.Hour;
if (hs>14&&hs<20)
{
czaswtygodniu = true;
}
else
{
czaswtygodniu = false;
}
if (hs>6&&hs<15)
{
czaswweekend = true;
}
else
{
czaswweekend = false;
}
if (ds=="Monday")
{
po = true;
}
else
{
po = false;
}
if (ds=="Tuesday")
{
wt = true;
}
else
{
wt = false;
}
if (ds=="Wednesday")
{
sr = true;
}
else
{
sr = false;
}
if (ds=="Thursday")
{
cz = true;
}
else
{
cz = false;
}
if (ds=="Saturday")
{
so = true;
}
else
{
so = false;
}
if (ds=="Sunday")
{
ni = true;
}
else
{
ni = false;
}
}
static void Wykonaj()
{
if ((po||wt||sr||cz)&&czaswtygodniu)
{
Console.Write("Czas na naukę! Masz 30 sekund i system się zamknie!");
Process.Start("shutdown", "-s -t 30");
Console.ReadKey(true);
}
if ((so||ni)&&czaswweekend)
{
Console.Write("Czas na naukę! Masz 30 sekund i system się zamknie!");
Process.Start("shutdown", "-s -t 30");
Console.ReadKey(true);
}
}
//źródło: stackoverflow.com/questions/6435099/how-to-get-datetime-from-the-internet/9830462#9830462
static DateTime GetNistTime()
{
DateTime dateTime = DateTime.MinValue;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://nist.time.gov/actualtime.cgi?lzbc=siqm9b");
request.Method = "GET";
request.Accept = "text/html, application/xhtml+xml, */*";
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)";
request.ContentType = "application/x-www-form-urlencoded";
request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore); //No caching
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
StreamReader stream = new StreamReader(response.GetResponseStream());
string html = stream.ReadToEnd();//<timestamp time=\"1395772696469995\" delay=\"1395772696469995\"/>
string time = Regex.Match(html, @"(?<=\btime="")[^""]*").Value;
double milliseconds = Convert.ToInt64(time) / 1000.0;
dateTime = new DateTime(1970, 1, 1).AddMilliseconds(milliseconds).ToLocalTime();
}
return dateTime;
}
//źródło: stackoverflow.com/questions/6435099/how-to-get-datetime-from-the-internet/9830462#9830462
static void CzasZInternetu()
{
string timestring = GetNistTime().ToString().Remove(0,11);
string hourstring = timestring.Remove(2,6);
int h = 0;
h = int.Parse(hourstring);
h = Convert.ToInt32(hourstring);
string d = GetNistTime().ToString().Remove(10,9);
if (h>14&&h<20)
{
ct = true;
}
else
{
ct = false;
}
if (h>6&&h<15)
{
cw = true;
}
else
{
cw = false;
}
if (d=="2015-11-09"||d=="2015-11-16"||d=="2015-11-23"||d=="2015-11-30")
{
po = true;
}
else
{
po = false;
}
if (d=="2015-11-10"||d=="2015-11-17"||d=="2015-11-24"||d=="2015-12-01")
{
wt = true;
}
else
{
wt = false;
}
if (d=="2015-11-04"||d=="2015-11-11"||d=="2015-11-18"||d=="2015-11-25"||d=="2015-12-02")
{
sr = true;
}
else
{
sr = false;
}
if (d=="2015-11-05"||d=="2015-11-12"||d=="2015-11-19"||d=="2015-11-26"||d=="2015-12-03")
{
cz = true;
}
else
{
cz = false;
}
if (d=="2015-11-07"||d=="2015-11-14"||d=="2015-11-21"||d=="2015-11-28"||d=="2015-12-05")
{
so = true;
}
else
{
so = false;
}
if (d=="2015-11-08"||d=="2015-11-15"||d=="2015-11-22"||d=="2015-11-29"||d=="2015-12-06")
{
ni = true;
}
else
{
ni = false;
}
}
}
}