API函数GetDevicePowerState用C#如何调用或是用方法

luohuayouy 2013-04-17 05:05:09
根据客户要求要做一个关于电源策略的设置。所以想请教各位大神API函数GetDevicePowerState用C#如何调用,最好是能告诉我具体方法有个实例参考。求指教.........
...全文
133 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
luohuayouy 2013-04-19
  • 打赏
  • 举报
回复
话说如果我要通过API函数去设置当前“电源计划”-->“高级设置”-->“多媒体设置”,用那个函数呢?我找了很久都没有找到一个可以用的?
gxingmin 2013-04-17
  • 打赏
  • 举报
回复
API函数不需要引用,是用DllImport导入的声明的
[DllImport("Kernel32.DLL", CharSet = CharSet.Auto,
     SetLastError = true)]
  private extern static
    bool GetSystemPowerStatus(
        SYSTEM_POWER_STATUS SystemPowerStatus);
给你一个参考网址吧MSDN上的,有代码 http://msdn.microsoft.com/en-us/library/ms704147(VS.85).aspx
luohuayouy 2013-04-17
  • 打赏
  • 举报
回复
引用 1 楼 gxingmin 的回复:
C# code?12345678910111213141516171819202122232425262728293031// Get the power state of a harddiskstring ReportDiskStatus(){ string status = string.Empty; bool fOn = false; Assem……
能否跟我解释一下 Assembly assembly = Assembly.GetExecutingAssembly(); FileStream[] files = assembly.GetFiles(); if (files.Length > 0) { IntPtr hFile = files[0].Handle;这几句代码。还有你引用API函数GetDevicePowerState(hFile, out fOn)时,动态链接库你是如何引用的?
gxingmin 2013-04-17
  • 打赏
  • 举报
回复
// Get the power state of a harddisk
string ReportDiskStatus()
{
    string status = string.Empty;
    bool fOn = false;

    Assembly assembly = Assembly.GetExecutingAssembly();
    FileStream[] files = assembly.GetFiles();
    if (files.Length > 0)
    {
        IntPtr hFile = files[0].Handle;
        bool result = GetDevicePowerState(hFile, out fOn);
        if (result)
        {
            if (fOn)
            {
                status = "Disk is powered up and spinning";
            }
            else
            {
                status = "Disk is sleeping";
            }
        }
        else
        {
            status = "Cannot get Disk Status";
        }
        Console.WriteLine(status);
    }
    return status;
}

110,525

社区成员

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

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

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