I have to disable LID of a Laptop or netbook using C# or C++. I found information about how to get power information.
Is this Possible? Using Command LIne is posible to change this settins, well, i need to translate this to c#. Thanks.
Yes you can. I don't have exact code for it but some reference which will definitely help you in visual cpp. Here is blog for how to work when LID closed and also this microsoft msdn will get you clear overview on it. Go with Microsoft Power Management which have list of all power management related events, methods and constants. Use as per your need.
Here is some snippet which deals with system events like start,shutdown, log in, log off.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler Microsoft.Win32.SystemEvents.PowerModeChanged, AddressOf SysSuspend
AddHandler Microsoft.Win32.SystemEvents.SessionEnding, AddressOf SysLogOff
AddHandler Microsoft.Win32.SystemEvents.SessionSwitch, AddressOf SysSwitch
AddHandler Microsoft.Win32.SystemEvents.SessionEnded, AddressOf SysLoggedOff
AddHandler Microsoft.Win32.SystemEvents.PowerModeChanged, AddressOf StatusChange
End Sub
Public Sub SysLogOff(ByVal sender As Object, ByVal e As Microsoft.Win32.SessionEndingEventArgs)
If e.Reason = Microsoft.Win32.SessionEndReasons.Logoff Then
MessageBox.Show("user logging off")
ElseIf e.Reason = Microsoft.Win32.SessionEndReasons.SystemShutdown Then
MessageBox.Show("system is shutting down")
End If
End Sub
Public Sub SysLoggedOff(ByVal sender As Object, ByVal e As Microsoft.Win32.SessionEndedEventArgs)
If e.Reason = Microsoft.Win32.SessionEndReasons.Logoff Then
MessageBox.Show("user logged off")
ElseIf e.Reason = Microsoft.Win32.SessionEndReasons.SystemShutdown Then
MessageBox.Show("system has shut down")
End If
End Sub
Public Sub SysSwitch(ByVal sender As Object, ByVal e As Microsoft.Win32.SessionSwitchEventArgs)
If e.Reason = Microsoft.Win32.SessionSwitchReason.ConsoleConnect Then
MessageBox.Show("Console connect")
ElseIf e.Reason = Microsoft.Win32.SessionSwitchReason.ConsoleDisconnect Then
MessageBox.Show("Console disconnect")
ElseIf e.Reason = Microsoft.Win32.SessionSwitchReason.RemoteConnect Then
MessageBox.Show("Remote Connect")
ElseIf e.Reason = Microsoft.Win32.SessionSwitchReason.RemoteDisconnect Then
MessageBox.Show("Remote Disconnect")
ElseIf e.Reason = Microsoft.Win32.SessionSwitchReason.SessionLock Then
MessageBox.Show("Session Lock")
ElseIf e.Reason = Microsoft.Win32.SessionSwitchReason.SessionLogoff Then
MessageBox.Show("Session Logoff")
ElseIf e.Reason = Microsoft.Win32.SessionSwitchReason.SessionLogon Then
MessageBox.Show("Session Logon")
ElseIf e.Reason = Microsoft.Win32.SessionSwitchReason.SessionRemoteControl Then
MessageBox.Show("Session Remote Control")
ElseIf e.Reason = Microsoft.Win32.SessionSwitchReason.SessionUnlock Then
MessageBox.Show("Session Unlock", "", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification)
End If
End Sub
Public Sub SysSuspend(ByVal sender As Object, ByVal e As Microsoft.Win32.PowerModeChangedEventArgs)
If e.Mode = Microsoft.Win32.PowerModes.Resume Then
MessageBox.Show("system is resuming")
ElseIf e.Mode = Microsoft.Win32.PowerModes.Suspend Then
MessageBox.Show("system being suspended")
ElseIf e.Mode = Microsoft.Win32.PowerModes.StatusChange Then
MessageBox.Show("system has a status change")
End If
End Sub
Public Sub StatusChange(ByVal sender As Object, ByVal e As Microsoft.Win32.PowerModeChangedEventArgs)
If e.Mode = Microsoft.Win32.PowerModes.StatusChange Then
MessageBox.Show("Battery status has changed")
End If
End Sub
First write a batch file :
call powercfg –q >poweroptions.txt
under administrator privileges. Second wirte anothe batch file as a template:
call Powercfg –SETACVALUEINDEX [powerschemeGUID] [putsubgroupGUID] [putpowersettingGUID] 000
at the end, rewrite the template using the keywords above, for future using.
public static void RunPowerCFGReport()
{
Process.Start(BUConfig.CurrentPath + @"\PowerCFG");
}
public static string GetSubGroupGuid(string AppPath)
{
string[] lines = File.ReadAllLines(AppPath);
string guid = lines.Where(s => s.Contains("Power buttons and lid")).First();
string[] loops = guid.Split(':') ;
string pr = loops[1].Replace(" (Power buttons and lid)", "");
return pr.Trim();
}
public static string GetPowerSchemeGuid(string AppPath)
{
string[] lines = File.ReadAllLines(AppPath);
string guid = lines.Where(s => s.Contains("Power Scheme GUID:")).First();
string[] loops = guid.Split(':');
string pr = loops[1].Replace(" (Balanced)", "");
return pr.Trim();
}
public static string GetPowerSettingGUID(string AppPath)
{
string[] lines = File.ReadAllLines(AppPath);
string guid = lines.Where(s => s.Contains(" (Lid close action)")).First();
string[] loops = guid.Split(':');
string pr = loops[1].Replace(" (Lid close action)", "");
return pr.Trim();
}
public static void SetPowerCFGValues(string _pathFile, string _currentPowerSchemeGuid, string _currentPowerSettingGuid, string _currentGroupGuid)
{
try
{
StreamWriter wr;
string currentCommand = File.ReadAllText(_pathFile);
currentCommand = currentCommand.Replace("[powerschemeGUID]", _currentPowerSchemeGuid);
currentCommand = currentCommand.Replace("[putsubgroupGUID]", _currentGroupGuid);
currentCommand = currentCommand.Replace("[putpowersettingGUID]", _currentPowerSettingGuid);
wr = new StreamWriter(_pathFile);
wr.WriteLine(currentCommand);
wr.Close();
}
catch(Exception ex)
{
}
}
public static void SetLIDAction(string _lidAction)
{
Process.Start(BUConfig.CurrentPath + @"\Test.Bat");
}
public static void ResetLIDFile()
{
string commandtmeplate = "call Powercfg –SETACVALUEINDEX [powerschemeGUID] [putsubgroupGUID] [putpowersettingGUID] 000";
StreamWriter wr = new StreamWriter(BUConfig.CurrentPath + @"\Test.Bat");
wr.WriteLine(commandtmeplate);
}
I tried using c# directly, but visual studio returns an error "Corrupt file".
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.