alex_bn_lee

导航

【013】◀▶ Diagnostics & System

System.Diagnostics 命名空间提供类,使您能够与系统进程、事件日志和性能计数器进行交互。

---------------------------------------------------------------------------------------------------------

●·● 目录

System.Diagnostics 命名空间

A1 ………… Process 类
A2 ………… Stopwatch 类

System 命名空间

G1 ………… Environment 类

Microsoft.Win32 命名空间

U1 ………… Registry 类
U2 ………… RegistryKey 类

System.Globalization 命名空间


X1 ………… ChineseLunisolarCalendar 类
X2 ………… RegistryKey 类

--------------------------------------------------------------------------------------------------------------

●·● System.Diagnostics 命名空间

--------------------------------------------------------------------------------------------------------------

            ╔════════╗
╠════╣    第A1个    ╠══════════════════════════════════════════════════╣
            ╚════════╝

●·● Process 类

1. 提供对本地和远程进程的访问并使您能够启动和停止本地系统进程。

2. Process 属性:

  • Id:获取关联进程的唯一标识符。
  • ProcessName:获取该进程的名称。
  • PrivateMemorySize64:获取为关联的进程分配的专用内存量。

3. Process 方法:

  • Start:启动程序或启动文件。
    View Code - 举例
    1. 用Explorer.exe打开文件夹:
      System.Diagnostics.Process.Start("Explorer.exe",@"D:\DOCUMENTS\");
      System.Diagnostics.Process.Start("Explorer.exe",@"D:\DOCUMENTS");

    2. 用notepad.exe打开记事本:
      System.Diagnostics.Process.Start("notepad.exe",@"F:\Desktop\1.txt");

    3. 用Word的快捷方式打开Word文件:
      System.Diagnostics.Process.Start(@"F:\Desktop\Word 2010", @"F:\Desktop\1.docx");

    4. 用Firefox打开网址:www.baidu.com:
      System.Diagnostics.Process.Start(@"C:\Program Files (x86)\Mozilla Firefox\firefox.exe", "www.baidu.com");
  • CloseMainWindow:通过向进程的主窗口发送关闭消息来关闭拥有用户界面的进程。
  • Close:释放与此组件关联的所有资源。
            Process myProcess = new Process();        //新建进程

    private void button5_Click(object sender, EventArgs e)
    {
    FileInfo fi = new FileInfo(@"F:\Desktop\test1.txt");
    using (StreamWriter sw = fi.AppendText())
    {
    sw.WriteLine("Hello!"); //追加文本
    }
    myProcess = Process.Start("notepad.exe", @"F:\Desktop\test1.txt"); //为进程赋值,然后打开进程
    }

    private void button6_Click(object sender, EventArgs e)
    {
    myProcess.CloseMainWindow(); //关闭新建的记事本进程窗体
    myProcess.Close(); //释放与此组件关联的所有资源。
    }
  • GetProcesses:为本地计算机上的每个进程资源创建一个新的 Process 组件。
    View Code - 举例
            string str = "";
    private void button7_Click(object sender, EventArgs e)
    {
    Process[] processes = Process.GetProcesses();
    for (int i = 0; i < processes.Length - 1;i++ )
    {
    str = str + Convert.ToString(processes[i].Id) + ":" + processes[i].ProcessName + "\r\n";
    }
    using (StreamWriter sw = File.CreateText(@"F:\Desktop\test1.txt"))
    {
    sw.Write(str);
    }
    Process.Start("notepad.exe",@"F:\Desktop\test1.txt");
    }
  • Kill:立即停止关联的进程。

参考:C#打开exe文件(三种方法)

//第一种方法:

System.Diagnostics.ProcessStartInfo info =new System.Diagnostics.ProcessStartInfo(path);
info.WorkingDirectory = Path.GetDirectoryName(path); System.Diagnostics.Process.Start(info);

//第二中方法:有些程序需要通过方法一才能打开

System.Diagnostics.Process.Start(path)

//第三种方法:

Process p =new Process();
p.StartInfo.FileName ="cmd.exe";
p.StartInfo.UseShellExecute =false;
p.StartInfo.RedirectStandardInput =true;
p.StartInfo.RedirectStandardOutput =true;
p.StartInfo.RedirectStandardError =true;
p.StartInfo.CreateNoWindow =true;
p.StartInfo.WorkingDirectory = Application.StartupPath +@"\FormGen\";
p.Start();
p.StandardInput.WriteLine("FormGen.exe");
p.StandardInput.WriteLine("exit"); 
View Code

举例:简易任务管理器

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace 进程管理器
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
GetProcess();
}
private void GetProcess()
{
Process[] ps = Process.GetProcesses();
label1.Text = ps.Length.ToString();
foreach (Process p in ps)
{
ListViewItem lvi = new ListViewItem();
lvi.Text = p.ProcessName;
lvi.SubItems.AddRange(new string[] { p.Id.ToString(), p.PrivateMemorySize64.ToString() });
listView1.Items.Add(lvi);
}
}

