Windows是一个强大的操作系统,也会向开发者提供海量的系统API来帮助开发者来完成Windows系统软件的开发工作。

整理的部分Windows API,C#可以直接调用。

1.获取.exe应用程序的图标

 [DllImport("shell32.DLL", EntryPoint = "ExtractAssociatedIcon")]
private static extern int ExtractAssociatedIconA(int hInst, string lpIconPath, ref int lpiIcon); //声明函数
System.IntPtr thisHandle;
public System.Drawing.Icon GetIco(string filePath)//filePath是要获取文件路径,返回ico格式文件
{
int RefInt = ;
thisHandle = new IntPtr(ExtractAssociatedIconA(, filePath, ref RefInt));
return System.Drawing.Icon.FromHandle(thisHandle);
}

2.获取硬盘信息

public string GetComputorInformation()
{ StringBuilder mStringBuilder = new StringBuilder();
DriveInfo[] myAllDrivers = DriveInfo.GetDrives();
try
{
foreach (DriveInfo myDrive in myAllDrivers)
{
if (myDrive.IsReady)
{
mStringBuilder.Append("磁盘驱动器盘符:");
mStringBuilder.AppendLine(myDrive.Name);
mStringBuilder.Append("磁盘卷标:");
mStringBuilder.AppendLine(myDrive.VolumeLabel);
mStringBuilder.Append("磁盘类型:");
mStringBuilder.AppendLine(myDrive.DriveType.ToString());
mStringBuilder.Append("磁盘格式:");
mStringBuilder.AppendLine(myDrive.DriveFormat);
mStringBuilder.Append("磁盘大小:");
decimal resultmyDrive = Math.Round((decimal)myDrive.TotalSize / / / , );
mStringBuilder.AppendLine(resultmyDrive "GB");
mStringBuilder.Append("剩余空间:");
decimal resultAvailableFreeSpace = Math.Round((decimal)myDrive.AvailableFreeSpace / / / , );
mStringBuilder.AppendLine(resultAvailableFreeSpace "GB");
mStringBuilder.Append("总剩余空间(含磁盘配额):");
decimal resultTotalFreeSpace = Math.Round((decimal)myDrive.TotalFreeSpace / / / , );
mStringBuilder.AppendLine(resultTotalFreeSpace "GB");
mStringBuilder.AppendLine("-------------------------------------");
}
} }
catch (Exception ex)
{
throw ex;
} return mStringBuilder.ToString();
}

3.开机启动程序

//获取注册表中的启动位置
RegistryKey RKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
///<summary>/// 设置开机启动
///</summary>///<param name="path"/>public void StartRunApp(string path)
{
string strnewName = path.Substring(path.LastIndexOf("\\") );//要写入注册表的键值名称
if (!File.Exists(path))//判断指定的文件是否存在
return;
if (RKey == null)
{
RKey = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
}
RKey.SetValue(strnewName, path);//通过修改注册表,使程序在开机时自动运行
}
///<summary>/// 取消开机启动
///</summary>///<param name="path"/>public void ForbitStartRun(string path)
{
string strnewName = path.Substring(path.LastIndexOf("\\") );//要写入注册表的键值名称
RKey.DeleteValue(strnewName, false);//通过修改注册表,取消程序在开机时自动运行
}

4.系统热键操作

[DllImport("user32.dll")] //声明api函数
public static extern bool RegisterHotKey(
IntPtr hwnd, // 窗口句柄
int id, // 热键ID
uint fsmodifiers, // 热键修改选项
Keys vk // 热键
);
[DllImport("user32.dll")] //声明api函数
public static extern bool UnregisterHotKey(
IntPtr hwnd, // 窗口句柄
int id // 热键ID
);
public enum keymodifiers //组合键枚举
{
none = ,
alt = ,
control = ,
shift = ,
windows =
}
private void processhotkey(Message m) //按下设定的键时调用该函数
{
IntPtr id = m.WParam; //intptr用于表示指针或句柄的平台特定类型
//messagebox.show(id.tostring());
string sid = id.ToString();
switch (sid)
{
case "": break;
case "": break;
}
}
///<summary>/// 注册热键
///</summary>public void RegisterHotkey(IntPtr handle, int hotkeyID, uint fsmodifiers, Keys mKeys)
{
RegisterHotKey(handle, hotkeyID, fsmodifiers, mKeys);
} ///<summary>/// 卸载热键
///</summary>///<param name="handle"/>///<param name="hotkeyID"/>public void UnregisterHotkey(IntPtr handle, int hotkeyID)
{
UnregisterHotKey(handle, hotkeyID);
}

5.系统进程操作

