Click here to Skip to main content
15,902,634 members
Home / Discussions / C#
   

C#

 
AnswerRe: how to save the files in c sharp.net Pin
Stefan Troschuetz29-Aug-05 21:39
Stefan Troschuetz29-Aug-05 21:39 
GeneralRe: how to save the files in c sharp.net Pin
sajikp30-Aug-05 20:18
sajikp30-Aug-05 20:18 
GeneralRe: how to save the files in c sharp.net Pin
Stefan Troschuetz30-Aug-05 21:01
Stefan Troschuetz30-Aug-05 21:01 
QuestionCapture and streaming Pin
aspiringCodeMonkey29-Aug-05 21:09
aspiringCodeMonkey29-Aug-05 21:09 
AnswerRe: Capture and streaming Pin
Dario Solera29-Aug-05 21:22
Dario Solera29-Aug-05 21:22 
QuestionSuperscript Pin
Patrik Ruzic29-Aug-05 19:49
Patrik Ruzic29-Aug-05 19:49 
AnswerRe: Superscript Pin
Christian Graus29-Aug-05 20:03
protectorChristian Graus29-Aug-05 20:03 
Questionhelp me to correct my code Pin
sajikp29-Aug-05 19:39
sajikp29-Aug-05 19:39 
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace shutdown
{
///
/// Summary description for Form1.
///

public class frmshutdown : System.Windows.Forms.Form
{
internal System.Windows.Forms.GroupBox grpQuestion;
internal System.Windows.Forms.Button btnDo;
internal System.Windows.Forms.RadioButton rdbLock;
internal System.Windows.Forms.RadioButton rdbLogoff;
internal System.Windows.Forms.RadioButton rdbRestart;
internal System.Windows.Forms.RadioButton rdbShutdown;
///
/// Required designer variable.
///

private System.ComponentModel.Container components = null;

public frmshutdown()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

///
/// Clean up any resources being used.
///

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

#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.grpQuestion = new System.Windows.Forms.GroupBox();
this.btnDo = new System.Windows.Forms.Button();
this.rdbLock = new System.Windows.Forms.RadioButton();
this.rdbLogoff = new System.Windows.Forms.RadioButton();
this.rdbRestart = new System.Windows.Forms.RadioButton();
this.rdbShutdown = new System.Windows.Forms.RadioButton();
this.grpQuestion.SuspendLayout();
this.SuspendLayout();
//
// grpQuestion
//
this.grpQuestion.Controls.Add(this.btnDo);
this.grpQuestion.Controls.Add(this.rdbLock);
this.grpQuestion.Controls.Add(this.rdbLogoff);
this.grpQuestion.Controls.Add(this.rdbRestart);
this.grpQuestion.Controls.Add(this.rdbShutdown);
this.grpQuestion.Location = new System.Drawing.Point(26, 56);
this.grpQuestion.Name = "grpQuestion";
this.grpQuestion.Size = new System.Drawing.Size(240, 160);
this.grpQuestion.TabIndex = 1;
this.grpQuestion.TabStop = false;
this.grpQuestion.Text = "What do you want to do?";
//
// btnDo
//
this.btnDo.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnDo.Location = new System.Drawing.Point(160, 120);
this.btnDo.Name = "btnDo";
this.btnDo.Size = new System.Drawing.Size(64, 23);
this.btnDo.TabIndex = 5;
this.btnDo.Text = "Do";
this.btnDo.Click += new System.EventHandler(this.btnDo_Click);
//
// rdbLock
//
this.rdbLock.Location = new System.Drawing.Point(16, 120);
this.rdbLock.Name = "rdbLock";
this.rdbLock.Size = new System.Drawing.Size(128, 24);
this.rdbLock.TabIndex = 4;
this.rdbLock.Text = "Lock Computer";
//
// rdbLogoff
//
this.rdbLogoff.Location = new System.Drawing.Point(16, 88);
this.rdbLogoff.Name = "rdbLogoff";
this.rdbLogoff.Size = new System.Drawing.Size(128, 24);
this.rdbLogoff.TabIndex = 3;
this.rdbLogoff.Text = "Log off";
//
// rdbRestart
//
this.rdbRestart.Location = new System.Drawing.Point(16, 56);
this.rdbRestart.Name = "rdbRestart";
this.rdbRestart.Size = new System.Drawing.Size(128, 24);
this.rdbRestart.TabIndex = 2;
this.rdbRestart.Text = "Restart";
//
// rdbShutdown
//
this.rdbShutdown.Checked = true;
this.rdbShutdown.Location = new System.Drawing.Point(16, 24);
this.rdbShutdown.Name = "rdbShutdown";
this.rdbShutdown.Size = new System.Drawing.Size(128, 24);
this.rdbShutdown.TabIndex = 1;
this.rdbShutdown.TabStop = true;
this.rdbShutdown.Text = "Shutdown";
//
// frmshutdown
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.grpQuestion);
this.Name = "frmshutdown";
this.Text = "shutdown";
this.Load += new System.EventHandler(this.frmshutdown_Load);
this.grpQuestion.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

///
/// The main entry point for the application.
///

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


private void btnDo_Click(object sender, System.EventArgs e)
{
shutmode st=new shutmode();
if(rdbShutdown.Checked == true)
st.Shutdown(); //for shutdown
else if(rdbRestart.Checked == true)
st.Restart(); // Restart

else if(rdbLogoff.Checked == true)
st.LogOff(); //' Log off
else if(rdbLock.Checked ==true)
st.LockTheComputer();//' Lock Workstation
else
MessageBox.Show("Invalid option", "Illegal operation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
// ' Invalid Radio Button (should never been shown)

}


}
}