private void button2_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
try
{
string proName = listView1.SelectedItems[0].Text;
Process[] p = Process.GetProcessesByName(proName);
p[0].Kill();
MessageBox.Show("进程关闭成功");
GetProcess();
}
catch (System.Exception ex)
{
MessageBox.Show("无法关闭此进程!");
}
}
else
{
MessageBox.Show("请选择要终止的进程!");
}
}

private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text != string.Empty)
{
Process.Start(textBox1.Text);
}
else
{
MessageBox.Show("请输入启动项");
textBox1.Focus();
}
}
}
}

--------------------------------------------------------------------------------------------------------------

            ╔════════╗
╠════╣    第A2个    ╠══════════════════════════════════════════════════╣
            ╚════════╝

●·● Stopwatch 类

 1. 提供一组方法和属性,可用于准确地测量运行时间。

 2. Stopwatch 属性:

  • Elapsed:获取当前实例测量得出的总运行时间。
  • ElapsedMilliseconds:获取当前实例测量得出的总运行时间(以毫秒为单位)。
  • ElapsedTicks:获取当前实例测量得出的总运行时间(用计时器计时周期表示)。
  • IsRunning:获取一个指示 Stopwatch 计时器是否在运行的值。

3. Stopwatch 方法:

  • Start:开始或继续测量某个时间间隔的运行时间。
  • Stop:停止测量某个时间间隔的运行时间。
  • Reset:停止时间间隔测量,并将运行时间重置为零。
  • Restart:停止时间间隔测量,将运行时间重置为零,然后开始测量运行时间。
  • StartNew:对新的 Stopwatch 实例进行初始化,将运行时间属性设置为零,然后开始测量运行时间。【静态】

--------------------------------------------------------------------------------------------------------------

●·● System 命名空间

--------------------------------------------------------------------------------------------------------------

            ╔════════╗
╠════╣    第G1个    ╠══════════════════════════════════════════════════╣
            ╚════════╝

●·● Environment 类

1. 提供有关当前环境和平台的信息以及操作它们的方法。 此类不能被继承。

2. Environment 属性:【静态】

  • CurrentDirectory:获取或设置当前工作目录的完全限定路径。
  • Is64BitOperatingSystem:确定当前操作系统是否为 64 位操作系统。
  • Is64BitProcess:确定当前进程是否为 64 位进程。
  • MachineName:获取此本地计算机的 NetBIOS 名称。
  • OSVersion:获取包含当前平台标识符和版本号的 OperatingSystem 对象。
  • ProcessorCount:获取当前计算机上的处理器数。
  • SystemDirectory:获取系统目录的完全限定路径。
  • SystemPageSize:获取操作系统的页面文件的内存量。
  • TickCount:获取系统启动后经过的毫秒数。
  • UserDomainName:获取与当前用户关联的网络域名。
  • Version:获取一个 Version 对象,该对象描述公共语言运行时的主版本、次版本、内部版本和修订号。

--------------------------------------------------------------------------------------------------------------

●·● Microsoft.Win32 命名空间

--------------------------------------------------------------------------------------------------------------

            ╔════════╗