public class GetProcess
{
bool isSuccess = false;
[DllImport("kernel32")]
public static extern void GetWindowsDirectory(StringBuilder WinDir, int count);
[DllImport("kernel32")]
public static extern void GetSystemDirectory(StringBuilder SysDir, int count);
[DllImport("kernel32")]
public static extern void GetSystemInfo(ref CPU_INFO cpuinfo);
[DllImport("kernel32")]
public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);
[DllImport("kernel32")]
public static extern void GetSystemTime(ref SYSTEMTIME_INFO stinfo); //定义CPU的信息结构
[StructLayout(LayoutKind.Sequential)]
public struct CPU_INFO
{
public uint dwOemId;
public uint dwPageSize;
public uint lpMinimumApplicationAddress;
public uint lpMaximumApplicationAddress;
public uint dwActiveProcessorMask;
public uint dwNumberOfProcessors;
public uint dwProcessorType;
public uint dwAllocationGranularity;
public uint dwProcessorLevel;
public uint dwProcessorRevision;
} //定义内存的信息结构
[StructLayout(LayoutKind.Sequential)]
public struct MEMORY_INFO
{
public uint dwLength;
public uint dwMemoryLoad;
public uint dwTotalPhys;
public uint dwAvailPhys;
public uint dwTotalPageFile;
public uint dwAvailPageFile;
public uint dwTotalVirtual;
public uint dwAvailVirtual;
} //定义系统时间的信息结构
[StructLayout(LayoutKind.Sequential)]
public struct SYSTEMTIME_INFO
{
public ushort wYear;
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort wHour;
public ushort wMinute;
public ushort wSecond;
public ushort wMilliseconds;
} public string GetSystemInformation()
{
MEMORY_INFO MemInfo = new MEMORY_INFO();
GlobalMemoryStatus(ref MemInfo);
return MemInfo.dwMemoryLoad.ToString();
} public string GetSystemCup()
{
CPU_INFO CpuInfo = new CPU_INFO();
GetSystemInfo(ref CpuInfo);
return CpuInfo.dwProcessorType.ToString();
} ///<summary>/// 获取当前所有进程
///</summary>///<returns></returns>public DataTable GetAllProcess()
{
DataTable mDataTable = new DataTable();
mDataTable.Rows.Clear();
mDataTable.Columns.Add("ProcessID");
mDataTable.Columns.Add("ProcessName");
mDataTable.Columns.Add("Memory");
mDataTable.Columns.Add("StartTime");
mDataTable.Columns.Add("FileName");
mDataTable.Columns.Add("ThreadNumber"); Process[] myProcess = Process.GetProcesses();
foreach (Process p in myProcess)
{
DataRow mDataRow = mDataTable.NewRow();
mDataRow[] = p.Id;
mDataRow[] = p.ProcessName;
mDataRow[] = string.Format("{0:###,##0.00}KB", p.PrivateMemorySize64 / );
//有些进程无法获取启动时间和文件名信息,所以要用try/catch;
try
{
mDataRow[] = string.Format("{0}", p.StartTime);
mDataRow[] = p.MainModule.FileName;
mDataRow[] = p.Threads.Count; }
catch
{
mDataRow[] = "";
mDataRow[] = ""; }
mDataTable.Rows.Add(mDataRow);
}
return mDataTable;
} ///<summary>/// 结束进程
///</summary>///<param name="processName"/>///<returns></returns>public bool KillProcess(string processName)
{
try
{
System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName(processName);
foreach (System.Diagnostics.Process p in process)
{
p.Kill();
}
}
catch
{
isSuccess = false;
}
return isSuccess;
}
}

6.改变窗口

public const int SE_SHUTDOWN_PRIVILEGE = 0x13;  

       [DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll")]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); [DllImport("user32.dll")]
public static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx,
int cy, uint uFlags);

