使用.net以编程方式启用/禁用LID

时间:2014-07-28 13:56:56

标签: c# .net visual-c++

我必须使用C#或C ++禁用笔记本电脑或上网本的LID。我找到了有关如何获取电源信息的信息。

这可能吗? Using Command LIne可以改变这个问题,我需要将其转换为c#。 感谢。

2 个答案:

答案 0 :(得分:0)

是的,你可以。 我没有确切的代码,但一些参考肯定会帮助你在visual cpp。 以下是 how to work when LID closed的博客,此microsoft msdn将为您提供清晰的概述。使用Microsoft Power Management,其中包含所有与电源管理相关的事件,方法和常量的列表。根据您的需要使用。

以下是一些片段,用于处理系统事件,如启动,关闭,登录,注销。

    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

答案 1 :(得分:0)

首先写一个批处理文件:

    call powercfg –q >poweroptions.txt

在管理员权限下。  第二个批处理文件作为模板:

    call Powercfg –SETACVALUEINDEX [powerschemeGUID] [putsubgroupGUID] [putpowersettingGUID] 000

最后,使用上面的关键字重写模板,以备将来使用。

    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);
    }

我尝试直接使用c#,但是visual studio会返回错误" Corrupt file"。