马上10.1了,都来秀秀自己的实用代码,顺便散分

白鸽 2011-09-29 04:09:24
抛砖引玉啊,随便发个,给晚上和我有一样的朋友用,经常会想定时关机,然后放个电影看到睡着。
定时关机系统实现原理:
主要是针对XP系统实现的,通过使用cmd的命令来实现系统的定时关机。
通过Process类来实现调用cmd,用Timer控件来监控系统时间实现时间的匹配。主要代码如下:
Process myProcess = new Process();
myProcess.StartInfo.FileName = "cmd.exe";
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");
myProcess.Close();
补充cmd命令:
shutdown -s -t 0 关机命令
shutdown -l -t 0 注销命令
shutdown –a 取消命令
...全文
2867 143 打赏 收藏 转发到动态 举报
写回复
用AI写文章
143 条回复
切换为时间正序
请发表友善的回复…
发表回复
hwzhong20 2011-10-08
  • 打赏
  • 举报
回复
great!Thanks a lot!
白鸽 2011-10-08
  • 打赏
  • 举报
回复
十一累惨了!!回来给大家结分。。。
ck289444660 2011-10-08
  • 打赏
  • 举报
回复
关机,重启,注销操作:
先加入 using System.Runtime.InteropServices;的引用。
代码如下:
public class Shudown
{
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}
[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 DoFlag, 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 static void DoExitWin(int DoFlag)
{
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, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
ok = ExitWindowsEx(DoFlag, 0);
}

public static void Reboot()
{
DoExitWin(EWX_FORCE | EWX_REBOOT);
}
public static void PowerOff()
{
DoExitWin(EWX_FORCE | EWX_POWEROFF);
}
public static void LogOff()
{
DoExitWin(EWX_FORCE | EWX_LOGOFF);
}
}
呃.....通过调用系统的DLL文件,加以不同的参数,来实现关机,重启,注销的操作。
调用很简单 只要调用 Reboot()重启,PowerOff()关闭,LogOff()注销 这三个函数就OK了~!
AnwJiango 2011-10-08
  • 打赏
  • 举报
回复
c 一句就搞定了 system("shutdown -a");

为了这个小程序还得装 net framework...纠结。。。
anything 2011-10-08
  • 打赏
  • 举报
回复
bios里面可以设置自动开机时间的啊,没用过么?
ajiangfeijun 2011-10-07
  • 打赏
  • 举报
回复
接分过年
casker 2011-10-07
  • 打赏
  • 举报
回复
Parallel.For(0, Long.MaxValue, i =>
{
Console.WriteLine("并行接分");
})
ChrisAK 2011-10-07
  • 打赏
  • 举报
回复
[Quote=引用 34 楼 vrhero 的回复:]

定时关太死板了...Windows的电源管理计划没人知道?设置一下空闲关机或休眠就ok了,还写啥代码...
[/Quote]俺习惯直接打shutdown -s -t 时间...
sun0201 2011-10-07
  • 打赏
  • 举报
回复
学习了!~~~~
sdseawolf 2011-10-07
  • 打赏
  • 举报
回复
定时关机的源码,ontime函数里比对和设定时间是否相等,相等则关闭计算机
void CTimeShutDownDlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
//////////////////////////////////////////////////////////////////////////
//显示当前时间
CTime t = CTime::GetCurrentTime();
m_sCurTime=t.Format("%H:%M:%S");

m_cCurTime.SetWindowText(m_sCurTime);


if (m_sCurTime==m_sSettedTime)
{
//////////////////////////////////////////////////////////////////////////
//关机
//////////////////////////////////////////////////////////////////////////
HANDLE hToken;
TOKEN_PRIVILEGES tkp;


// Get a token for this process.

if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
AfxMessageBox(L"OpenProcessToken");

// Get the LUID for the shutdown privilege.

LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
&tkp.Privileges[0].Luid);

tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

// Get the shutdown privilege for this process.

AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES)NULL, 0);

// Cannot test the return value of AdjustTokenPrivileges.

if (GetLastError() != ERROR_SUCCESS)
AfxMessageBox(L"AdjustTokenPrivileges");

// Shut down the system and force all applications to close.

if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0))
AfxMessageBox(L"ExitWindowsEx");


}

CDialog::OnTimer(nIDEvent);
}
cjh200102 2011-10-04
  • 打赏
  • 举报
回复
GOOD
Rommend 2011-10-03
  • 打赏
  • 举报
回复
what ~?
回眸婉约 2011-10-03
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 chenyingshu880603 的回复:]
拔电源!OK!
[/Quote]
正解
小妹917 2011-10-03
  • 打赏
  • 举报
回复
谢谢楼主分享
a813101629 2011-10-03
  • 打赏
  • 举报
回复
双手抬高,求分
hj460910133 2011-10-03
  • 打赏
  • 举报
回复
用WMI实现关机、重启和注销,还可以远程操作。
yojinlin 2011-10-03
  • 打赏
  • 举报
回复
友情幫頂。
juanjuan5211314 2011-10-03
  • 打赏
  • 举报
回复
shutdown /p
从开始到现在 2011-10-03
  • 打赏
  • 举报
回复
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.IO;
using System.Reflection;
using System.Diagnostics;

namespace Caculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Caculate ca = new Caculate();
ca.RegType(textBox1.Text);
ca.Compile();
IA a = ca.Create_A();
textBox2.Text = a.f().ToString() ;
ca.UnloadAdm();
}
}
public interface IA
{
int f();
}

public class Caculate:MarshalByRefObject
{
AppDomain adm;


public void RegType(string str) // 动态注册一个类型
{
StreamWriter sw = new StreamWriter("a.cs");
sw.WriteLine("public class A :System.MarshalByRefObject, Caculator.IA { public int f() { try { return " + str + "; } catch { return int.MinValue; } } } ");
sw.Close();
}

// C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe
// C:\Windows\System32\cmd.exe
//在C盘根目录下 out.txt查看是否编译成功

public void Compile()//编译 下面的字符串写你的CMD 和 编译器路径
{
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = @"C:\Windows\System32\cmd.exe";
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
info.Arguments = @"/c C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe /t:library " + path + @"\a.cs /r:"+ Assembly.GetExecutingAssembly().Location+@" >E:\out.txt";
info.WindowStyle = ProcessWindowStyle.Hidden;
Process p = Process.Start(info);
p.WaitForExit();
}
public IA Create_A()//创建A的对象
{
adm = AppDomain.CreateDomain("abc", null, new AppDomainSetup());
return adm.CreateInstanceFromAndUnwrap("a.dll", "A") as IA;

//string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
//Assembly asm = Assembly.LoadFrom(path + @"\a.dll");
//IA a = asm.CreateInstance("A") as IA;
//return a;
}
public void UnloadAdm()
{
AppDomain.Unload(adm);

}
}
}


反射计算器 刚写的
不屈意志 2011-10-01
  • 打赏
  • 举报
回复
[Quote=引用 91 楼 k767474055 的回复:]
引用 79 楼 wjfwd2009 的回复:
引用 77 楼 dtb2010 的回复:
能不能实现自动开机呢????

自动开机需要特殊的电源


可以通过网卡实现开机吧
[/Quote]
我把十一当成 运算符了
加载更多回复(123)

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