štvrtok, 01 december 2011 10:11 Written by 9163 times
Rate this item
(0 votes)

Shutdown v c#

Je možné vypnúť pc pomocou c#?Ano. V tomto článku si ukážeme ako pomocou c# vypnúť, reštartovať alebo odhlásiť windows.

V prvom príklade použijeme

[DllImport("user32.dll")]
public static extern int ExitFunc(int operationFlag, int operationReason);

Vypnúť windows

ExitFunc(1, 0);

Reštartovať windows

ExitFunc(2, 0);

Odhlásiť windows

ExitFunc(0, 0);

Kód v c#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace ShutDown
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll")]
        public static extern int ExitFunc(int operationFlag, int operationReason);

        public Form1()
        {
            InitializeComponent();
        }       
        protected void btnRestart_Click(object sender, EventArgs e)
        {
            ExitFunc(2, 0);
        }
        protected void btnLogOff_Click(object sender, EventArgs e)
        {
            ExitFunc(0, 0);
        }
        protected void btnShutDown_Click(object sender, EventArgs e)
        {
            ExitFunc(1, 0);
        }
    }
}

Použitím príkazového riadku

 System.Diagnostics.Process.Start("shutdown", "/s");  

Parametre :

  • /l logoff - odhlásiť
  • /s shuts down - vypnúť windows
  • /r restarts - reštartovať windows
  • /h hibernates windows
Last modified on sobota, 10 december 2011 22:12
Ing.Jaroslav Vadel

Som zakladateľom www.projectik.eu.

Hrám sa na programátora, ktorý ovláda:

c#,cpp,java,unity3d,php,html,NI testand,NI Vision Builder,Cognex In-Sight,NI LabView

"Naprogramovať program, ktorý funguje vie každy. Ale to, že program funguje ešte neznamena, že je napísany správne "

Website: www.projectik.eu