首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用.net以编程方式启用/禁用LID

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

Stack Overflow用户
提问于 2014-07-28 13:56:56
回答 2查看 285关注 0票数 0

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

这是可能的吗?使用命令LIne可以改变这个设置,我需要把它转换成c#。谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-08-01 13:31:47

首先编写一个批处理文件:

代码语言:javascript
复制
    call powercfg –q >poweroptions.txt

根据管理员权限。第二种方法将批处理文件指定为模板:

代码语言:javascript
复制
    call Powercfg –SETACVALUEINDEX [powerschemeGUID] [putsubgroupGUID] [putpowersettingGUID] 000

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

代码语言:javascript
复制
    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返回一个错误“损坏的文件”。

票数 0
EN

Stack Overflow用户

发布于 2014-07-28 14:07:59

是的你可以。我没有确切的代码为它,但一些参考,这将肯定有助于您在视觉cpp。这里是盖子关闭时如何工作的博客,这个微软msdn也会让你清楚地了解它。与拥有所有电源管理相关事件、方法和常量列表的微软电源管理一起运行。根据你的需要使用。

下面是一些处理开始、关闭、登录、注销等系统事件的代码片段。

代码语言:javascript
复制
    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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24996898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档