╠════╣    第U1个    ╠══════════════════════════════════════════════════╣
            ╚════════╝

●·● Registry 类

1. 提供表示 Windows 注册表中的根项的 RegistryKey 对象,并提供访问项/值对的 static 方法。

2. Registry 字段:【静态】

  • ClassesRoot:定义文档的类型(或类)以及与那些类型关联的属性。 该字段读取 Windows 注册表基项 HKEY_CLASSES_ROOT。
  • CurrentConfig: 包含有关非用户特定的硬件的配置信息。 该字段读取 Windows 注册表基项 HKEY_CURRENT_CONFIG。 
  • CurrentUser: 包含有关当前用户首选项的信息。 该字段读取 Windows 注册表基项 HKEY_CURRENT_USER
  • LocalMachine: 包含本地计算机的配置数据。 该字段读取 Windows 注册表基项 HKEY_LOCAL_MACHINE。 
  • PerformanceData: 包含软件组件的性能信息。 该字段读取 Windows 注册表基项 HKEY_PERFORMANCE_DATA。 
  • Users: 包含有关默认用户配置的信息。 该字段读取 Windows 注册表基项 HKEY_USERS。 

3. Registry 方法:【静态】

  • GetValue: 检索与指定的注册表项中的指定名称关联的值。 如果在指定的项中未找到该名称,则返回您提供的默认值;或者,如果指定的项不存在,则返回 Nothing 
  • SetValue(String, String, Object): 设置指定的注册表项的指定名称/值对。 如果指定的项不存在,则创建该项。 

--------------------------------------------------------------------------------------------------------------

            ╔════════╗
╠════╣    第U2个    ╠══════════════════════════════════════════════════╣
            ╚════════╝

●·● RegistryKey 类

1. 表示 Windows 注册表中的项级节点。 此类是注册表封装。

 

2. RegistryKey 属性:

  • handle:获取一个 SafeRegistryHandle 对象,该对象表示当前 RegistryKey 对象封装的注册表项。
  • Name:检索项的名称。
  • SubKeyCount:检索当前项的子项数目。
  • ValueCount:检索项中值的计数。
  • View:获取用于创建注册表项的视图。

3. RegistryKey 方法:

  • OpenSubKey:
    GetSubKeyNames:
  • GetValueNames:
  • GetValue(String): 检索与指定名称关联的值。 如果注册表中不存在名称/值对,则返回 Nothing

※ 参考:http://archive.cnblogs.com/a/2001040/

--------------------------------------------------------------------------------------------------------------

●·● System.Globalization 命名空间

--------------------------------------------------------------------------------------------------------------

            ╔════════╗
╠════╣    第X1个    ╠══════════════════════════════════════════════════╣
            ╚════════╝

●·● ChineseLunisolarCalendar 类

1. 将时间分成多个部分来表示,如分成年、月和日。 年按农历计算,而日和月按阴阳历计算。

 

2. ChineseLunisolarCalendar 方法:

  • GetCelestialStem:计算甲子(60 年)循环中指定年份的天干。
    • 返回值:一个从 1 到 60 的整数,表示甲子循环中的一年。
  • GetDayOfMonth(DateTime time):计算指定日期中的月中日期。
  • GetLeapMonth(int year):计算指定年份的闰月。(不存在,返回 0)
  • GetMonthsInYear(int year, int era):计算指定纪元年份中的月数。

举例:

获取指定日期的 农历!存在以下问题:

  • 考虑将年份的整型数字转为汉字,1987 为 一九八七 ...!
  • 考虑将月份转成汉字的月份,1 为 正 ...!
  • 考虑将日转为汉字的日,1 为 初一 ...!
  • 考虑闰月的存在!

==> 将获取的闰月后的月份减一返回:

 

private int GetMonthFromYear(DateTime time)
{
    ChineseLunisolarCalendar clc = new ChineseLunisolarCalendar();
    if (clc.GetLeapMonth(time.Year) != 0)   //存在闰月
    {
        if (clc.GetMonth(time) > clc.GetLeapMonth(time.Year))   //如果月份大于闰月
        {
            return clc.GetMonth(time) - 1;
        }
    }
    return clc.GetMonth(time);
}

 

 

