c#制作的定时关机重起小软件(需要dotnetfx.exe支持)

下载地址:/Files/kasafuma/TimeShutDown.rar
图象如下:




代码如下:
using Microsoft.Win32;
using System.IO;
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.Timers;
namespace WinShutDown
{
    
/// <summary>
    
/// Form1 的摘要说明。
    
/// </summary>

    public class Form1 : System.Windows.Forms.Form
        
    
{
        [StructLayout(LayoutKind.Sequential, Pack
=1)]
            
internal struct TokPriv1Luid
        
{
            
public int Count;
            
public long Luid;
            
public int Attr;
        }

        
private System.ComponentModel.IContainer components;
    
        [DllImport(
"kernel32.dll", ExactSpelling=true) ]
        
internal static extern IntPtr GetCurrentProcess();

        [DllImport(
"advapi32.dll", ExactSpelling=true, SetLastError=true) ]
        
internal static extern bool OpenProcessToken( IntPtr h, int acc, ref IntPtr phtok );

        [DllImport(
"advapi32.dll", SetLastError=true) ]
        
internal static extern bool LookupPrivilegeValue( string host, string name, ref long pluid );

        [DllImport(
"advapi32.dll", ExactSpelling=true, SetLastError=true) ]
        
internal static extern bool AdjustTokenPrivileges( IntPtr htok, bool disall,
            
ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen );

        [DllImport(
"user32.dll", ExactSpelling=true, SetLastError=true) ]
        
internal static extern bool ExitWindowsEx( int flg, int rea );

        
internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
        
internal const int TOKEN_QUERY = 0x00000008;
        
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
        
internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
        
internal const int EWX_LOGOFF = 0x00000000;
        
internal const int EWX_SHUTDOWN = 0x00000001;
        
internal const int EWX_REBOOT = 0x00000002;
        
internal const int EWX_FORCE = 0x00000004;
        
internal const int EWX_POWEROFF = 0x00000008;
        
internal const int EWX_FORCEIFHUNG = 0x00000010;
        
private System.Windows.Forms.GroupBox groupBox1;
        
private System.Windows.Forms.Timer timer1;
        
private System.Windows.Forms.GroupBox groupBox2;
        
private System.Windows.Forms.GroupBox groupBox3;
        
private System.Windows.Forms.DateTimePicker dtpDay1;
        
private System.Windows.Forms.DateTimePicker dtpTime1;
        
private System.Windows.Forms.Button btnOK;
        
private System.Windows.Forms.NotifyIcon notifyIcon1;
        
private System.Windows.Forms.ContextMenu contextMenu1;
        
private System.Windows.Forms.MenuItem menuItem1;
        
private System.Windows.Forms.MenuItem menuItem2;
        
private System.Windows.Forms.MenuItem menuItem3;
        
private System.Windows.Forms.MenuItem menuItem4;
        
private System.Windows.Forms.Button btnCancel;
        
private DateTime shutdownTime=DateTime.Now;
        
private System.Windows.Forms.RadioButton rb_shutdown;
        
private System.Windows.Forms.RadioButton rb_reboot;
        
private System.Windows.Forms.MenuItem menuItem5;
        
private System.Windows.Forms.TextBox txtTime;
        
public Form1()
        
{
            
//
            
// Windows 窗体设计器支持所必需的
            
//
            InitializeComponent();

            
//
            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            
//
        }


        
/// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if (components != null
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
Windows 窗体设计器生成的代码
    
        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main() 
        
{
            Application.Run(
new Form1());
        }

        
private static void DoExitWin(int flg)
        
{
            
bool ok;
            TokPriv1Luid tp;
            IntPtr hproc 
= GetCurrentProcess();
            IntPtr htok 
= IntPtr.Zero;
            ok 
= OpenProcessToken( hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok );
            tp.Count 
= 1;
            tp.Luid 
= 0;
            tp.Attr 
= SE_PRIVILEGE_ENABLED;
            ok 
= LookupPrivilegeValue( null, SE_SHUTDOWN_NAME, ref tp.Luid );
            ok 
= AdjustTokenPrivileges( htok, falseref tp, 0, IntPtr.Zero, IntPtr.Zero );
            ok 
= ExitWindowsEx( flg, 0 );
        }



        
private void Form1_Load(object sender, System.EventArgs e)
       

            
this.notifyIcon1.Visible=true;
            
