360教程:是一个免费提供流行杀毒软件教程、在线学习分享的学习平台!

运用C#编写的一个定时关机程序

时间:2024/7/17作者:未知来源:360教程人气:

[摘要]TOKEN_QUERY, ref tokenHandle) == 0) throw new PrivilegeException(FormatError(Marshal.GetLastWin3...
TOKEN_QUERY, ref tokenHandle) == 0)
    throw new PrivilegeException(FormatError(Marshal.GetLastWin32Error()));
   if (LookupPrivilegeValue("", privilege, ref privilegeLUID) == 0)
    throw new PrivilegeException(FormatError(Marshal.GetLastWin32Error()));
   tokenPrivileges.PrivilegeCount = 1;
   tokenPrivileges.Privileges.Attributes = SE_PRIVILEGE_ENABLED;
   tokenPrivileges.Privileges.pLuid = privilegeLUID;
   int size = 4;
   if (AdjustTokenPrivileges(tokenHandle, 0, ref tokenPrivileges, 4 + (12 * tokenPrivileges.PrivilegeCount), ref newPrivileges, ref size) == 0)
    throw new PrivilegeException(FormatError(Marshal.GetLastWin32Error()));
  }
  /// <summary>
  /// Suspends or hibernates the system.
  /// </summary>
  /// <param name="hibernate">True if the system has to hibernate, false if the system has to be suspended.</param>
  /// <param name="force">True if the exit has to be forced, false otherwise.</param>
  /// <exception cref="PlatformNotSupportedException">The requested exit method is not supported on this platform.</exception>
  protected static void SuspendSystem(bool hibernate , bool force  ){
   if (!CheckEntryPoint("powrprof.dll", "SetSuspendState"))
    throw new PlatformNotSupportedException("The SetSuspendState method is not supported on this system!");
   SetSuspendState((int)(hibernate ? 1 : 0), (int)(force ? 1 : 0), 0);
  }
  /// <summary>
  /// Checks whether a specified method exists on the local computer.
  /// </summary>
  /// <param name="library">The library that holds the method.</param>
  /// <param name="method">The entry point of the requested method.</param>
  /// <returns>True if the specified method is present, false otherwise.</returns>
  protected static bool CheckEntryPoint(string library , string method ) {
   IntPtr  libPtr = LoadLibrary(library);
   if (!libPtr.Equals(IntPtr.Zero)) {
    if (!GetProcAddress(libPtr, method).Equals(IntPtr.Zero)) {
     FreeLibrary(libPtr);
     return true;
    }
    FreeLibrary(libPtr);
   }
   return false;
  }
  /// <summary>
  /// Formats an error number into an error message.
  /// </summary>
  /// <param name="number">The error number to convert.</param>
  /// <returns>A string representation of the specified error number.</returns>
  protected static string FormatError(int number ) {
   StringBuilder buffer =new StringBuilder(255);
   FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, IntPtr.Zero, number, 0, buffer, buffer.Capacity, 0);
   return buffer.ToString();
  }
}
/// <summary>
/// The exception that is thrown when an error occures when requesting a specific privilege.
/// </summary>
public class PrivilegeException : Exception {
  /// <summary>
  /// Initializes a new instance of the PrivilegeException class.
  /// </summary>
  public PrivilegeException () : base() {}
  /// <summary>
  /// Initializes a new instance of the PrivilegeException class with a specified error message.
  /// </summary>
  /// <param name="message">The message that describes the error.</param>
  public PrivilegeException (string message ) :base(message) {}
}
}

关键词:运用C#编写的一个定时关机程序




Copyright © 2012-2018 360教程(http://www.360sd.cn) .All Rights Reserved 网站地图 友情链接

免责声明:本站资源均来自互联网收集 如有侵犯到您利益的地方请及时联系管理删除,敬请见谅!

QQ:1006262270   邮箱:kfyvi376850063@126.com   手机版