==> 定义月份和日的两个 Hashtable:

 

Hashtable lunarYear = new Hashtable();
lunarYear.Add(1, "一");
lunarYear.Add(2, "二");
lunarYear.Add(3, "三");
lunarYear.Add(4, "四");
lunarYear.Add(5, "五");
lunarYear.Add(6, "六");
lunarYear.Add(7, "七");
lunarYear.Add(8, "八");
lunarYear.Add(9, "九");
lunarYear.Add(0, "零");
Hashtable lunarMonth = new Hashtable();
lunarMonth.Add(1, "");
lunarMonth.Add(2, "");
lunarMonth.Add(3, "");
lunarMonth.Add(4, "");
lunarMonth.Add(5, "");
lunarMonth.Add(6, "");
lunarMonth.Add(7, "");
lunarMonth.Add(8, "");
lunarMonth.Add(9, "");
lunarMonth.Add(10, "");
lunarMonth.Add(11, "十一");
lunarMonth.Add(12, "十二");

Hashtable lunarDay = new Hashtable();
lunarDay.Add(1, "初一");
lunarDay.Add(2, "初二");
lunarDay.Add(3, "初三");
lunarDay.Add(4, "初四");
lunarDay.Add(5, "初五");
lunarDay.Add(6, "初六");
lunarDay.Add(7, "初七");
lunarDay.Add(8, "初八");
lunarDay.Add(9, "初九");
lunarDay.Add(10, "初十");
lunarDay.Add(11, "十一");
lunarDay.Add(12, "十二");
lunarDay.Add(13, "十三");
lunarDay.Add(14, "十四");
lunarDay.Add(15, "十五");
lunarDay.Add(16, "十六");
lunarDay.Add(17, "十七");
lunarDay.Add(18, "十八");
lunarDay.Add(19, "十九");
lunarDay.Add(20, "二十");
lunarDay.Add(21, "二十一");
lunarDay.Add(22, "二十二");
lunarDay.Add(23, "二十三");
lunarDay.Add(24, "二十四");
lunarDay.Add(25, "二十五");
lunarDay.Add(26, "二十六");
lunarDay.Add(27, "二十七");
lunarDay.Add(28, "二十八");
lunarDay.Add(29, "二十九");
lunarDay.Add(30, "三十");

 

 

==> 定义获取农历的方法:

 

char[] yearNums = new char[4];  //获取年份的单个数字
yearNums = new ChineseLunisolarCalendar().GetYear(dateTimePicker1.Value.Date).ToString().ToCharArray();  //获取年份的整数,并将其转为单个的 char
string year =
    lunarYear[Convert.ToInt32(yearNums[0].ToString())].ToString() +  //注意:直接将 char 转为 int 时候,会按 ASCII 的规则!
    lunarYear[Convert.ToInt32(yearNums[1].ToString())].ToString() +
    lunarYear[Convert.ToInt32(yearNums[2].ToString())].ToString() +
    lunarYear[Convert.ToInt32(yearNums[3].ToString())].ToString();  //将 char 先转为 string,再将其转为 int,然后在带入 lunarYear 中获取汉字
string month = lunarMonth[GetMonthFromYear(dateTimePicker1.Value.Date)].ToString();  //月份转换
string day = lunarDay[new ChineseLunisolarCalendar().GetDayOfMonth(dateTimePicker1.Value.Date)].ToString();  //日转换
textBox1.Text = year + "" +month + "" + day;  //用汉字连接起来

 

 

效果图:

--------------------------------------------------------------------------------------------------------------

            ╔════════╗
╠════╣    第X2个    ╠══════════════════════════════════════════════════╣
            ╚════════╝

●·● Calendar 类

1. 将时间分成段来表示,如分成星期、月和年。

  •  

 

  •  

 

 

 

 

 

 

posted on 2012-02-20 18:45  McDelfino  阅读(573)  评论(2编辑  收藏  举报