            DateTime shutdownTime
=DateTime.Now;                                    

            txtTime.Text
=System.DateTime.Now.ToString();            
            dtpDay1.Format 
= DateTimePickerFormat.Custom;
            dtpDay1.CustomFormat 
= "yyyy-MM-d";
            dtpTime1.Format 
= DateTimePickerFormat.Custom;
            dtpTime1.CustomFormat 
= "HH:mm:ss";            
            
        }


        
private void timer1_Tick(object sender, System.EventArgs e)
        
{    
         txtTime.Text
=System.DateTime.Now.ToString();            
         
string     CurrDate=System.DateTime.Today.ToShortDateString() ; 
         
string    CurrTime=System.DateTime.Now.ToLongTimeString();
            
            
if(CurrDate==dtpDay1.Value.ToShortDateString() && CurrTime==dtpTime1.Value.ToLongTimeString())
            
{
                
if(rb_reboot.Checked==true)
                
{
                    DoExitWin(EWX_REBOOT);
                    Application.Exit();

                }

                
else
                
{
                    DoExitWin(EWX_SHUTDOWN);
                    Application.Exit();
                }

                
            }


        }


        
private void btnOK_Click(object sender, System.EventArgs e)
        
{
            
this.Visible=false;
            
this.notifyIcon1.Visible=true;    
        }


        
private void menuItem1_Click(object sender, System.EventArgs e)
        
{
            notifyIcon1.Visible
=false;
            
this.Close();
            Application.Exit();
        }


        
private void notifyIcon1_DoubleClick(object sender, System.EventArgs e)
        
{
            
this.Visible=true;
        }


        
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        
{    
            e.Cancel
=true;
            
this.Visible=false;
            
this.notifyIcon1.Visible=true;
        }


        
private void menuItem2_Click(object sender, System.EventArgs e)
        
{
            
this.Visible=false;
            notifyIcon1.Visible
=false;
            
        }


        
private void btnCancel_Click(object sender, System.EventArgs e)
        
{
            notifyIcon1.Visible
=false;
            
this.Close();
            Application.Exit();

        }


        
private void menuItem3_Click(object sender, System.EventArgs e)
        
{
            Form2 frm2
=new Form2();
            frm2.Show();
        }


        
private void menuItem4_Click(object sender, System.EventArgs e)
        
{
            DoExitWin(EWX_POWEROFF);
            Application.Exit();
        
        }


        
private void menuItem5_Click(object sender, System.EventArgs e)
        
{
            DoExitWin(EWX_REBOOT);
            Application.Exit();
        
        }


        
bool isNumberic(string s)
        
{
            
try
            
{
                
int.Parse(s);
                
return true;
            }

            
catch
            
{
                
return false;

            }

        }


        
private void rb_reboot_CheckedChanged(object sender, System.EventArgs e)
        
{
            
if(rb_reboot.Checked==true)
            
{
                rb_shutdown.Checked
=false;
            }

            
else
            
{
                rb_shutdown.Checked
=true;

            }

        
        }


        
private void rb_shutdown_CheckedChanged(object sender, System.EventArgs e)
        
{
            
if(rb_shutdown.Checked==true)
            
{
                rb_reboot.Checked
=false;
            }

            
else
            
{
                rb_reboot.Checked
=true;
            }

        
        }


    }

}

posted on 2005-12-20 02:19  kasafuma  阅读(2222)  评论(0编辑  收藏  举报