using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;
namespace shutdown
{
///
/// Summary description for shutmode.
///

public class shutmode
{
public shutmode()
{
//
// TODO: Add constructor logic here
//
}

// Constants
const int SE_PRIVILEGE_ENABLED=2;
const int TOKEN_QUERY=8;
const int TOKEN_ADJUST_PRIVILEGES=32;
const string SE_SHUTDOWN_NAME= "SeShutdownPrivilege";
//' Exit Windows Constant
const int EWX_LOGOFF = 0;
const int EWX_SHUTDOWN= 1;
const int EWX_REBOOT = 2;
const int EWX_FORCE = 4;
const int EWX_POWEROFF = 8;
const int EWX_FORCEIFHUNG= 16;
[StructLayout(LayoutKind.Sequential, Pack=1)]
//'Structure

internal struct Luid
{
public int Count;
public long luid;
public int Attr;
};

//End Structure 'TokPriv1Luid
//' Lock Workstation

[DllImport("user32.dll", ExactSpelling=true)]
static extern bool LockWorkStation();

//' Get Current Processes
[DllImport("kernel32.dll", ExactSpelling=true)]
static extern IntPtr GetCurrentProcess();

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


//' Look up Priviledge Value

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


// Adjust Token Priviledges

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


//' Exit Windows

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



//' Exit Windows Sub

private void DoExitWindows(int flg)
{
Luid tp;
bool s;
int h1,h2;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, htok);
tp.Count = 1;
tp.luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
LookupPrivilegeValue(null, SE_SHUTDOWN_NAME,ref tp.luid);
AdjustTokenPrivileges(htok, false, tp, 0, IntPtr.Zero, IntPtr.Zero);
ExitWindowsEx(flg,0);

}

//' Shutdown

public void Shutdown()
{
DoExitWindows(EWX_SHUTDOWN);
}

// ' Restart
public void Restart()
{
DoExitWindows(EWX_REBOOT | EWX_FORCE);
}
//' Log off
public void LogOff()
{
DoExitWindows(EWX_LOGOFF);
}
//' Lock Workstation

public void LockTheComputer()
{
LockWorkStation();
}


}
}


-- modified at 1:46 Tuesday 30th August, 2005
AnswerRe: help me to correct my code Pin
Christian Graus29-Aug-05 19:42
protectorChristian Graus29-Aug-05 19:42 
GeneralRe: help me to correct my code Pin
sajikp29-Aug-05 19:58
sajikp29-Aug-05 19:58 
GeneralRe: help me to correct my code Pin
Christian Graus29-Aug-05 20:01
protectorChristian Graus29-Aug-05 20:01 
GeneralRe: help me to correct my code Pin
sajikp29-Aug-05 20:54
sajikp29-Aug-05 20:54 
QuestionProblems with FileStream Pin
Tehnoon Raza29-Aug-05 19:13
sussTehnoon Raza29-Aug-05 19:13 
AnswerRe: Problems with FileStream Pin
Christian Graus29-Aug-05 19:24
protectorChristian Graus29-Aug-05 19:24 
GeneralRe: Problems with FileStream Pin
Member 171916929-Aug-05 19:34
Member 171916929-Aug-05 19:34 
GeneralRe: Problems with FileStream Pin
Christian Graus29-Aug-05 19:40
protectorChristian Graus29-Aug-05 19:40 
GeneralRe: Problems with FileStream Pin
Matt Gerrans29-Aug-05 20:09
Matt Gerrans29-Aug-05 20:09 
GeneralRe: Problems with FileStream Pin
Dave Kreskowiak30-Aug-05 7:41
mveDave Kreskowiak30-Aug-05 7:41 
QuestionComparing string with string array Pin
binglin29-Aug-05 18:58
binglin29-Aug-05 18:58 
AnswerRe: Comparing string with string array Pin
Guffa29-Aug-05 19:04
Guffa29-Aug-05 19:04 
AnswerRe: Comparing string with string array Pin
mkani30-Aug-05 21:19
mkani30-Aug-05 21:19 
QuestionTimer Pin
Taurian11029-Aug-05 18:28
Taurian11029-Aug-05 18:28 
AnswerRe: Timer Pin
Andy Brummer29-Aug-05 18:41
sitebuilderAndy Brummer29-Aug-05 18:41 
AnswerRe: Timer Pin
Alomgir Miah29-Aug-05 18:47
Alomgir Miah29-Aug-05 18:47 
AnswerRe: Timer Pin
mav.northwind29-Aug-05 20:35
mav.northwind29-Aug-05 20:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.