建立Winform后,以管理员权限运行。
建立两个按钮和三个listbox(listbox可以用数组代替,为了可视化我不再用数组)
修改代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Text.RegularExpressions;
using Microsoft.Win32
using System.Runtime.InteropServices;
namespace PowerSaver
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string saverid = "";
string curreid = "";
string newerid = "";
string firstInner = "";
string Bigdex = "";
string Smldex = "";
#region 应用函数库
private string directCommand(string incmd)
{
ProcessStartInfo start = new ProcessStartInfo("cmd");
start.RedirectStandardInput = true;
start.RedirectStandardOutput = true;
start.UseShellExecute = false;
start.CreateNoWindow = true;
Process pro = Process.Start(start);
pro.StandardInput.WriteLine("chcp 437");
pro.StandardInput.WriteLine(incmd);
pro.StandardInput.WriteLine("exit");
string result = pro.StandardOutput.ReadToEnd();
return result;
}
string tentosixteen(int input)
{
return String.Format("{0:X}", input);
}
int sixteentoten(string input)
{
return Int32.Parse(input, System.Globalization.NumberStyles.HexNumber);
}
#endregion
private void button1_Click(object sender, EventArgs e)
{
#region 首次刷新列表
//首次输入列表
firstInner = directCommand("powercfg /l");
//正则提取列表
Regex reg = new Regex(@"(?im)(?<=^\s*Power Scheme GUID:\s*).+$");
foreach (Match m in reg.Matches(firstInner))
{
//得到节能和当前
this.listBox1.Items.Add(m.Value);
if (m.Value.Contains("Power saver"))
{
saverid = m.Value.Substring(0, 37);
}
if (m.Value.Contains("*"))
{
curreid = m.Value.Substring(0, 37);
}
}
# endregion
#region 复制节能计划新建项ID
//复制节能项
string inner2 = directCommand("powercfg /DUPLICATESCHEME " + saverid);
Regex reg2 = new Regex(@"(?im)(?<=^\s*Power Scheme GUID:\s*).+$");
//得到节能项id
foreach (Match m in reg2.Matches(inner2))
{
newerid = m.Value;
}
//提取前37字符
newerid = newerid.Substring(0,37);
#endregion
#region 刷新列表
//再次刷新列表以实现开发人员看到状态
firstInner = directCommand("powercfg /l");
Regex reg3 = new Regex(@"(?im)(?<=^\s*Power Scheme GUID:\s*).+$");
listBox1.Items.Clear();
foreach (Match m in reg3.Matches(firstInner))
{
this.listBox1.Items.Add(m.Value);
if (m.Value.Contains("Power saver"))
{
saverid = m.Value.Substring(0, 37);
}
if (m.Value.Contains("*"))
{
curreid = m.Value.Substring(0, 37);
}
}
#endregion
#region 提取属性id
//列举新建项属性列表
string inner3 = directCommand("powercfg /q "+newerid);
Regex reg4 = new Regex(@"(?im)(?<=^\s* Power Setting GUID: \s*).+$");
//提取小属性到2
listBox2.Items.Clear();
foreach (Match m in reg4.Matches(inner3))
{
this.listBox2.Items.Add(m.Value);
if (m.Value.Contains("Display brightness"))
{
Smldex = m.Value.Substring(0,37);
}
}
Regex reg5 = new Regex(@"(?im)(?<=^\s* Subgroup GUID:\s*).+$");
//提取大属性到3
listBox3.Items.Clear();
foreach (Match m in reg5.Matches(inner3))
{
this.listBox3.Items.Add(m.Value);
if (m.Value.Contains("Display"))
{
Bigdex = m.Value.Substring(0, 37);
}
}
#endregion
//改新项名
MessageBox.Show(directCommand("powercfg /changename "+newerid+" \"CMLG Power Saver\""));
//执行命令设置新项亮度属性
directCommand("powercfg /SETACVALUEINDEX " + newerid + " "+Bigdex+" "+Smldex+" "+ "1");
//应用设置
directCommand("powercfg /s "+newerid);
}
private void button2_Click(object sender, EventArgs e)
{
//切换为旧设置
directCommand("powercfg /s "+curreid);
//删除新项
directCommand("powercfg /d "+newerid);
#region 刷新列表
//刷新列表
firstInner = directCommand("powercfg /l");
Regex reg3 = new Regex(@"(?im)(?<=^\s*Power Scheme GUID:\s*).+$");
listBox1.Items.Clear();
foreach (Match m in reg3.Matches(firstInner))
{
this.listBox1.Items.Add(m.Value);
if (m.Value.Contains("Power saver"))
{
saverid = m.Value.Substring(0, 37);
}
if (m.Value.Contains("*"))
{
curreid = m.Value.Substring(0, 37);
}
}
#endregion
}
}
}
建立两个按钮和三个listbox(listbox可以用数组代替,为了可视化我不再用数组)
修改代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Text.RegularExpressions;
using Microsoft.Win32
using System.Runtime.InteropServices;
namespace PowerSaver
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string saverid = "";
string curreid = "";
string newerid = "";
string firstInner = "";
string Bigdex = "";
string Smldex = "";
#region 应用函数库
private string directCommand(string incmd)
{
ProcessStartInfo start = new ProcessStartInfo("cmd");
start.RedirectStandardInput = true;
start.RedirectStandardOutput = true;
start.UseShellExecute = false;
start.CreateNoWindow = true;
Process pro = Process.Start(start);
pro.StandardInput.WriteLine("chcp 437");
pro.StandardInput.WriteLine(incmd);
pro.StandardInput.WriteLine("exit");
string result = pro.StandardOutput.ReadToEnd();
return result;
}
string tentosixteen(int input)
{
return String.Format("{0:X}", input);
}
int sixteentoten(string input)
{
return Int32.Parse(input, System.Globalization.NumberStyles.HexNumber);
}
#endregion
private void button1_Click(object sender, EventArgs e)
{
#region 首次刷新列表
//首次输入列表
firstInner = directCommand("powercfg /l");
//正则提取列表
Regex reg = new Regex(@"(?im)(?<=^\s*Power Scheme GUID:\s*).+$");
foreach (Match m in reg.Matches(firstInner))
{
//得到节能和当前
this.listBox1.Items.Add(m.Value);
if (m.Value.Contains("Power saver"))
{
saverid = m.Value.Substring(0, 37);
}
if (m.Value.Contains("*"))
{
curreid = m.Value.Substring(0, 37);
}
}
# endregion
#region 复制节能计划新建项ID
//复制节能项
string inner2 = directCommand("powercfg /DUPLICATESCHEME " + saverid);
Regex reg2 = new Regex(@"(?im)(?<=^\s*Power Scheme GUID:\s*).+$");
//得到节能项id
foreach (Match m in reg2.Matches(inner2))
{
newerid = m.Value;
}
//提取前37字符
newerid = newerid.Substring(0,37);
#endregion
#region 刷新列表
//再次刷新列表以实现开发人员看到状态
firstInner = directCommand("powercfg /l");
Regex reg3 = new Regex(@"(?im)(?<=^\s*Power Scheme GUID:\s*).+$");
listBox1.Items.Clear();
foreach (Match m in reg3.Matches(firstInner))
{
this.listBox1.Items.Add(m.Value);
if (m.Value.Contains("Power saver"))
{
saverid = m.Value.Substring(0, 37);
}
if (m.Value.Contains("*"))
{
curreid = m.Value.Substring(0, 37);
}
}
#endregion
#region 提取属性id
//列举新建项属性列表
string inner3 = directCommand("powercfg /q "+newerid);
Regex reg4 = new Regex(@"(?im)(?<=^\s* Power Setting GUID: \s*).+$");
//提取小属性到2
listBox2.Items.Clear();
foreach (Match m in reg4.Matches(inner3))
{
this.listBox2.Items.Add(m.Value);
if (m.Value.Contains("Display brightness"))
{
Smldex = m.Value.Substring(0,37);
}
}
Regex reg5 = new Regex(@"(?im)(?<=^\s* Subgroup GUID:\s*).+$");
//提取大属性到3
listBox3.Items.Clear();
foreach (Match m in reg5.Matches(inner3))
{
this.listBox3.Items.Add(m.Value);
if (m.Value.Contains("Display"))
{
Bigdex = m.Value.Substring(0, 37);
}
}
#endregion
//改新项名
MessageBox.Show(directCommand("powercfg /changename "+newerid+" \"CMLG Power Saver\""));
//执行命令设置新项亮度属性
directCommand("powercfg /SETACVALUEINDEX " + newerid + " "+Bigdex+" "+Smldex+" "+ "1");
//应用设置
directCommand("powercfg /s "+newerid);
}
private void button2_Click(object sender, EventArgs e)
{
//切换为旧设置
directCommand("powercfg /s "+curreid);
//删除新项
directCommand("powercfg /d "+newerid);
#region 刷新列表
//刷新列表
firstInner = directCommand("powercfg /l");
Regex reg3 = new Regex(@"(?im)(?<=^\s*Power Scheme GUID:\s*).+$");
listBox1.Items.Clear();
foreach (Match m in reg3.Matches(firstInner))
{
this.listBox1.Items.Add(m.Value);
if (m.Value.Contains("Power saver"))
{
saverid = m.Value.Substring(0, 37);
}
if (m.Value.Contains("*"))
{
curreid = m.Value.Substring(0, 37);
}
}
#endregion
}
}
}
