自动关机程序

(一).程序简单示例图

如果您感兴趣的话,还可以继续增加好多功能,比如:重启机器/待机/睡眠等状态,还有倒计时功能

(二).代码

           

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace ShutDownComputer
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.button1 = new System.Windows.Forms.Button();
   this.label1 = new System.Windows.Forms.Label();
   this.comboBox1 = new System.Windows.Forms.ComboBox();
   this.label2 = new System.Windows.Forms.Label();
   this.label3 = new System.Windows.Forms.Label();
   this.timer1 = new System.Timers.Timer();
   ((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
   this.SuspendLayout();
   //
   // button1
   //
   this.button1.BackColor = System.Drawing.Color.Olive;
   this.button1.Location = new System.Drawing.Point(88, 184);
   this.button1.Name = "button1";
   this.button1.TabIndex = 0;
   this.button1.Text = "开始";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(24, 80);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(120, 23);
   this.label1.TabIndex = 2;
   this.label1.Text = "您想让你的计算机在";
   //
   // comboBox1
   //
   this.comboBox1.Items.AddRange(new object[] {
                 "10",
                 "20",
                 "30",
                 "40",
                 "50",
                 "60",
                 "70",
                 "80",
                 "90",
                 "100",
                 "110",
                 "120"});
   this.comboBox1.Location = new System.Drawing.Point(64, 104);
   this.comboBox1.Name = "comboBox1";
   this.comboBox1.Size = new System.Drawing.Size(88, 20);
   this.comboBox1.TabIndex = 3;
   this.comboBox1.Text = "30";
   //
   // label2
   //
   this.label2.Location = new System.Drawing.Point(152, 112);
   this.label2.Name = "label2";
   this.label2.Size = new System.Drawing.Size(104, 16);
   this.label2.TabIndex = 4;
   this.label2.Text = "分之后自动关机";
   //
   // label3
   //
   this.label3.Location = new System.Drawing.Point(32, 32);
   this.label3.Name = "label3";
   this.label3.TabIndex = 5;
   //
   // timer1
   //
   this.timer1.Interval = 1000;
   this.timer1.SynchronizingObject = this;
   this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(192)), ((System.Byte)(255)));
   this.ClientSize = new System.Drawing.Size(280, 238);
   this.Controls.Add(this.label3);
   this.Controls.Add(this.label2);
   this.Controls.Add(this.comboBox1);
   this.Controls.Add(this.label1);
   this.Controls.Add(this.button1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.Load += new System.EventHandler(this.Form1_Load);
   ((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void Form1_Load(object sender, System.EventArgs e)
  {
  
  }


  [StructLayout(LayoutKind.Sequential, Pack=1)]

  internal struct TokPriv1Luid
  {
   public int Count;

   public long Luid;

   public int Attr;

  }

  [DllImport("kernel32.dll", ExactSpelling=true) ]

  internal static extern IntPtr GetCurrentProcess();

  [DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]

  internal static extern bool OpenProcessToken( IntPtr h, int acc, ref IntPtr phtok );

  [DllImport("advapi32.dll", SetLastError=true) ]

  internal static extern bool LookupPrivilegeValue( string host, string name, ref long pluid );

  [DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]

  internal static extern bool AdjustTokenPrivileges( IntPtr htok, bool disall,

   ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen );

  [DllImport("user32.dll", ExactSpelling=true, SetLastError=true) ]

  internal static extern bool ExitWindowsEx( int flg, int rea );

  internal const int SE_PRIVILEGE_ENABLED = 0x00000002;

  internal const int TOKEN_QUERY = 0x00000008;

  internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;

  internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";

  internal const int EWX_LOGOFF = 0x00000000;

  internal const int EWX_SHUTDOWN = 0x00000001;

  internal const int EWX_REBOOT = 0x00000002;

  internal const int EWX_FORCE = 0x00000004;

  internal const int EWX_POWEROFF = 0x00000008;
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.ComboBox comboBox1;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.Label label3;
  private System.Timers.Timer timer1;

  internal const int EWX_FORCEIFHUNG = 0x00000010;

 

  private void DoExitWin( int flg )

  {

   bool ok;

   TokPriv1Luid tp;

   IntPtr hproc = GetCurrentProcess();

   IntPtr htok = IntPtr.Zero;

   ok = OpenProcessToken( hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok );

   tp.Count = 1;

   tp.Luid = 0;

   tp.Attr = SE_PRIVILEGE_ENABLED;

   ok = LookupPrivilegeValue( null, SE_SHUTDOWN_NAME, ref tp.Luid );

   ok = AdjustTokenPrivileges( htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero );

   ok = ExitWindowsEx( flg, 0 );

  }


  DateTime ShutDownItTime = new DateTime();
  int status = 0;  //0: 表示还没有开始计时关机;   1: 已经开始计时关机
  private void button1_Click(object sender, System.EventArgs e)
  {   
   if(status == 1)   
   {
    this.timer1.Enabled = false;
    status = 0;
    this.button1.Text = "开始";
    return;
   }   
   int intTime = 0;
   if(this.comboBox1.Text.Length == 0)
   {
    this.label3.Text = "请输入时间!";
    return;
   }
   try
   {
    intTime = int.Parse(this.comboBox1.Text);
   }
   catch
   {
    this.label3.Text = "时间只能为整数,请重新输入!";
   }
   this.ShutDownItTime=DateTime.Now.AddMinutes(intTime);
   this.timer1.Enabled = true;
   status = 1;
   this.button1.Text = "停止";
  }

  
  private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  {
   if(DateTime.Now >= this.ShutDownItTime)
   {
    this.label3.Text = "正在关机...";
    DoExitWin(EWX_SHUTDOWN);
   } 
   
  }
 }
}

 
在使用Python来安装geopandas包时,由于geopandas依赖于几个其他的Python库(如GDAL, Fiona, Pyproj, Shapely等),因此安装过程可能需要一些额外的步骤。以下是一个基本的安装指南,适用于大多数用户: 使用pip安装 确保Python和pip已安装: 首先,确保你的计算机上已安装了Python和pip。pip是Python的包管理工具,用于安装和管理Python包。 安装依赖库: 由于geopandas依赖于GDAL, Fiona, Pyproj, Shapely等库,你可能需要先安装这些库。通常,你可以通过pip直接安装这些库,但有时候可能需要从其他源下载预编译的二进制包(wheel文件),特别是GDAL和Fiona,因为它们可能包含一些系统级的依赖。 bash pip install GDAL Fiona Pyproj Shapely 注意:在某些系统上,直接使用pip安装GDAL和Fiona可能会遇到问题,因为它们需要编译一些C/C++代码。如果遇到问题,你可以考虑使用conda(一个Python包、依赖和环境管理器)来安装这些库,或者从Unofficial Windows Binaries for Python Extension Packages这样的网站下载预编译的wheel文件。 安装geopandas: 在安装了所有依赖库之后,你可以使用pip来安装geopandas。 bash pip install geopandas 使用conda安装 如果你正在使用conda作为你的Python包管理器,那么安装geopandas和它的依赖可能会更简单一些。 创建一个新的conda环境(可选,但推荐): bash conda create -n geoenv python=3.x anaconda conda activate geoenv 其中3.x是你希望使用的Python版本。 安装geopandas: 使用conda-forge频道来安装geopandas,因为它提供了许多地理空间相关的包。 bash conda install -c conda-forge geopandas 这条命令会自动安装geopandas及其所有依赖。 注意事项 如果你在安装过程中遇到任何问题,比如编译错误或依赖问题,请检查你的Python版本和pip/conda的版本是否是最新的,或者尝试在不同的环境中安装。 某些库(如GDAL)可能需要额外的系统级依赖,如地理空间库(如PROJ和GEOS)。这些依赖可能需要单独安装,具体取决于你的操作系统。 如果你在Windows上遇到问题,并且pip安装失败,尝试从Unofficial Windows Binaries for Python Extension Packages网站下载相应的wheel文件,并使用pip进行安装。 脚本示例 虽然你的问题主要是关于如何安装geopandas,但如果你想要一个Python脚本来重命名文件夹下的文件,在原始名字面加上字符串"geopandas",以下是一个简单的示例: python import os # 指定文件夹路径 folder_path = 'path/to/your/folder' # 遍历文件夹中的文件 for filename in os.listdir(folder_path): # 构造原始文件路径 old_file_path = os.path.join(folder_path, filename) # 构造新文件名 new_filename = 'geopandas_' + filename # 构造新文件路径 new_file_path = os.path.join(folder_path, new_filename) # 重命名文件 os.rename(old_file_path, new_file_path) print(f'Renamed "{filename}" to "{new_filename}"') 请确保将'path/to/your/folder'替换为你想要重命名文件的实际文件夹路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值