《Pro c# with .net 3.0 Sepcial Edition》
真是好书,下面的代码是我自己理解写的,做个备查。
跟书上源代码不大一样我的是写在一个项目里头的。
最后程序域的那个例子里头需要引用System.Windows.Form。

Code
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Diagnostics;
6
using System.Reflection;
7
using System.Windows.Forms;
8
9
10
namespace ProcessTest
11

{
12
class Program
13
{
14
static void Main(string[] args)
15
{
16
do
17
{
18
try
19
{
20
//ShowAllProceses();
21
//int id = int.Parse(Console.ReadLine());
22
//ShowAllThreads(id);
23
//EnumMOdsByPid(id);
24
25
AppDomain currectAD = AppDomain.CurrentDomain;
26
MessageBox.Show("Hello");
27
ShowAllAppDomainAssembles(currectAD);
28
AppDomain secondAppDomain = AppDomain.CreateDomain("SecondAppDoamin");
29
ShowAllAppDomainAssembles(secondAppDomain);
30
}
31
catch (Exception exc)
32
{
33
Console.WriteLine(exc.Message);
34
}
35
}
36
while (Console.ReadLine().ToUpper() != "Q");
37
}
38
39
//获取当前进程集合
40
public static void ShowAllProceses()
41
{
42
43
try
44
{
45
Process[] runningProcs = Process.GetProcesses();
46
Console.WriteLine("Current Process Running");
47
Console.WriteLine("**************************************************");
48
foreach (Process p in runningProcs)
49
{
50
Console.Write(p.Id.ToString() + "\t" + p.ProcessName.ToString());
51
Console.WriteLine();
52
}
53
//int id =int.Parse (Console.ReadLine());
54
//Process.GetProcessById(id).Kill();
55
}
56
catch (Exception exc)
57
{
58
Console.Write(exc.Message);
59
}
60
61
62
}
63
64
//获取指定进程的线程集合
65
public static void ShowAllThreads(int id)
66
{
67
try
68
{
69
Process theProc = Process.GetProcessById(id);
70
ProcessThreadCollection theThreads = theProc.Threads;
71
Console.WriteLine("All Threads of {0}", id);
72
Console.WriteLine("******************************************");
73
foreach (ProcessThread pt in theThreads)
74
{
75
string info = string.Format("-> Thread ID:{0}\tStart Time {1}\tPriority {2}", pt.Id, pt.StartTime, pt.PriorityLevel);
76
Console.WriteLine(info);
77
}
78
Console.WriteLine("This Process have {0} Threads", theProc.Threads.Count);
79
}
80
catch (Exception exc)
81
{
82
Console.WriteLine(exc.Message);
83
}
84
}
85
86
//获取指定进程的模块集合
87
public static void EnumMOdsByPid(int id)
88
{
89
try
90
{
91
Process theProc = Process.GetProcessById(id);
92
ProcessModuleCollection theModules = theProc.Modules;
93
Console.WriteLine("All Modules of {0}", id);
94
Console.WriteLine("**************************************************");
95
foreach (ProcessModule pm in theModules)
96
{
97
string info = string.Format("->{0}\tMemorySize:{1}\tFileName:{2}\tVer:{3}", pm.ModuleName, pm.ModuleMemorySize, pm.FileName, pm.FileVersionInfo);
98
Console.WriteLine(info);
99
}
100
Console.WriteLine("There are {0} Modules", theModules.Count);
101
}
102
catch (Exception exc)
103
{
104
Console.WriteLine(exc.Message);
105
}
106
}
107
108
//获取当前应用程序域中模块的名称和版本
109
public static void ShowAllAppDomainAssembles(AppDomain ad)
110
{
111
try
112
{
113
Assembly[] loadedAssembles = ad.GetAssemblies();
114
Console.WriteLine("All Assembles in currect AppDomain"+ad.FriendlyName );
115
Console.WriteLine("**********************************************");
116
foreach (Assembly a in loadedAssembles)
117
{
118
string info = string.Format("->Name:{0}\tVersion:{1}\t", a.GetName().Name ,a.GetName().Version );
119
Console.WriteLine(info);
120
}
121
}
122
catch (Exception exc)
123
{
124
Console.WriteLine(exc.Message);
125
}
126
}
127
}
128
}
129
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架