Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 2 của 2
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    Shutdown windows trong C# như thế nào?

    Các bác ơi cho em hỏi tý
    Em down ví dụ trong phần tiện ích về để thử, code như sau:
    Mã:
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    namespace LockComp
    {
    	/// <summary>
    	/// Description of MainForm.
    	/// </summary>
    	public partial class MainForm
    	{
    		// Importing Windows API library
            [DllImport("user32.dll")]
            public static extern void LockWorkStation();		
            [DllImport("user32.dll")]
            public static extern int ExitWindowsEx(int uFlags, int dwReason);        
            
    		[STAThread]
    		public static void Main(string[] args)
    		{
    			Application.EnableVisualStyles();
    			Application.SetCompatibleTextRenderingDefault(false);
    			Application.Run(new MainForm());
    		}
    		
    		public MainForm()
    		{
    			//
    			// The InitializeComponent() call is required for Windows Forms designer support.
    			//
    			InitializeComponent();
    			
    			//
    			// TODO: Add constructor code after the InitializeComponent() call.
    			//
    		}
    		
    		// Lock workstation
    		void BtnLockCompClick(object sender, System.EventArgs e)
    		{
    			LockWorkStation();
    		}
    		
    		// Log Off
    		void BtnLogOffClick(object sender, System.EventArgs e)
    		{
    			if(DialogResult.Yes==MessageBox.Show("Do you really want to Log Off?","Log Off", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2))
    			ExitWindowsEx(0, 0);
    		}
    		
    		// Reboot
    		void BtnRebootClick(object sender, System.EventArgs e)
    		{
    			if(DialogResult.Yes==MessageBox.Show("Do you really want to Reboot?","Reboot", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2))
    			ExitWindowsEx(2, 0);
    		}
    		
    		// Shutdown
    		void BtnShutdownClick(object sender, System.EventArgs e)
    		{
    			if(DialogResult.Yes==MessageBox.Show("Do you really want to Shutdown?","Shutdown", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2))
                ExitWindowsEx(1, 0);
    		}
    		
    		// Force LogOff
    		void BtnForceLogOffClick(object sender, System.EventArgs e)
    		{
    			if(DialogResult.Yes==MessageBox.Show("Do you really want to force Log Off?","Force LogOff", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2))
    			ExitWindowsEx(4, 0);
    		}
    		
    		// Hibernate
    		void BtnHibernateClick(object sender, System.EventArgs e)
    		{
    			Application.SetSuspendState(PowerState.Hibernate, true, true);
    		}
    		
    		// Stand By
    		void BtnStandbyClick(object sender, System.EventArgs e)
    		{
    			Application.SetSuspendState(PowerState.Suspend, true, true);
    		}
    	}
    }
    Nhưng khi em chạy các chức năng như LogOf, standBy... thì chạy tốt nhưng chức năng shutdown thì không chạy được, bác nào biết chỉ giúp em với, cám ơn!

  2. #2
    Ngày tham gia
    Dec 2015
    Bài viết
    0
    bạn phải đặt quyền tắt máy bằng hàm API,bạn có thể xem ở đây và gán tham số như trong C++ vậy
    http://forums.congdongcviet.com/show...A%AFt+m%C3%A1y

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •