C#常用代码集合(1)
引用自james li的博客,地址:http://www.cnblogs.com/JamesLi2015/p/3147986.html
1 读取操作系统和CLR的版本
OperatingSystem os = System.Environment.OSVersion;
Console.WriteLine("Platform: {0}", os.Platform);
Console.WriteLine("Service Pack: {0}", os.ServicePack);
Console.WriteLine("Version: {0}", os.Version);
Console.WriteLine("VersionString: {0}", os.VersionString);
Console.WriteLine("CLR Version: {0}", System.Environment.Version);
在我的Windows 7系统中,输出以下信息
Platform: Win32NT
Service Pack:
Version: 6.1.7600.0
VersionString: Microsoft Windows NT 6.1.7600.0
CLR Version: 4.0.21006.1
2 读取CPU数量,内存容量
可以通过Windows Management Instrumentation (WMI)提供的接口读取所需要的信息。
private static UInt32 CountPhysicalProcessors()
{
ManagementObjectSearcher objects = new ManagementObjectSearcher(
"SELECT * FROM Win32_ComputerSystem");
ManagementObjectCollection coll = objects.Get();
foreach(ManagementObject obj in coll)
{
return (UInt32)obj["NumberOfProcessors"];
}
return 0;
}
private static UInt64 CountPhysicalMemory()
{
ManagementObjectSearcher objects =new ManagementObjectSearcher(
"SELECT * FROM Win32_PhysicalMemory");
ManagementObjectCollection coll = objects.Get();
UInt64 total = 0;
foreach (ManagementObject obj in coll)
{
total += (UInt64)obj["Capacity"];
}
return total;
}
请添加对程序集System.Management的引用,确保代码可以正确编译。
Console.WriteLine("Machine: {0}", Environment.MachineName);
Console.WriteLine("# of processors (logical): {0}", Environment.ProcessorCount);
Console.WriteLine("# of processors (physical): {0}" CountPhysicalProcessors());
Console.WriteLine("RAM installed: {0:N0} bytes", CountPhysicalMemory());
Console.WriteLine("Is OS 64-bit? {0}", Environment.Is64BitOperatingSystem);
Console.WriteLine("Is process 64-bit? {0}", Environment.Is64BitProcess);
Console.WriteLine("Little-endian: {0}", BitConverter.IsLittleEndian);
foreach (Screen screen in System.Windows.Forms.Screen.AllScreens)
{
Console.WriteLine("Screen {0}", screen.DeviceName);
Console.WriteLine("\tPrimary {0}", screen.Primary);
Console.WriteLine("\tBounds: {0}", screen.Bounds);
Console.WriteLine("\tWorking Area: {0}",screen.WorkingArea);
Console.WriteLine("\tBitsPerPixel: {0}",screen.BitsPerPixel);
}
3 读取注册表键值对
using (RegistryKey keyRun = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run"))
{
foreach (string valueName in keyRun.GetValueNames())
{
Console.WriteLine("Name: {0}\tValue: {1}", valueName, keyRun.GetValue(valueName));
}
}
请添加命名空间Microsoft.Win32,以确保上面的代码可以编译。
4 启动,停止Windows服务
这项API提供的实用功能常常用来管理应用程序中的服务,而不必到控制面板的管理服务中进行操作。
ServiceController controller = new ServiceController("e-M-POWER");
controller.Start();
if (controller.CanPauseAndContinue)
{
controller.Pause();
controller.Continue();
}
controller.Stop();
.net提供的API中,可以实现一句话安装与卸载服务
if (args[0] == "/i")
{
ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
}
else if (args[0] == "/u")
{
ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
}
如代码所示,给应用程序传入i或u参数,以表示是卸载或是安装程序。
5 验证程序是否有strong name (P/Invoke)
比如在程序中,为了验证程序集是否有签名,可调用如下方法
[DllImport("mscoree.dll", CharSet=CharSet.Unicode)]
static extern bool StrongNameSignatureVerificationEx(string wszFilePath, bool fForceVerification, ref bool pfWasVerified);
bool notForced = false;
bool verified = StrongNameSignatureVerificationEx(assembly, false, ref notForced);
Console.WriteLine("Verified: {0}\nForced: {1}", verified, !notForced);
这个功能常用在软件保护方法,可用来验证签名的组件。即使你的签名被人去掉,或是所有程序集的签名都被去除,只要程序中有这一项调用代码,则可以停止程序运行。
6 响应系统配置项的变更
比如我们锁定系统后,如果QQ没有退出,则它会显示了忙碌状态。
请添加命名空间Microsoft.Win32,然后对注册下面的事件。
. DisplaySettingsChanged (包含Changing) 显示设置
. InstalledFontsChanged 字体变化
. PaletteChanged
. PowerModeChanged 电源状态
. SessionEnded (用户正在登出或是会话结束)
. SessionSwitch (变更当前用户)
. TimeChanged 时间改变
. UserPreferenceChanged (用户偏号 包含Changing)
我们的ERP系统,会监测系统时间是否改变,如果将时间调整后ERP许可文件之外的范围,会导致ERP软件不可用。
7 运用Windows7的新特性
Windows7系统引入一些新特性,比如打开文件对话框,状态栏可显示当前任务的进度。
Microsoft.WindowsAPICodePack.Dialogs.CommonOpenFileDialog ofd = new Microsoft.WindowsAPICodePack.Dialogs.CommonOpenFileDialog();
ofd.AddToMostRecentlyUsedList = true;
ofd.IsFolderPicker = true;
ofd.AllowNonFileSystemItems = true;
ofd.ShowDialog();
用这样的方法打开对话框,与BCL自带类库中的OpenFileDialog功能更多一些。不过只限于Windows 7系统中,所以要调用这段代码,还要检查操作系统的版本要大于6,并且添加对程序集Windows API Code Pack for Microsoft®.NET Framework的引用,请到这个地址下载 http://code.msdn.microsoft.com/WindowsAPICodePack
8 检查程序对内存的消耗
用下面的方法,可以检查.NET给程序分配的内存数量
long available = GC.GetTotalMemory(false);
Console.WriteLine("Before allocations: {0:N0}", available);
int allocSize = 40000000;
byte[] bigArray = new byte[allocSize];
available = GC.GetTotalMemory(false);
Console.WriteLine("After allocations: {0:N0}", available);
在我的系统中,它运行的结果如下所示
Before allocations: 651,064
After allocations: 40,690,080
使用下面的方法,可以检查当前应用程序占用的内存
Process proc = Process.GetCurrentProcess();
Console.WriteLine("Process Info: "+Environment.NewLine+
"Private Memory Size: {0:N0}"+Environment.NewLine +
"Virtual Memory Size: {1:N0}" + Environment.NewLine +
"Working Set Size: {2:N0}" + Environment.NewLine +
"Paged Memory Size: {3:N0}" + Environment.NewLine +
"Paged System Memory Size: {4:N0}" + Environment.NewLine +
"Non-paged System Memory Size: {5:N0}" + Environment.NewLine,
proc.PrivateMemorySize64, proc.VirtualMemorySize64, proc.WorkingSet64, proc.PagedMemorySize64, proc.PagedSystemMemorySize64, proc.NonpagedSystemMemorySize64 );
9 使用记秒表检查程序运行时间
如果你担忧某些代码非常耗费时间,可以用StopWatch来检查这段代码消耗的时间,如下面的代码所示
System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
timer.Start();
Decimal total = 0;
int limit = 1000000;
for (int i = 0; i < limit; ++i)
{
total = total + (Decimal)Math.Sqrt(i);
}
timer.Stop();
Console.WriteLine("Sum of sqrts: {0}",total);
Console.WriteLine("Elapsed milliseconds: {0}",
timer.ElapsedMilliseconds);
Console.WriteLine("Elapsed time: {0}", timer.Elapsed);
现在已经有专门的工具来检测程序的运行时间,可以细化到每个方法,比如dotNetPerformance软件。
以上面的代码为例子,您需要直接修改源代码,如果是用来测试程序,则有些不方便。请参考下面的例子。
class AutoStopwatch : System.Diagnostics.Stopwatch, IDisposable
{
public AutoStopwatch()
{
Start();
}
public void Dispose()
{
Stop();
Console.WriteLine("Elapsed: {0}", this.Elapsed);
}
}
借助于using语法,像下面的代码所示,可以检查一段代码的运行时间,并打印在控制台上。
using (new AutoStopwatch())
{
Decimal total2 = 0;
int limit2 = 1000000;
for (int i = 0; i < limit2; ++i)
{
total2 = total2 + (Decimal)Math.Sqrt(i);
}
}
10 使用光标
当程序正在后台运行保存或是册除操作时,应当将光标状态修改为忙碌。可使用下面的技巧。
class AutoWaitCursor : IDisposable
{
private Control _target;
private Cursor _prevCursor = Cursors.Default;
public AutoWaitCursor(Control control)
{
if (control == null)
{
throw new ArgumentNullException("control");
}
_target = control;
_prevCursor = _target.Cursor;
_target.Cursor = Cursors.WaitCursor;
}
public void Dispose()
{
_target.Cursor = _prevCursor;
}
}
用法如下所示,这个写法,是为了预料到程序可能会抛出异常
using (new AutoWaitCursor(this))
{
...
throw new Exception();
}
如代码所示,即使抛出异常,光标也可以恢复到之间的状态。
C#常用代码集合(1)的更多相关文章
- phpcms v9模板制作常用代码集合(转)
phpcms v9模板制作常用代码集合(个人收藏) 1.截取调用标题长度 {str_cut($r[title],36,'')} 2.格式化时间 调用格式化时间 2011-05-06 11:22:33 ...
- phpcms v9模板制作常用代码集合
phpcms v9模板制作常用代码集合(个人收藏) 1.截取调用标题长度 {str_cut($r[title],36,'')} 2.格式化时间 调用格式化时间 2011-05-06 11:22:33 ...
- SAP屏幕字段常用代码集合
SAP屏幕字段常用代码集合 ().Screen 设计 TABLES: SSCRFIELDS. PARAMETERS: P_EBLEN LIKE VBRK-EBLEN DEFAULT ' '. PARA ...
- ExtJS常用代码集合
ExtJS常用代码集合,包括弹出提示框,登陆框,树状结构等等.1. [代码]弹出提示框 <html> <head> <title>Ge ...
- Android常用代码集合
这篇文章主要记录一些常用的一些代码段,方便以后查阅,不断更新中. 1:调用浏览器,载入某网址 1 2 3 Uri uri = Uri.parse("http://www.android-st ...
- Unity3D常用代码集合
1.基本碰撞检测代码 function OnCollisionEnter(theCollision : Collision){ if(theCollision.gameObject.n ...
- Yii2 常用代码集合
Yii2.0 对数据库查询的一些简单的操作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ...
- Android 常用代码大集合 [转]
[Android]调用字符串资源的几种方法 字符串资源的定义 文件路径:res/values/strings.xml 字符串资源定义示例: <?xml version="1.0&q ...
- Java 常用List集合使用场景分析
Java 常用List集合使用场景分析 过年前的最后一篇,本章通过介绍ArrayList,LinkedList,Vector,CopyOnWriteArrayList 底层实现原理和四个集合的区别.让 ...
随机推荐
- Eclipse创建Maven时提示错误could not resolve archetype
今天用Eclipse创建Maven多模块项目的时候提示错误: could not resolve archetype ******release from any of the configured ...
- Light OJ 1030 - Discovering Gold(概率dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题目大意:有一个很长的洞穴, 可以看做是1-n的格子.你的起始位置在1的 ...
- 记录容易忘记的知识点(html 内容)
<xx 表文件名> 导入外部样式表 <link type="text/css" rel="stylesheet" href="xx. ...
- Mac 安装mysql5.7 注意事项
下载与安装 去mysql官网(http://dev.mysql.com/downloads/mysql/)下载自己Mac相对应 MySQL Community 下载下来接压之后你会发现mysql就 ...
- Nginx+tomcat负载均衡时静态页面报404
百度到的问题解决BLOG http://os.51cto.com/art/201204/326843.htm nginx+2台tomcat负载均衡,应用程序已部署,单独访问tomcat时,可以访问到所 ...
- 問題排查:DataGridView 資料行下拉選單,資料繫結階段顯示 DataGridViewComboBoxCell 值無效
可能原因: 1.下拉選單的選項資料繫結晚於 DataGridView 的資料繫結 2.下拉選單的 DataPropertyName 屬性,比 DisplayMember.ValueMember 早賦值 ...
- Shell基础-ech0,cat,history,alias,unalias,bash快捷键,wc,执行结果写入文件
1 系统所支持的shell存放于 /etc/shells 文件中,shell脚本的开头 #!/bing/bash 是指定使用的脚本类型 不能省略,省略之后有些文件可以执行,但容易出错 这行不是注释 2 ...
- Module 'fileinfo' already loaded in Unknown on line 0
出现的原因是:需要加载的扩展已经以而二进制文件的形式写入了php中,但是,在php.ini中却再一次动态加载 参考出处
- Getting Started With Hazelcast 读书笔记(第七章)
第七章 部署策略 Hazelcast具有适应性,能根据不同的架构和应用进行特定的部署配置,每个应用可以根据具体情况选择最优的配置: 数据与应用紧密结合的模式(重点,of就是这种) 胖客户端模式(最好用 ...
- [PHP] Xhprof 非侵入式使用指南
一般使用 Xhprof ,按文档操作可以快速上手,文件头开启 Xhprof,应用结束处得到访问的url查看. 这种使用方式可以快速看到效果,同时也有一些不好的地方: 一是不利于重复利用写好的示例代码: ...