各种小功能的代码
各种小功能的代码,开放给大家,一起用!
一、设置系统休眠: Application.SetSuspendState(PowerState.Hibernate, true, false);//设置休眠状态
二、设置系统关机: public partial class Frm_Main : Form
public Frm_Main()
{
InitializeComponent();
}
private int isClose = 0;//声明一个变量表示是否关机
private const int WM_QUERYENDSESSION = 0x0011;//系统发送的关机命令
protected override void WndProc(ref Message m)//此方法用于处理Windows消息
{
switch (m.Msg)//获取消息值
{
case WM_QUERYENDSESSION:
m.Result = (IntPtr)isClose;//为了响应消息处理,设置返回值
break;
default://默认执行
base.WndProc(ref m);//重写此方法
break;
}
}
private void button1_Click(object sender, EventArgs e)
{
isClose = 0;//使消息值等于0,实现禁止关机
MessageBox.Show("禁止关闭计算机");
}
private void button2_Click(object sender, EventArgs e)
{
isClose = 1;//使消息值等于1,实现允许关机
MessageBox.Show("允许关闭计算机");
}
}
三、注销系统: [DllImport("user32.dll", EntryPoint = "ExitWindowsEx", CharSet = CharSet.Ansi)]
四、关闭系统: System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.UseShellExecute = false;//是否使用系统外壳程序启动进程
myProcess.StartInfo.RedirectStandardInput = true;//是否从流中读取
myProcess.StartInfo.RedirectStandardOutput = true;//是否写入流
myProcess.StartInfo.RedirectStandardError = true;//是否将错误信息写入流
myProcess.StartInfo.CreateNoWindow = true;//是否在新窗口中启动进程
myProcess.Start();//启动进程
myProcess.StandardInput.WriteLine("shutdown -s -t 0");//执行关机命令
五、重启系统:System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.UseShellExecute = false;//是否使用系统外壳程序启动进程
myProcess.StartInfo.RedirectStandardInput = true;//是否从流中读取
myProcess.StartInfo.RedirectStandardOutput = true;//是否写入流
myProcess.StartInfo.RedirectStandardError = true;//是否将错误信息写入流
myProcess.StartInfo.CreateNoWindow = true;//是否在新窗口中启动进程
myProcess.Start();//启动进程
myProcess.StandardInput.WriteLine("shutdown -r -t 0");//执行重启计算机命令
六、远程关闭或者重启电脑:
/// 关闭或重启远程计算机
/// </summary>
/// <param name="doinfo">要执行的操作命令</param>
private void CloseComputer(string doinfo)
{
ConnectionOptions op = new ConnectionOptions();//创建ConnectionOptions对象
op.Username = textBox4.Text;//设置远程机器用户名
op.Password = textBox3.Text;//设置远程机器登录密码
//创建ManagementScope对象
ManagementScope scope = new ManagementScope("\\\\" + textBox2.Text + "\\root\\cimv2:Win32_Service", op);
try
{
scope.Connect();//连接远程对象
ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");//实例化ObjectQuery对象
//创建ManagementObjectSearcher对象
ManagementObjectSearcher query1 = new ManagementObjectSearcher(scope, oq);
ManagementObjectCollection queryCollection1 = query1.Get();//得到WMI控制
foreach (ManagementObject mobj in queryCollection1)
{
string[] str = { "" };
mobj.InvokeMethod(doinfo, str);
}
}
catch (Exception ey)
{
MessageBox.Show(ey.Message);
}
}
七、取消磁盘共享:
System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.UseShellExecute = false;//是否使用系统外壳程序启动进程
myProcess.StartInfo.RedirectStandardInput = true;//是否从流中读取
myProcess.StartInfo.RedirectStandardOutput = true;//是否写入流
myProcess.StartInfo.RedirectStandardError = true;//是否将错误信息写入流
myProcess.StartInfo.CreateNoWindow = true;//是否在新窗口中启动进程
myProcess.Start();//启动进程
//执行取消磁盘共享命令
myProcess.StandardInput.WriteLine("NET SHARE " + textBox1.Text + "$ /DEL");
MessageBox.Show("执行成功", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
八、获取磁盘信息:SelectQuery selectQuery = new SelectQuery("select * from win32_logicaldisk");//查询磁盘信息
foreach (ManagementObject disk in searcher.Get())//遍历所有磁盘
{
comboBox1.Items.Add(disk["Name"].ToString());//将磁盘名称添加到下拉列表中
}
九、验证磁盘是否可写:DriveInfo dinfo = new DriveInfo(comboBox1.Text);//创建DriveInfo对象
label2.Text = "该磁盘已经准备好";//如果准备好则弹出提示
else//否则
label2.Text = "该磁盘未准备好";//通知磁盘未准备好
十、检查磁盘的可用容量:
float tsize = dinfo.TotalSize;//获得磁盘的总容量
float fsize = dinfo.TotalFreeSpace;//获取剩余容量
Graphics graphics = this.CreateGraphics();//创建Graphics绘图对象
Pen pen1 = new Pen(Color.Red);//创建画笔对象
Brush brush1 = new SolidBrush(Color.WhiteSmoke);//创建笔刷
Brush brush2 = new SolidBrush(Color.LimeGreen);//创建笔刷
Brush brush3 = new SolidBrush(Color.RoyalBlue);//创建笔刷
Font font1 = new Font("Courier New", 16, FontStyle.Bold);//设置字体
Font font2 = new Font("宋体", 9);//设置字体
graphics.DrawString("磁盘容量分析", font1, brush2, new Point(60, 50));//绘制文本
float angle1 = Convert.ToSingle((360 * (Convert.ToSingle(fsize / 100000000000) / Convert.ToSingle(tsize / 100000000000))));//计算绿色饼形图的范围
float angle2 = Convert.ToSingle((360 * (Convert.ToSingle((tsize - fsize) / 100000000000) / Convert.ToSingle(tsize / 100000000000))));//计算蓝色饼形图的范围
//调用Graphics对象的FillPie方法绘制饼形图
graphics.FillPie(brush2, 60, 80, 150, 150, 0, angle1);
graphics.FillPie(brush3, 60, 80, 150, 150, angle1, angle2);
graphics.DrawRectangle(pen1, 30, 235, 200, 50);
graphics.FillRectangle(brush2, 35, 245, 20, 10);
graphics.DrawString("磁盘剩余容量:" + dinfo.TotalFreeSpace / 1000 + "KB", font2, brush2, 55, 245);
graphics.FillRectangle(brush3, 35, 265, 20, 10);
graphics.DrawString("磁盘已用容量:" + (dinfo.TotalSize - dinfo.TotalFreeSpace) / 1000 + "KB", font2, brush3, 55, 265);