NEWS: Total Picture Views: 146791 | Total Article Views: 155157 | Top 5 Most Popular Article: 1. Am I Stuck Algorithm with Becker Robot | 2. How to read a JSON and work with it | 3. PickUpThings with Becker Robot | 4. Encrypted Data GUI (Download .Zip) | 5. Create Wall and Things with Becker

Compare time and close computer if it is smaller or bigger C

Description:

In this example we will be using C#

We will compare time and if time is bigger or smaller we will close the computer. We will also add the program to the startup so when we turn on our computer, the program will open automaticly.

Code:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.Win32;
namespace Hubert
{
    class Program
    {

        static void Main(string[] args)
        {
            Console.WriteLine("Do Not Close!");
            int timeout = 0;
            AddApplicationToStartup();
            while (true)
            {
                timeout = (60 - DateTime.Now.Second) * 1000 - DateTime.Now.Millisecond;

                String day = DateTime.Today.DayOfWeek.ToString();

                DateTime dt8AM = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 8, 0, 0);

                if (day == "Friday" || day == "Saturday")
                {
                        DateTime dt22PM = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 22, 0, 0);
                        if (DateTime.Now > dt22PM || DateTime.Now < dt8AM){
                            //Console.WriteLine("Must close computer.");
                            System.Diagnostics.Process.Start("shutdown", "/s /t 30");     
                    }
                }
                else
                { 
                    DateTime dt19PM = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 19, 0, 0);

                    if (DateTime.Now > dt19PM || DateTime.Now < dt8AM)
                    {
                        //Console.WriteLine("Must close computer.");
                        System.Diagnostics.Process.Start("shutdown", "/s /t 30");
                    }
                }
                //Console.ReadLine();

                Thread.Sleep(timeout);
            }

            
        }
        public static void AddApplicationToStartup()
        {
            using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWAREMicrosoftWindowsCurrentVersionRun", true))
            {
                key.SetValue("Shutdown", "C:Shutdown.exe");
            }
        }
        

    }
}
Attachments: None

Tags: C# Startup Console DateTime

Total Views: 792

My name is Jean-Mathieu

I created this website so other people could enjoy finding useful stuff easier. If you have any question do not hesitate to contact me.

jean8mathieuCreated on 01/10/15 and updated on 01/10/15


affiliate_link

Disclosure: We are a website that needs compensation to operate like any other website on the internet.
We may receive consideration for our reviews but we are totally unbiased and do not accept paid reviews or fake reviews claiming to be something they are not.