本帖最后由 许庭洲 于 2012-11-1 21:11 编辑
创建系统信息显示程序
第一步:创建一个Windows窗体应用程序项目InfoDemo。
第二步:Form1设计如下,4个按钮事件如下
private void button1_Click(object sender, EventArgs e)
{
throw new IndexOutRangeException();
}
private void button2_Click(object sender, EventArgs e)
{
throw new InvalidOperationException();
}
private void button3_Click(object sender, EventArgs e)
{
throw new InvalidCastException();
}
private void button4_Click(object sender, EventArgs e)
{
throw new InvalidProgramException();
}
第三步:添加一个窗体Form2,设计如下,并添加构造函数
Exception e = new Exception();
public Form2(Exception m_Exception)
{
InitializeComponet();
e = m_Exception;
#region 提示信息
label1.Text += e.Message;
label2.Text += e.HelpLink;
label3.Text += e.Source;
textBox1.Text += e.StackTrace;
textBox2.Text += e.TargetSite.ToString();
#endregion
#region 提示信息
label4.Text += Environment.CurrentDirectoy;
label5.Text += Environment.MachineName;
label6.Text += Environment.OSVersion;
label7.Text += Environment.SystemDirectory;
label8.Text += Environment.UserName;
label9.Text += Environment.Version;
#endregion
}
第四步:修改主函数如下
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
try
{
Application.Run(new Form1());
}
catch(Exception e)
{
Form2 frm = new Form2(e);
Application.Run(frm);
]
}
|
|