1.1 判断本机是否联网
if(SystemInformation.Network){ //联网状态}else{ //未联网状态}
1.2 获取特殊文件路径
1.2.1 获取Program Files路径
string FilePath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
1.2.2 获取桌面目录路径
string FilePath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
1.2.3 获取开始菜单路径
string Filepath = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu);
1.2.4 获取用户程序组路径
string Filepath = Environment.GetFolderPath(Environment.SpecialFolder.Programs);
1.2.5 获取文档模板路径
string Filepath = Environment.GetFolderPath(Environment.SpecialFolder.Templates);
1.2.6 获取收藏夹路径
string Filepath = Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
1.2.7 获取共享组件路径
string Filepath = Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles);
1.2.8 获取我的图片路径
string Filepath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
1.2.9 获取Internet历史记录路径
string Filepath = Environment.GetFolderPath(Environment.SpecialFolder.History);
1.2.10 获取Internet临时文件路径
string Filepath = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
1.3 剪切板内容处理
//设置剪切板内容Clipboard.SetText("将此字符串置于剪切板中");//获得剪切板内容string ClipBoardMsg = Clipboard.GetText();
1.4 获取本机内网IP
string str = "";//获取IP地址列表System.Net.IPAddress[] addressList = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName()).AddressList;//遍历IP地址列表for (int i = 0; i < addressList.Length; i++){ //获得遍历到的IP地址 str+= addressList[i].ToString() + "\n";}//str就是本机内网ip
1.5 获取本机公网IP
/// /// 获得本机公网IP/// /// 获取不到则返回空串 private static string GetIP(){ string tempip = ""; try { WebRequest wr = WebRequest.Create("http://www.ip138.com/ips138.asp"); Stream s = wr.GetResponse().GetResponseStream(); StreamReader sr = new StreamReader(s, Encoding.Default); string all = sr.ReadToEnd(); //读取网站的数据 int start = all.IndexOf("您的IP地址是:[") + 9; int end = all.IndexOf("]", start); tempip = all.Substring(start, end - start); sr.Close(); s.Close(); } catch { } return tempip;}
1.6 更改屏幕分辨率
[DllImport("user32.dll", CharSet = CharSet.Auto)]static extern int ChangeDisplaySettings([In] ref DEVMODE lpDevMode, int dwFlags);public enum DMDO{ DEFAULT = 0, D90 = 1, D180 = 2, D270 = 3}[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]struct DEVMODE{ public const int DM_DISPLAYFREQUENCY = 0x400000; public const int DM_PELSWIDTH = 0x80000; public const int DM_PELSHEIGHT = 0x100000; public const int DM_BITSPERPEL = 262144; private const int CCHDEVICENAME = 32; private const int CCHFORMNAME = 32; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)] public string dmDeviceName; public short dmSpecVersion; public short dmDriverVersion; public short dmSize; public short dmDriverExtra; public int dmFields; public int dmPositionX; public int dmPositionY; public DMDO dmDisplayOrientation; public int dmDisplayFixedOutput; public short dmColor; public short dmDuplex; public short dmYResolution; public short dmTTOption; public short dmCollate; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHFORMNAME)] public string dmFormName; public short dmLogPixels; public int dmBitsPerPel; public int dmPelsWidth; public int dmPelsHeight; public int dmDisplayFlags; public int dmDisplayFrequency; public int dmICMMethod; public int dmICMIntent; public int dmMediaType; public int dmDitherType; public int dmReserved1; public int dmReserved2; public int dmPanningWidth; public int dmPanningHeight;}//****************************调用***************************long RetVal = 0;DEVMODE dm = new DEVMODE();dm.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));dm.dmPelsWidth = 1024;//宽dm.dmPelsHeight = 768;//高dm.dmDisplayFrequency = 60;//刷新率dm.dmFields = DEVMODE.DM_PELSWIDTH | DEVMODE.DM_PELSHEIGHT | DEVMODE.DM_DISPLAYFREQUENCY | DEVMODE.DM_BITSPERPEL;RetVal = ChangeDisplaySettings(ref dm, 0);
1.7 机器码相关
/// /// 获取CPU序列号/// /// CPU序列号字符串 private static string GetCpuInfo(){ try { string cpuInfo = ""; ManagementClass cimobject = new ManagementClass("Win32_Processor"); ManagementObjectCollection moc = cimobject.GetInstances(); foreach (ManagementObject mo in moc) { cpuInfo += mo.Properties["ProcessorId"].Value.ToString(); } return cpuInfo.ToString(); } catch { return "unknown"; }}/// /// 获取硬盘ID/// /// string private static string GetHardDiskID(){ try { ManagementClass mcHD = new ManagementClass("win32_logicaldisk"); ManagementObjectCollection mocHD = mcHD.GetInstances(); foreach (ManagementObject m in mocHD) { if (m["DeviceID"].ToString() == "C:") { return m["VolumeSerialNumber"].ToString(); } } return ""; } catch { return "unknown"; }}/// /// 获取网卡的物理地址/// /// private static string GetMacAddress(){ try { string mac = ""; ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if ((bool)mo["IPEnabled"] == true) { mac += mo["MacAddress"].ToString(); } } moc = null; mc = null; return mac; } catch { return "unknown"; }}/// /// 获取网卡的IP地址/// /// private static string GetIPAddress(){ try { //获取IP地址 string st = ""; ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if ((bool)mo["IPEnabled"] == true) { System.Array ar; ar = (System.Array)(mo.Properties["IpAddress"].Value); st += ar.GetValue(0).ToString(); } } moc = null; mc = null; return st; } catch { return "unknow"; } finally { }}
1.8 注销、关机、重启
[DllImport("user32.dll", EntryPoint = "ExitWindowsEx", CharSet = CharSet.Ansi)]private static extern int ExitWindowsEx(int uFlags, int dwReserved);//注销计算机ExitWindowsEx(0, 0);//关机System.Diagnostics.Process myProcess = new System.Diagnostics.Process();myProcess.StartInfo.FileName = "cmd.exe";//启动cmd命令myProcess.StartInfo.UseShellExecute = false;//是否使用系统外壳程序启动进程myProcess.StartInfo.RedirectStandardInput = true;//是否从流中读取myProcess.StartInfo.RedirectStandardOutput = true;//是否写入流myProcess.StartInfo.RedirectStandardError = true;//是否将错误信息写入流myProcess.StartInfo.CreateNoWindow = true;//是否在新窗口中启动进程myProcess.Start();//启动进程myProcess.StandardInput.WriteLine("shutdown -s -t 0");//执行关机命令//重启System.Diagnostics.Process myProcess = new System.Diagnostics.Process();myProcess.StartInfo.FileName = "cmd.exe";//启动cmd命令myProcess.StartInfo.UseShellExecute = false;//是否使用系统外壳程序启动进程myProcess.StartInfo.RedirectStandardInput = true;//是否从流中读取myProcess.StartInfo.RedirectStandardOutput = true;//是否写入流myProcess.StartInfo.RedirectStandardError = true;//是否将错误信息写入流myProcess.StartInfo.CreateNoWindow = true;//是否在新窗口中启动进程myProcess.Start();//启动进程myProcess.StandardInput.WriteLine("shutdown -r -t 0");//执行重启计算机命令
1.9 获取内存使用情况
//需要添加引用 Microsoft.VisualBasicusing Microsoft.VisualBasic.Devices;//获得内存使用情况Computer myComputer = new Computer();//获取系统的物理内存总量string MemoryMsg = Convert.ToString(myComputer.Info.TotalPhysicalMemory / 1024 / 1024);//获取系统的可用物理内存string MemoryMsg = Convert.ToString(myComputer.Info.AvailablePhysicalMemory / 1024 / 1024);//获取系统的虚拟内存总量string MemoryMsg = Convert.ToString(myComputer.Info.TotalVirtualMemory / 1024 / 1024);//获取系统的可用虚拟内存string MemoryMsg = Convert.ToString(myComputer.Info.AvailableVirtualMemory / 1024 / 1024);
1.10 获取当前屏幕分辨率
//宽int ScreenWidth = SystemInformation.VirtualScreen.Width;//高int ScreenHeight = SystemInformation.VirtualScreen.Height;
1.11 获取当前程序运行目录
string AppPath = Environment.CurrentDirectory;
1.12获取磁盘空间使用情况
try{ System.IO.DriveInfo[] drive = System.IO.DriveInfo.GetDrives();//获取所有驱动器 for (int i = 0; i < drive.Length; i++)//遍历驱动器 { Console.WriteLine(drive[i].Name + ": 总空间:" + drive[i].TotalSize / 1024 / 1024 / 1024 + "G");//显示总空间 Console.WriteLine(drive[i].Name + ": 剩余空间:" + drive[i].TotalFreeSpace / 1024 / 1024 / 1024 + "G");//显示剩余空间 Console.WriteLine(drive[i].Name + ": 已用空间:" + (drive[i].TotalSize - drive[i].TotalFreeSpace) / 1024 / 1024 / 1024 + "G");//显示已用空间 }}catch { }
1.13 获取系统启动后持续运行的时间
string TickCont = (Environment.TickCount / 1000).ToString() + "秒";
1.14 获取计算机名称
string MechineName = Environment.MachineName;
1.14 操作蜂鸣器
protected enum Tone//枚举 五线谱{ REST = 0, A = 220, B = 247, C = 262, D = 294, E = 330, F = 349, G = 392,}protected enum Duration//枚举 发音时间{ WHOLE = 1200, HALF = WHOLE / 2, QUARTER = HALF / 2, EIGHTH = QUARTER / 2, SIXTEENTH = EIGHTH / 2,}protected struct Note{ Tone toneVal; //初始化一个Tone对象 Duration durVal; //初始化一个Duration对象 public Note(Tone frequency, Duration time) //定义一个Note方法 { toneVal = frequency; //为变量toneVal赋值 durVal = time; //为变量durVal赋值 } public Tone NoteTone { get { return toneVal; //定义一个Tone类型的属性 } set { toneVal = value; } } public Duration NoteDuration { get { return durVal; //定义一个Duration类型的属性 } set { durVal = value; } }}protected void Play(Note tune){ if (tune.NoteTone == Tone.REST) //当没有选择RadioButton按钮时 Thread.Sleep((int)tune.NoteDuration); //将当前线程挂起指定的时间 else //当选定某一个RadioButton按钮时 //通过控制台扬声器播放具有指定频率和持续时间的声音 Console.Beep((int)tune.NoteTone, (int)tune.NoteDuration);}/// /// 发音方法/// /// 1 - 7 音/// 声调 1长音 2中长音 3中音 4短长音 5短音private void PlayMic(int a, int b){ Note note = new Note(); switch (a) { case 1: note.NoteTone = Tone.A; break; case 2: note.NoteTone = Tone.B; break; case 3: note.NoteTone = Tone.C; break; case 4: note.NoteTone = Tone.D; break; case 5: note.NoteTone = Tone.E; break; case 6: note.NoteTone = Tone.F; break; case 7: note.NoteTone = Tone.G; break; default: break; } switch (b) { case 1: note.NoteDuration = Duration.WHOLE; break; case 2: note.NoteDuration = Duration.HALF; break; case 3: note.NoteDuration = Duration.QUARTER; break; case 4: note.NoteDuration = Duration.EIGHTH; break; case 5: note.NoteDuration = Duration.SIXTEENTH; break; } Play(note);}//****************************调用***************************PlayMic(1, 1);PlayMic(2, 1);PlayMic(3, 1);PlayMic(4, 1);PlayMic(5, 1);PlayMic(6, 1);PlayMic(7, 1);