C# WindowsAPI的更多相关文章

  1. WindowsAPI调用和OCR图片识别

    傻了吧唧的装双系统.成功的干崩了原本的系统.现在重装VS.闲的没事胡扯几句. WindowsAPI在每一台Windows系统上开放标准API供开发人员调用.功能齐全.在这里只介绍三个部分. 1.利用A ...

  2. [原创]C#应用WindowsApi实现查找(FindWindowEx)文本框(TextBox、TextEdit)。

    /// <summary> /// 获取文本框控件 /// </summary> /// <param name="hwnd">文本框所在父窗口 ...

  3. [原创]C#应用WindowsApi实现查找\枚举(FindWindow、EnumChildWindows)窗体控件,并发送消息。

    首先介绍基本WindowsApi: public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 函 ...

  4. 多媒体(3):基于WindowsAPI的视频捕捉卡操作

    目录 多媒体(1):MCI接口编程 多媒体(2):WAVE文件格式分析 多媒体(3):基于WindowsAPI的视频捕捉卡操作 多媒体(4):JPEG图像压缩编码 多媒体(3):基于WindowsAP ...

  5. Windows服务启动进程----Cjwdev.WindowsApi.dll

    windows服务下无法启动外部程序 做一个windows服务监听服务,涉及到windows服务启动外部程序的一个过程,但是调试测试发现,无法简单的用process.start()这种方法, 原因是在 ...

  6. WindowsAPI每日一练(2) 使用应用程序句柄

    WindowsAPI每日一练系列 :https://www.cnblogs.com/LexMoon/category/1246238.html WindowsAPI每日一练() WinMain Win ...

  7. WindowsAPI每日一练(1) MessageBoxA

    WindowsAPI每日一练系列 :https://www.cnblogs.com/LexMoon/category/1246238.html WindowsAPI每日一练(1) WinMain 要跟 ...

  8. 使用WindowsAPI播放PCM音频

    这一篇文章同上一篇<使用WindowsAPI获取录音音频>原理具有相似之处,不再详细介绍函数与结构体的参数 1. waveOutGetNumDevs 2. waveOutGetDevCap ...

  9. 使用WindowsAPI实现播放PCM音频的方法

    这篇文章主要介绍了使用WindowsAPI实现播放PCM音频的方法,很实用的一个功能,需要的朋友可以参考下 本文介绍了使用WindowsAPI实现播放PCM音频的方法,同前面一篇使用WindowsAP ...

  10. 使用WindowsAPI获取录音音频的方法

    这篇文章主要介绍了使用WindowsAPI获取录音音频的方法,非常实用的功能,需要的朋友可以参考下 本文实例介绍了使用winmm.h进行音频流的获取的方法,具体步骤如下: 一.首先需要包含以下引用对象 ...

随机推荐

  1. 2016 提高组c++ 错题

    需重做 树的重心 链表 计算机基础知识 无线通讯技术: 蓝牙,wifi,GPRS 现在常用的无线通信技术:FM调频广播(用于收音机): 2G.3G移动通信技术(中国移动.中国联通.中国电信正在运营的网 ...

  2. 【转】Docker基础

    一.简介 Docker是一个开源的应用容器引擎,使用Go语言开发,基于Linux内核的CGroup.Namespace.Union FS等技术实现的一种系统级虚拟化技术. 特性 更高效的利用系统资源: ...

  3. 解决VS不能智能提示

    前一段时间在电脑上装了VS2013,导致VS2010上不能正常进行单元测试,折腾了一番,把VS2013又给卸载了,结果发现VS2010的智能提示没有了,没有就没有吧,懒的去管,就一直用 Ctrl + ...

  4. inotify-tools+rsync实时同步文件安装和配置

    服务器A:论坛的主服务器,运行DZ X2论坛程序;服务器B:论坛从服务器,需要把X2的图片附件和MySQL数据实时从A主服务器实时同步到B服务器.MySQL同步设置会在下一编中说到.以下是用于实时同步 ...

  5. vue项目中遇到的打印,以及处理重新排版后不显示echarts图片问题。

    1. 项目中用到的打印 页面: css: 控制好宽度一般A4 我调试的是794px多了放不下,小了填不满.当时多页打印的时候,一定要控制好每一个页面内容显示的高度不要超过一个页面,当然根据自己项目来. ...

  6. JavaScript 获取某个字符的 Unicode 码

    function getUnicode (charCode) { return charCode.charCodeAt(0).toString(16); } 获取的是 UTF-16 编码的值,不足4位 ...

  7. ELK搭建和部署-----(上半部分)

    本实验基于centos7安装部署操作步骤如下: 1.首先准备两台centos7系统,IP地址自行定义. 2.先在服务器上安装时间同步中间件为chronyc 3.并启动命令为systemctl star ...

  8. python中方法与函数的区别与联系

    今天huskiesir在对列表进行操作的时候,用到了sorted()函数,偶然情况下在菜鸟教程上看到了内置方法sort,同样都可以实现我对列表的排序操作,那么方法和函数有什么区别和联系呢? 如下是我个 ...

  9. 小学生绞尽脑汁也学不会的python(反射)

    小学生绞尽脑汁也学不会的python(反射) 1. issubclass, type, isinstance issubclass 判断xxxx类是否是xxxx类的子类 type 给出xxx的数据类型 ...

  10. windows关于定时执行的php脚本

    根据业务需求,需要服务器每天定时执行一些脚本,如后台提交数据,定时处理数据库等. 最初的思路是在某个控制器里写好方法,加入code验证,定期的用计划任务去访问.由于window计划任务这方面比较low ...