﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>语源科技BlogJava-AndyZhang</title><link>http://www.blogjava.net/AndyZhang/</link><description>welcome to java world</description><language>zh-cn</language><lastBuildDate>Tue, 21 Apr 2026 06:29:39 GMT</lastBuildDate><pubDate>Tue, 21 Apr 2026 06:29:39 GMT</pubDate><ttl>60</ttl><item><title>WinForm窗体间如何传值</title><link>http://www.blogjava.net/AndyZhang/archive/2014/03/04/410598.html</link><dc:creator>SkyDream</dc:creator><author>SkyDream</author><pubDate>Tue, 04 Mar 2014 08:32:00 GMT</pubDate><guid>http://www.blogjava.net/AndyZhang/archive/2014/03/04/410598.html</guid><description><![CDATA[<div m_b_15"="" style="margin: 10px 0px 0px; padding: 15px; background-color: #f7f7f7; border: 1px solid #e1eded; line-height: 24px; font-family: simsun; overflow: hidden; text-indent: 24px; color: #333333;"><span style="margin: 0px; padding: 0px; font-weight: bold;">本文导读</span>：稍微复杂一点的程序一般都有两个或者更多的窗体。有时在程序设计中，数据不仅要在同一个窗体中传递，还要在窗体间传递，尤其是用子窗体如何改变父窗体。WinForm多窗体间传值的方法有很多，根据需要传送的值的类型可以采用不同的方法</div><div id="contentText" style="margin: 0px; height: 9940px; overflow: hidden; color: #333333; font-family: simsun; line-height: 26px; background-color: #ffffff;"><p style="margin: 20px 0px 0px; padding: 0px;"><strong style="margin: 0px; padding: 0px;">窗体间传递数据，无论是父窗体操作子窗体，还是子窗体操作符窗体，有以下几种方式：</strong></p><p style="margin: 20px 0px 0px; padding: 0px;">&nbsp;</p><ol style="margin: 0px; padding: 0px; list-style: none;"><li style="margin: 0px; padding: 0px; list-style: none;">公共静态变量；</li><li style="margin: 0px; padding: 0px; list-style: none;">使用共有属性；</li><li style="margin: 0px; padding: 0px; list-style: none;">使用委托与事件；</li><li style="margin: 0px; padding: 0px; list-style: none;">通过构造函数把主窗体传递到从窗体中；</li></ol><p style="margin: 20px 0px 0px; padding: 0px;">&nbsp;</p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #000000;"><strong style="margin: 0px; padding: 0px;">一、通过静态变量</strong></span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #000000;"><strong style="margin: 0px; padding: 0px;">特点：传值是双向的，实现简单</strong></span></p><p style="margin: 20px 0px 0px; padding: 0px;">&nbsp;</p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #000000;"><strong style="margin: 0px; padding: 0px;">实现代码如下：</strong></span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #000000;">在一个<span face="Times New Roman" style="margin: 0px; padding: 0px;">app</span>类中定义一个静态成员<span face="Times New Roman" style="margin: 0px; padding: 0px;">value</span></span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">public class app</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">{</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">public static string value;</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">}</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #000000;">在窗体<span face="Times New Roman" style="margin: 0px; padding: 0px;">Form1</span>中这样调用</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">app.value = "f2"; //</span>给静态成员赋值</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">new Form2 ( ).Show ( ); //</span>显示<span face="Times New Roman" style="margin: 0px; padding: 0px;">Form2</span></span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #000000;">在窗体<span face="Times New Roman" style="margin: 0px; padding: 0px;">Form2</span>中</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">this.Text = app.value; //</span>取回<span face="Times New Roman" style="margin: 0px; padding: 0px;">app.value</span>的值</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">app.value = "Form2"; //</span>给<span face="Times New Roman" style="margin: 0px; padding: 0px;">app.value</span>赋值，以便其他窗体调用</span></p><p style="margin: 20px 0px 0px; padding: 0px;">&nbsp;</p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #000000;"><strong style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; font-size: 16px;">二、公共变量</span><span style="margin: 0px; padding: 0px; font-size: 16px;">传值</span></strong></span></p><p style="margin: 20px 0px 0px; padding: 0px;">这种方法就是利用一个公共变量，先把需要的值存放到这个公共变量，然后需要的时候再通过读取该变量的值来使用</p><p style="margin: 20px 0px 0px; padding: 0px;"><strong style="margin: 0px; padding: 0px;">例子</strong></p><p style="margin: 20px 0px 0px; padding: 0px;">Form1：</p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">public static string Form1Value; // 注意，必须申明为static变量</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">private void button1_Click(object sender, EventArgs e)</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">{</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">Form1Value = "来自Form1";</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">new Form2().Show();</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">}</span></p><p style="margin: 20px 0px 0px; padding: 0px;">Form2：</p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">private void Form_Load(object sender, EventArgs e)</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">{</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">MessageBox.Show(Form1.Form1Value);</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">}</span></p><p style="margin: 20px 0px 0px; padding: 0px;">这种方式传值，理解和使用上都相对的简单，但是容易让变量串值，比如第一次修改为&#8220;a&#8221;，而第二次修改为&#8220;b&#8221;，则有可能本来需要&#8220;a&#8221;值的结果变成了&#8220;b&#8221;。</p><p style="margin: 20px 0px 0px; padding: 0px;">&nbsp;</p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #000000;"><strong style="margin: 0px; padding: 0px;">三、静态方法方式访问</strong></span></p><p style="margin: 20px 0px 0px; padding: 0px;">这种方法与传值方法的第一种有些类似，就是将需要被其他窗体访问的方法用static定义，以便其他传递可以直接访问到</p><p style="margin: 20px 0px 0px; padding: 0px;"><strong style="margin: 0px; padding: 0px;">例子</strong>：</p><p style="margin: 20px 0px 0px; padding: 0px;">Form1:</p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">private void button1_Click(object sender, EventArgs e)</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">{</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">new Form2().Show();</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">}</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">public static void FF()</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">{</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">MessageBox.Show("Form1方法");</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">}</span></p><p style="margin: 20px 0px 0px; padding: 0px;">Form2:</p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">private void Form_Load(object sender, EventArgs e)</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">{</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">Form1.FF();</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">}</span></p><p style="margin: 20px 0px 0px; padding: 0px;">使用这种方法访问其他窗体的方法，在跨窗体上实现方便，但是需要需要访问到控件值则不能直接访问，需要将值先传递给其他窗体，再有窗体传递回来，或者存储在其他变量后对这变量进行访问。</p><p style="margin: 20px 0px 0px; padding: 0px;">&nbsp;</p><p style="margin: 20px 0px 0px; padding: 0px;"><strong style="margin: 0px; padding: 0px;">四、通过窗体的公有属性值</strong></p><p style="margin: 20px 0px 0px; padding: 0px;"><strong style="margin: 0px; padding: 0px;">特点：实现简单</strong></p><p style="margin: 20px 0px 0px; padding: 0px;">&nbsp;</p><p style="margin: 20px 0px 0px; padding: 0px;"><strong style="margin: 0px; padding: 0px;">实现代码如下：</strong></p><p style="margin: 20px 0px 0px; padding: 0px;">在窗体<span face="Times New Roman" style="margin: 0px; padding: 0px;">Form2</span>中定义一个公有属性<span face="Times New Roman" style="margin: 0px; padding: 0px;">Form2Value,</span>获取和设置<span face="Times New Roman" style="margin: 0px; padding: 0px;">textBox1</span>的文本值</p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">public string Form2Value</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">{</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">get</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">{</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">return this.textBox1.Text;</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">}</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">set</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">{</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">this.textBox1.Text = value;</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">}</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">}</span></p><p style="margin: 20px 0px 0px; padding: 0px;">在窗体<span face="Times New Roman" style="margin: 0px; padding: 0px;">Form1</span>中这样调用</p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">Form2 f2 = new Form2 ( );</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">f2.Form2Value = "Ok"; //</span>给<span face="Times New Roman" style="margin: 0px; padding: 0px;">Form2</span>的<span face="Times New Roman" style="margin: 0px; padding: 0px;">textBox1</span>赋值<span face="Times New Roman" style="margin: 0px; padding: 0px;">Ok</span></span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">f2.ShowDialog ( );</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><strong style="margin: 0px; padding: 0px;">&nbsp;</strong></p><p style="margin: 20px 0px 0px; padding: 0px;"><strong style="margin: 0px; padding: 0px;">五、通过窗体的公有属性值和<span face="Times New Roman" style="margin: 0px; padding: 0px;">Owner</span>属性</strong></p><p style="margin: 20px 0px 0px; padding: 0px;"><strong style="margin: 0px; padding: 0px;">特点：实现简单<span face="Times New Roman" style="margin: 0px; padding: 0px;">,</span>灵活</strong></p><p style="margin: 20px 0px 0px; padding: 0px;">&nbsp;</p><p style="margin: 20px 0px 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;"><strong style="margin: 0px; padding: 0px;">实现代码如下</strong>：</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">在窗体<span face="Times New Roman" style="margin: 0px; padding: 0px;">Form1</span>中</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">public int Form1Value = 1;</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">Form2 f2 = new Form2 ( );</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;"><span face="Times New Roman" style="margin: 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">f2.ShowDialog ( this ); //</span>把<span face="Times New Roman" style="margin: 0px; padding: 0px;">Form1</span>作为<span face="Times New Roman" style="margin: 0px; padding: 0px;">Form2</span>的所有者传递给<span face="Times New Roman" style="margin: 0px; padding: 0px;">Form2</span></span></span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">在窗体<span face="Times New Roman" style="margin: 0px; padding: 0px;">Form2</span>中</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;"><span face="Times New Roman" style="margin: 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">//Form2</span>的所有者是<span face="Times New Roman" style="margin: 0px; padding: 0px;">Form1</span></span></span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">Form1 f1 = ( Form1 ) this.Owner;</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;"><span face="Times New Roman" style="margin: 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">//</span>取到<span face="Times New Roman" style="margin: 0px; padding: 0px;">Form1</span>的值是<span face="Times New Roman" style="margin: 0px; padding: 0px;">1</span></span></span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">MessageBox.Show ( f1.Form1Value .ToString ( ) );</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;"><span face="Times New Roman" style="margin: 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">//</span>给<span face="Times New Roman" style="margin: 0px; padding: 0px;">Form1</span>的<span face="Times New Roman" style="margin: 0px; padding: 0px;">Form1Value</span>赋值<span face="Times New Roman" style="margin: 0px; padding: 0px;">222</span></span></span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">f1.Form1Value = 222;</span></p><p style="margin: 20px 0px 0px; padding: 0px;">&nbsp;</p><p style="margin: 20px 0px 0px; padding: 0px;"><strong style="margin: 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">六、</span>通过窗体的公有属性值和<span face="Times New Roman" style="margin: 0px; padding: 0px;">Application.OpenForms</span>属性</strong></p><p style="margin: 20px 0px 0px; padding: 0px;"><strong style="margin: 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">说明：<span face="Times New Roman" style="margin: 0px; padding: 0px;">Application.OpenForms</span>属性：获取属于应用程序的打开窗体的集合。<span face="Times New Roman" style="margin: 0px; padding: 0px;">(</span>此属性在<span face="Times New Roman" style="margin: 0px; padding: 0px;">.NET Framework2.0</span>版中<span face="Times New Roman" style="margin: 0px; padding: 0px;">)</span></span></strong></p><p style="margin: 20px 0px 0px; padding: 0px;">&nbsp;</p><p style="margin: 20px 0px 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;"><strong style="margin: 0px; padding: 0px;">实现代码如下</strong>：</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">在窗体<span face="Times New Roman" style="margin: 0px; padding: 0px;">Form1</span>中</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">public int Form1Value = 1;</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">Form2 f2 = new Form2 ( );</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">f2.Show ( );</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">在窗体<span face="Times New Roman" style="margin: 0px; padding: 0px;">Form2</span>中</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">string formName = "Form1";</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">Form fr = Application.OpenForms [ formName ];</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">if ( fr != null )</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">{</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">Form1 f1 = ( Form1 ) fr;</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;"><span face="Times New Roman" style="margin: 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">//</span>取到<span face="Times New Roman" style="margin: 0px; padding: 0px;">Form1</span>的值是<span face="Times New Roman" style="margin: 0px; padding: 0px;">1</span></span></span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">MessageBox.Show ( f1.Form1Value.ToString ( ) );</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;"><span face="Times New Roman" style="margin: 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">//</span>给<span face="Times New Roman" style="margin: 0px; padding: 0px;">Form1</span>的<span face="Times New Roman" style="margin: 0px; padding: 0px;">Form1Value</span>赋值<span face="Times New Roman" style="margin: 0px; padding: 0px;">222</span></span></span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">f1.Form1Value = 222;</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">}</span></p><p style="margin: 20px 0px 0px; padding: 0px;">&nbsp;</p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #000000;"><span face="Times New Roman" style="margin: 0px; padding: 0px;"><strong style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; font-size: 16px;">七、传参式传值</span></strong></span></span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">此方法顾名思义就是将需要的值一参数形式传递给需要值的窗体</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;"><strong style="margin: 0px; padding: 0px;">例子</strong>：</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">Form1：</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">private void button1_Click(object sender, EventArgs e)</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">{</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">new Form2("来自Form1").Show();</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">}</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">Form2:</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">public Form2(string value)</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">{</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">InitializeComponent();</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">MessageBox.Show(vaue);</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">}</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">此种窗体间传值方法明显优与第一种的传值参数，也不会出现串数据现象，但是需要注意修改Form2的构造函数，每个窗体的默认构造函数默认都是无参数的，因此需要去修改构造函数。</span></p><p style="margin: 20px 0px 0px; padding: 0px;">&nbsp;</p><p style="margin: 20px 0px 0px; padding: 0px;"><strong style="margin: 0px; padding: 0px;">八、通过构造函数</strong></p><p style="margin: 20px 0px 0px; padding: 0px;"><strong style="margin: 0px; padding: 0px;">特点：传值是单向的（不可以互相传值），实现简单</strong></p><p style="margin: 20px 0px 0px; padding: 0px;">&nbsp;</p><p style="margin: 20px 0px 0px; padding: 0px;"><strong style="margin: 0px; padding: 0px;">实现代码如下：</strong></p><p style="margin: 20px 0px 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">在窗体<span face="Times New Roman" style="margin: 0px; padding: 0px;">Form2</span>中</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">int value1;</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">string value2;</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">public Form2 ( int value1 , string value2 )</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">{</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">InitializeComponent ( );</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">this.value1 = value1;</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">this.value2 = value2;</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">}</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">在窗体<span face="Times New Roman" style="margin: 0px; padding: 0px;">Form1</span>中这样调用</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;"><span face="Times New Roman" style="margin: 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">new Form2 ( 111 , "222" ).Show ( ); //</span>这样就把<span face="Times New Roman" style="margin: 0px; padding: 0px;">111,"222",</span>这<span face="Times New Roman" style="margin: 0px; padding: 0px;">2</span>个值传送给了<span face="Times New Roman" style="margin: 0px; padding: 0px;">Form2</span></span></span></p><p style="margin: 20px 0px 0px; padding: 0px;">&nbsp;</p><p style="margin: 20px 0px 0px; padding: 0px;"><strong style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #000000;">九、用委托来实现。</span></strong></p><p style="margin: 20px 0px 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">委托可以把一个方法作为参数带入另一个方法，在窗体传值中，子窗体需要执行一个方法来改变父窗体的值，</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">而这个方法，则可以用委托从父窗体传递过来。在父窗体中，声明修改文本框的方法AfterChildChange，在new一个子窗体的时候，将这个方法传递给子窗体。那么当子窗体点击同步按钮时，会执行父窗体的AfterChildChange方法，已达到修改文本框值的目的。</span></p><p style="margin: 20px 0px 0px; padding: 0px;">&nbsp;</p><p style="margin: 20px 0px 0px; padding: 0px;"><strong style="margin: 0px; padding: 0px;">例子</strong></p><p style="margin: 20px 0px 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">1、在子窗体中设置一个委托类型的属性：</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">public Action&lt;string&gt; AfterChangeTextDel { get; set; }</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">2、在子窗体同步按钮中：</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">if (AfterChangeTextDel != null)<br />{<br />AfterChangeTextDel(this.textBox1.Text);//执行委托<br />}</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">3、在父窗体中添加方法：</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">public void AfterChildChange(string text)<br />{<br />txtName.Text = text;<br />}</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">4、在父窗体启动子窗体按钮：</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">ChildFrm frm =new ChildFrm();</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">frm.AfterChangeTextDel = new Action&lt;string&gt;(this.AfterChildChange);</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">frm.Show();</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">5、这样也能实现窗体传值，子窗体的委托可以在父窗体 其他地方可以进行直接执行。：解决此问题。微软引入了事件。</span></p><p style="margin: 20px 0px 0px; padding: 0px;">&nbsp;</p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #000000;"><strong style="margin: 0px; padding: 0px;">十、用事件来实现</strong></span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">事件是委托类型的对象。它内部是用委托来实现的，对于事件来说，外部只能注册自己+=，注销自己-=,外界不可以注销其他的注册者，也不能主动触发事件。而委托则无法实现这些控制，因此诞生了事件这话总语法。</span></p><p style="margin: 20px 0px 0px; padding: 0px;">&nbsp;</p><p style="margin: 20px 0px 0px; padding: 0px;"><strong style="margin: 0px; padding: 0px;">实现代码如下</strong></p><p style="margin: 20px 0px 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">在窗体<span face="Times New Roman" style="margin: 0px; padding: 0px;">Form2</span>中定义公有属性<span face="Times New Roman" style="margin: 0px; padding: 0px;">Form2Value,</span>获取和设置<span face="Times New Roman" style="margin: 0px; padding: 0px;">textBox1</span>的文本值</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">并且还定义一个<span face="Times New Roman" style="margin: 0px; padding: 0px;">accept</span>事件</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">public string Form2Value</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">{</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">get</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">{</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">return this.textBox1.Text;</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">}</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">set</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">{</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">this.textBox1.Text = value;</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">}</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">}</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">public event EventHandler accept;</span></p><p style="margin: 20px 0px 0px; padding: 0px;">&nbsp;</p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">private void button1_Click ( object sender , EventArgs e )</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">{</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">if ( accept != null )</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">{</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">accept ( this , EventArgs.Empty ); //</span>当窗体触发事件，传递自身引用</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">}</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">}</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">在窗体<span face="Times New Roman" style="margin: 0px; padding: 0px;">Form1</span>中</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">Form2 f2 = new Form2 ( );</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">f2.accept += new EventHandler ( f2_accept );</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">f2.Show ( );</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">void f2_accept ( object sender , EventArgs e )</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">{</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">//</span>事件的接收者通过一个简单的类型转换得到<span face="Times New Roman" style="margin: 0px; padding: 0px;">Form2</span>的引用</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">Form2 f2 = (Form2) sender;</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;"><span face="Times New Roman" style="margin: 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;"><span face="Times New Roman" style="margin: 0px; padding: 0px;">//</span>接收到<span face="Times New Roman" style="margin: 0px; padding: 0px;">Form2</span>的<span face="Times New Roman" style="margin: 0px; padding: 0px;">textBox1.Text</span></span></span></span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">this.textBox1.Text = f2.Form2Value;</span></p><p style="margin: 20px 0px 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000cd;">}</span></p></div><img src ="http://www.blogjava.net/AndyZhang/aggbug/410598.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/AndyZhang/" target="_blank">SkyDream</a> 2014-03-04 16:32 <a href="http://www.blogjava.net/AndyZhang/archive/2014/03/04/410598.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>!page.ispostback 的意思,用法！</title><link>http://www.blogjava.net/AndyZhang/archive/2012/05/01/377129.html</link><dc:creator>SkyDream</dc:creator><author>SkyDream</author><pubDate>Tue, 01 May 2012 14:46:00 GMT</pubDate><guid>http://www.blogjava.net/AndyZhang/archive/2012/05/01/377129.html</guid><description><![CDATA[<p>!Page.IsPostBack，表示页面首次加载,还有按钮默认是回发服务器的，所以点击按钮页面会刷新。
</p>
<p>&nbsp; &nbsp; 控件都是服务器端的控件, 如果你的控件设为自动回发到服务器（即AutoPostBack属性设为True），执行每次操作都会提交到后台处理(就是所谓的后台代码，即以[.aspx.cs]结尾的页面),也就要在再次加载页面的同时做出相应的操作，所以先再次执行page_load事件，执行了
if(!page.IsPostBack){}外的代码
后再执行其它的操作（你在页面上的操作）。处理完再回发到，即跳转到相应的页面。如果还是回发到原来的页面，则这时候该页面就不是第一次加载了，而是俗称的回传页。</p>
<p>&nbsp; &nbsp; &nbsp; 把 if(!page.IsPostBack){ 这里是首次加载页面时要实现的功能代码；} 代码写在page_load事件中。<span style="color: red">还</span><span style="color: red">可以使用if(!IsPostBack)来判断&#8212;&#8212;是否是当前页面控件的操作 ！！</span></p>
<img src ="http://www.blogjava.net/AndyZhang/aggbug/377129.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/AndyZhang/" target="_blank">SkyDream</a> 2012-05-01 22:46 <a href="http://www.blogjava.net/AndyZhang/archive/2012/05/01/377129.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>为工具栏添加鼠标的“按下、移动、松开”三个事件，用来移动窗体</title><link>http://www.blogjava.net/AndyZhang/archive/2012/04/27/376782.html</link><dc:creator>SkyDream</dc:creator><author>SkyDream</author><pubDate>Fri, 27 Apr 2012 08:50:00 GMT</pubDate><guid>http://www.blogjava.net/AndyZhang/archive/2012/04/27/376782.html</guid><description><![CDATA[<p><font face="Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private bool isMouseDown = false;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private Point FormLocation;&nbsp;&nbsp;&nbsp;&nbsp; //form的location<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private Point mouseOffset;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //鼠标的按下位置<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private void toolStrip1_MouseDown(object sender, MouseEventArgs e)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (e.Button == MouseButtons.Left)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; isMouseDown = true;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FormLocation = this.Location;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mouseOffset = Control.MousePosition;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font></p>
<p><font face="Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font></p>
<p><font face="Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private void toolStrip1_MouseUp(object sender, MouseEventArgs e)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; isMouseDown = false;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font></p>
<p><font face="Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private void toolStrip1_MouseMove(object sender, MouseEventArgs e)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int _x = 0;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int _y = 0;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (isMouseDown)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Point pt = Control.MousePosition;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _x = mouseOffset.X - pt.X;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _y = mouseOffset.Y - pt.Y;</font></p>
<p><font face="Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.Location = new Point(FormLocation.X - _x, FormLocation.Y - _y);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font></p>
<p><font face="Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br /></font></p><img src ="http://www.blogjava.net/AndyZhang/aggbug/376782.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/AndyZhang/" target="_blank">SkyDream</a> 2012-04-27 16:50 <a href="http://www.blogjava.net/AndyZhang/archive/2012/04/27/376782.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C# 计算文件的MD5(值信息摘要算法)</title><link>http://www.blogjava.net/AndyZhang/archive/2012/04/23/376399.html</link><dc:creator>SkyDream</dc:creator><author>SkyDream</author><pubDate>Mon, 23 Apr 2012 09:40:00 GMT</pubDate><guid>http://www.blogjava.net/AndyZhang/archive/2012/04/23/376399.html</guid><description><![CDATA[<p style="margin: 5px auto; word-spacing: 0px; font: 13px/19px Verdana, Geneva, Arial, Helvetica, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><font face="Verdana">MD5 是 Message Digest Algorithm 5（信息摘要算法）的缩写，MD5 一种散列(Hash)技术，广泛用于加密、解密、数据签名和数据完整性校验等方面。任何一个文件，无论是可执行程序、图像文件、临时文件或者其他任何类型的文件，也不管它体积多大，都可以计算出一个MD5值，如果文件被修改过，就算只改动了一个字节，其 MD5 值也会变得完全不同。因此，我们可以通过对比同一文件的 MD5 值，来校验这个文件是否被&#8220;篡改&#8221;过。</font></p><span style="display: inline! important; float: none; word-spacing: 0px; font: 13px/19px Verdana, Geneva, Arial, Helvetica, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">C# 可以方便的计算出文件的 MD5 值：</span> 
<div class="cnblogs_code" style="border-right: rgb(204,204,204) 1px solid; padding-right: 5px; border-top: rgb(204,204,204) 1px solid; padding-left: 5px; padding-bottom: 5px; margin: 5px 0px; word-spacing: 0px; font: 12px/19px 'Courier New'; overflow: auto; text-transform: none; border-left: rgb(204,204,204) 1px solid; color: rgb(0,0,0); word-break: break-all; text-indent: 0px; padding-top: 5px; border-bottom: rgb(204,204,204) 1px solid; white-space: normal; letter-spacing: normal; background-color: rgb(245,245,245); orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><img id="Code_Open_Image_174933" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" onclick="this.style.display='none'; document.getElementById('Code_Open_Text_174933').style.display='none'; getElementById('Code_Closed_Image_174933').style.display='inline'; getElementById('Code_Closed_Text_174933').style.display='inline';" height="16" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top"><span id="Code_Open_Text_174933" style="display: inline; font-size: 12px; line-height: 1.5; font-family: 'Courier New'"><br /><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">///</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">&lt;summary&gt;</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'"><br /></span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">///</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;计算文件的&nbsp;MD5&nbsp;值<br /></span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">///</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">&lt;/summary&gt;</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'"><br /></span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">///</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">&lt;param&nbsp;name="fileName"&gt;</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'">要计算&nbsp;MD5&nbsp;值的文件名和路径</span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">&lt;/param&gt;</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'"><br /></span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">///</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">&lt;returns&gt;</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'">MD5&nbsp;值16进制字符串</span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">&lt;/returns&gt;</span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'"><br /></span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">public</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">static</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">string</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;MD5File(</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">string</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;fileName)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">return</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;HashFile(fileName,&nbsp;</span><span style="font-size: 12px; color: rgb(128,0,0); line-height: 1.5; font-family: 'Courier New'">"</span><span style="font-size: 12px; color: rgb(128,0,0); line-height: 1.5; font-family: 'Courier New'">md5</span><span style="font-size: 12px; color: rgb(128,0,0); line-height: 1.5; font-family: 'Courier New'">"</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">);<br />}<br /><br /></span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">///</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">&lt;summary&gt;</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'"><br /></span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">///</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;计算文件的哈希值<br /></span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">///</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">&lt;/summary&gt;</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'"><br /></span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">///</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">&lt;param&nbsp;name="fileName"&gt;</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'">要计算哈希值的文件名和路径</span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">&lt;/param&gt;</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'"><br /></span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">///</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">&lt;param&nbsp;name="algName"&gt;</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'">算法:sha1,md5</span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">&lt;/param&gt;</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'"><br /></span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">///</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">&lt;returns&gt;</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'">哈希值16进制字符串</span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">&lt;/returns&gt;</span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'"><br /></span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">public</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">static</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">string</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;HashFile(</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">string</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;fileName,&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">string</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;algName)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">if</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;(</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">!</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">System.IO.File.Exists(fileName))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">return</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">string</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">.Empty;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;FileStream&nbsp;fs&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">=</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">new</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;FileStream(fileName,&nbsp;FileMode.Open,&nbsp;FileAccess.Read);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">byte</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">[]&nbsp;hashBytes&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">=</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;HashData(fs,&nbsp;algName);<br />&nbsp;&nbsp;&nbsp;&nbsp;fs.Close();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">return</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;ByteArrayToHexString(hashBytes);<br />}<br /><br /></span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">///</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">&lt;summary&gt;</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'"><br /></span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">///</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;计算哈希值<br /></span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">///</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">&lt;/summary&gt;</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'"><br /></span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">///</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">&lt;param&nbsp;name="stream"&gt;</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'">要计算哈希值的&nbsp;Stream</span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">&lt;/param&gt;</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'"><br /></span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">///</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">&lt;param&nbsp;name="algName"&gt;</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'">算法:sha1,md5</span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">&lt;/param&gt;</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'"><br /></span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">///</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">&lt;returns&gt;</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'">哈希值字节数组</span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">&lt;/returns&gt;</span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'"><br /></span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">public</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">static</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">byte</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">[]&nbsp;HashData(Stream&nbsp;stream,&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">string</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;algName)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;HashAlgorithm&nbsp;algorithm;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">if</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;(algName&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">==</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">null</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">throw</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">new</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;ArgumentNullException(</span><span style="font-size: 12px; color: rgb(128,0,0); line-height: 1.5; font-family: 'Courier New'">"</span><span style="font-size: 12px; color: rgb(128,0,0); line-height: 1.5; font-family: 'Courier New'">algName&nbsp;不能为&nbsp;null</span><span style="font-size: 12px; color: rgb(128,0,0); line-height: 1.5; font-family: 'Courier New'">"</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">if</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;(</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">string</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">.Compare(algName,&nbsp;</span><span style="font-size: 12px; color: rgb(128,0,0); line-height: 1.5; font-family: 'Courier New'">"</span><span style="font-size: 12px; color: rgb(128,0,0); line-height: 1.5; font-family: 'Courier New'">sha1</span><span style="font-size: 12px; color: rgb(128,0,0); line-height: 1.5; font-family: 'Courier New'">"</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">,&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">true</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">)&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">==</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(128,0,128); line-height: 1.5; font-family: 'Courier New'">0</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;algorithm&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">=</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;SHA1.Create();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">else</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'"><br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">if</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;(</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">string</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">.Compare(algName,&nbsp;</span><span style="font-size: 12px; color: rgb(128,0,0); line-height: 1.5; font-family: 'Courier New'">"</span><span style="font-size: 12px; color: rgb(128,0,0); line-height: 1.5; font-family: 'Courier New'">md5</span><span style="font-size: 12px; color: rgb(128,0,0); line-height: 1.5; font-family: 'Courier New'">"</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">,&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">true</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">)&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">!=</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(128,0,128); line-height: 1.5; font-family: 'Courier New'">0</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">throw</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">new</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;Exception(</span><span style="font-size: 12px; color: rgb(128,0,0); line-height: 1.5; font-family: 'Courier New'">"</span><span style="font-size: 12px; color: rgb(128,0,0); line-height: 1.5; font-family: 'Courier New'">algName&nbsp;只能使用&nbsp;sha1&nbsp;或&nbsp;md5</span><span style="font-size: 12px; color: rgb(128,0,0); line-height: 1.5; font-family: 'Courier New'">"</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;algorithm&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">=</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;MD5.Create();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">return</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;algorithm.ComputeHash(stream);<br />}</span></span></div>
<p style="margin: 5px auto; word-spacing: 0px; font: 13px/19px Verdana, Geneva, Arial, Helvetica, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><font face="Verdana">&nbsp;&nbsp;&nbsp; ComputeHash 方法返回的是<font face="Verdana">哈希值字节数组，而文件的 MD5 值通常是用16进制的字符串表示，<font face="Verdana">字节数组转换为16进制表示的字符串</font>的方法在<font face="Verdana"><span class="Apple-converted-space">&nbsp;</span>System.Web.Configuration.MachineKeySection</font><span class="Apple-converted-space">&nbsp;</span>中已经提供了一个名为<span class="Apple-converted-space">&nbsp;</span><font face="Verdana">ByteArrayToHexString</font><span class="Apple-converted-space">&nbsp;</span>方法，但是该方法是<font face="Verdana">NonPublic 的，我们不能直接调用。这里我们<font face="Verdana">通过反射来调用 MachineKeySection 中的 ByteArrayToHexString 方法。</font></font></font></font></p><span style="display: inline! important; float: none; word-spacing: 0px; font: 13px/19px Verdana, Geneva, Arial, Helvetica, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">通过反射调用 MachineKeySection 中的 ByteArrayToHexString 方法：</span> 
<div class="cnblogs_code" style="border-right: rgb(204,204,204) 1px solid; padding-right: 5px; border-top: rgb(204,204,204) 1px solid; padding-left: 5px; padding-bottom: 5px; margin: 5px 0px; word-spacing: 0px; font: 12px/19px 'Courier New'; overflow: auto; text-transform: none; border-left: rgb(204,204,204) 1px solid; color: rgb(0,0,0); word-break: break-all; text-indent: 0px; padding-top: 5px; border-bottom: rgb(204,204,204) 1px solid; white-space: normal; letter-spacing: normal; background-color: rgb(245,245,245); orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><img id="Code_Open_Image_185113" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" onclick="this.style.display='none'; document.getElementById('Code_Open_Text_185113').style.display='none'; getElementById('Code_Closed_Image_185113').style.display='inline'; getElementById('Code_Closed_Text_185113').style.display='inline';" height="16" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top"><span id="Code_Open_Text_185113" style="display: inline; font-size: 12px; line-height: 1.5; font-family: 'Courier New'"><br /><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">///</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">&lt;summary&gt;</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'"><br /></span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">///</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;字节数组转换为16进制表示的字符串<br /></span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">///</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'">&lt;/summary&gt;</span><span style="font-size: 12px; color: rgb(128,128,128); line-height: 1.5; font-family: 'Courier New'"><br /></span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">public</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">static</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">string</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;ByteArrayToHexString(</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">byte</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">[]&nbsp;buf)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">int</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;iLen&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">=</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(128,0,128); line-height: 1.5; font-family: 'Courier New'">0</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'">//</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;通过反射获取&nbsp;MachineKeySection&nbsp;中的&nbsp;ByteArrayToHexString&nbsp;方法，该方法用于将字节数组转换为16进制表示的字符串。</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'"><br /></span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;Type&nbsp;type&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">=</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">typeof</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">(System.Web.Configuration.MachineKeySection);<br />&nbsp;&nbsp;&nbsp;&nbsp;MethodInfo&nbsp;byteArrayToHexString&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">=</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;type.GetMethod(</span><span style="font-size: 12px; color: rgb(128,0,0); line-height: 1.5; font-family: 'Courier New'">"</span><span style="font-size: 12px; color: rgb(128,0,0); line-height: 1.5; font-family: 'Courier New'">ByteArrayToHexString</span><span style="font-size: 12px; color: rgb(128,0,0); line-height: 1.5; font-family: 'Courier New'">"</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">,&nbsp;BindingFlags.Static&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">|</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;BindingFlags.NonPublic);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'">//</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;字节数组转换为16进制表示的字符串</span><span style="font-size: 12px; color: rgb(0,128,0); line-height: 1.5; font-family: 'Courier New'"><br /></span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">return</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;(</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">string</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">)byteArrayToHexString.Invoke(</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">null</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">,&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">new</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 12px; color: rgb(0,0,255); line-height: 1.5; font-family: 'Courier New'">object</span><span style="font-size: 12px; color: rgb(0,0,0); line-height: 1.5; font-family: 'Courier New'">[]&nbsp;{&nbsp;buf,&nbsp;iLen&nbsp;});<br />}</span></span></div>
<p style="margin: 5px auto; word-spacing: 0px; font: 13px/19px Verdana, Geneva, Arial, Helvetica, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;&nbsp;&nbsp; 如今&#8220;MD5碰撞生成器&#8221;的出现，使 MD5 也正在面临挑战。</p>
<p style="margin: 5px auto; word-spacing: 0px; font: 13px/19px Verdana, Geneva, Arial, Helvetica, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;&nbsp;&nbsp; 顺便介绍一个 MD5 工具 Md5Checker，Md5Checker 是一款免费、快速、小巧、易用的文件 MD5 值计算、验证、管理软件。Md5Checker 的官方网站是：<font face="Verdana"><a style="color: rgb(0,102,170); text-decoration: none" href="http://cn.getmd5checker.com/">http://cn.getmd5checker.com/</a></font></p><img src ="http://www.blogjava.net/AndyZhang/aggbug/376399.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/AndyZhang/" target="_blank">SkyDream</a> 2012-04-23 17:40 <a href="http://www.blogjava.net/AndyZhang/archive/2012/04/23/376399.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>winform怎么比较时间</title><link>http://www.blogjava.net/AndyZhang/archive/2012/04/23/376397.html</link><dc:creator>SkyDream</dc:creator><author>SkyDream</author><pubDate>Mon, 23 Apr 2012 09:30:00 GMT</pubDate><guid>http://www.blogjava.net/AndyZhang/archive/2012/04/23/376397.html</guid><description><![CDATA[<span style="display: inline! important; float: none; word-spacing: 0px; font: bold 15px/24px 微软雅黑; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(252,254,252); orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">winform时间比较。例如：当前时间超过早上8点就不可在打卡了。怎么比较的。</span><br /><pre class="reply-text mb10" id="content-846764126" style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 10px; word-spacing: 0px; font: 14px/24px Arial; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; zoom: 1; letter-spacing: normal; background-color: rgb(253,255,248); word-wrap: break-word; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">DateTime dt1 = DateTime.Now;
DateTime dt2 = Convert.ToDateTime(DateTime.Now.ToShortDateString() + " 08:00:00");
if (DateTime.Compare(dt1, dt2) &gt; 0)
{
    //dt1时间比dt2大
}
else if (DateTime.Compare(dt1, dt2) == 0)
{
    //TODO
}
else
{
    //TODO
}</pre>
<p>&nbsp;</p>
<p>&nbsp;</p><pre id="question-content" style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/24px Arial; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; zoom: 1; letter-spacing: normal; background-color: rgb(252,254,252); word-wrap: break-word; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">数据库里有个结束时间！ 我登录的时候要获取系统时间和 数据库里的时间做对比！没到结束时间 可以登录  到了就不可以登录  </pre>
<p>&nbsp;</p><pre class="reply-text mb10" id="best-answer-content" style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 10px; word-spacing: 0px; font: 14px/22px Arial; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; zoom: 1; letter-spacing: normal; background-color: rgb(255,252,246); word-wrap: break-word; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">winform里比较：
Datetime overTime = 获取数据库结束时间；
if(Datetime.Now &lt;= overTime)
{
登录；
}
else
{
无法登录；
}

数据库里比较(以mysql举例）：
select （now（） &lt; overTime);
其中now（）是系统函数当前时间，overtime的话你select一下你的结束时间，如果是确定值可以直接用'2012-12-21 00:00:00'这样的形式</pre><pre style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/24px Arial; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; zoom: 1; letter-spacing: normal; background-color: rgb(255,252,246); word-wrap: break-word; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"> if (Convert.ToDateTime(tiems).CompareTo(Convert.ToDateTime(beoverdate)) &lt; 1)</pre>
<p>&nbsp;</p>
<p><span style="display: inline! important; float: none; word-spacing: 0px; font: bold 15px/24px 微软雅黑; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(252,254,252); orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">C# winform中怎么将时间转换成int类型的数字并且与一个数字进行比较？</span><br /></p><pre class="reply-text mb10" id="best-answer-content" style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 10px; word-spacing: 0px; font: 14px/22px Arial; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; zoom: 1; letter-spacing: normal; background-color: rgb(255,252,246); word-wrap: break-word; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">string num="1234560";
int a=int.Prase(num);
注意：num中只能出现数字且开头不为0，否则会出错 
如果是小数就用
string num1="3.1415926";
float bfloat.prase(num1);</pre>
<p>&nbsp;</p><pre class="reply-text mb10" id="content-708503592" style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 10px; word-spacing: 0px; font: 14px/24px Arial; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; zoom: 1; letter-spacing: normal; background-color: rgb(255,255,255); word-wrap: break-word; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">int.parse

convert.toint32 </pre><span style="display: inline! important; float: none; word-spacing: 0px; font: 14px/23px simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">将系统时间减去30天之后，格式化为数据库中时间的格式</span><br style="word-spacing: 0px; font: 14px/23px simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px" /><span style="display: inline! important; float: none; word-spacing: 0px; font: 14px/23px simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">string dt = DateTime.Now.AddDays(-30).ToString("yyyy-MM-dd");</span><br style="word-spacing: 0px; font: 14px/23px simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px" /><br style="word-spacing: 0px; font: 14px/23px simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px" /><span style="display: inline! important; float: none; word-spacing: 0px; font: 14px/23px simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">select * from table where 数据库时间 &gt; '"+dt+"'</span><br style="word-spacing: 0px; font: 14px/23px simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px" /><br style="word-spacing: 0px; font: 14px/23px simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px" /><span style="display: inline! important; float: none; word-spacing: 0px; font: 14px/23px simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">如果能返回记录为0，说明已经超过30天<br /><br /><span style="display: inline! important; float: none; word-spacing: 0px; font: 12px/19px simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">DateTime SystemTime = DateTime.Now;&nbsp; //获得系统时间</span><br style="word-spacing: 0px; font: 12px/19px simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px" /><span style="display: inline! important; float: none; word-spacing: 0px; font: 12px/19px simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">string str = "2009-8-13 12:00:00";&nbsp; //str是数据库记录的时间</span><br style="word-spacing: 0px; font: 12px/19px simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px" /><span style="display: inline! important; float: none; word-spacing: 0px; font: 12px/19px simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">DateTime DatabaseTime = DateTime.Parse(str);</span><br style="word-spacing: 0px; font: 12px/19px simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px" /><br style="word-spacing: 0px; font: 12px/19px simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px" /><span style="display: inline! important; float: none; word-spacing: 0px; font: 12px/19px simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">TimeSpan ts = SystemTime.Subtract(DatabaseTime);&nbsp; //系统时间减去数据库时间</span><br style="word-spacing: 0px; font: 12px/19px simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px" /><br style="word-spacing: 0px; font: 12px/19px simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px" /><span style="display: inline! important; float: none; word-spacing: 0px; font: 12px/19px simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">int i = ts.Days;&nbsp; //间隔的天数</span></span><img src ="http://www.blogjava.net/AndyZhang/aggbug/376397.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/AndyZhang/" target="_blank">SkyDream</a> 2012-04-23 17:30 <a href="http://www.blogjava.net/AndyZhang/archive/2012/04/23/376397.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>winform验证textbox只能输入数字或中文正则表达式</title><link>http://www.blogjava.net/AndyZhang/archive/2012/04/23/376384.html</link><dc:creator>SkyDream</dc:creator><author>SkyDream</author><pubDate>Mon, 23 Apr 2012 08:01:00 GMT</pubDate><guid>http://www.blogjava.net/AndyZhang/archive/2012/04/23/376384.html</guid><description><![CDATA[<h3 class="title pre fs1" style="word-spacing: 0px; text-transform: none; color: rgb(102,102,102); text-indent: 0px; line-height: normal; font-style: normal; font-family: Arial; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); font-variant: normal; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="color: rgb(0,0,255); line-height: normal"><strong style="line-height: normal"><span style="color: rgb(51,102,255); line-height: normal">验证数字的正则表达式集</span></strong></span><span style="color: rgb(0,0,255); line-height: normal"><br style="line-height: normal" /></span>验证数字：^[0-9]*$<br style="line-height: normal" />验证n位的数字：^\d{n}$<br style="line-height: normal" />验证至少n位数字：^\d{n,}$<br style="line-height: normal" />验证m-n位的数字：^\d{m,n}$<br style="line-height: normal" />验证零和非零开头的数字：^(0|[1-9][0-9]*)$<br style="line-height: normal" />验证有两位小数的正实数：^[0-9]+(.[0-9]{2})?$<br style="line-height: normal" />验证有1-3位小数的正实数：^[0-9]+(.[0-9]{1,3})?$<br style="line-height: normal" />验证非零的正整数：^\+?[1-9][0-9]*$<br style="line-height: normal" />验证非零的负整数：^\-[1-9][0-9]*$<br style="line-height: normal" />验证非负整数（正整数 + 0）：^\d+$<br style="line-height: normal" />验证非正整数（负整数 + 0）： ^((-\d+)|(0+))$</h3>
<div class="bct fc05 fc11 nbw-blog ztag js-fs2" style="filter: none; visibility: visible! important; word-spacing: 0px; font: 12px Arial; text-transform: none; color: rgb(102,102,102); word-break: break-all; text-indent: 0px; zoom: 1! important; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); word-wrap: break-word; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">
<p style="line-height: normal"><strong style="line-height: normal"><span style="color: rgb(51,102,255); line-height: normal">验证中文的正则表达式集合</span></strong><br style="line-height: normal" />验证所有东亚区的语言：^[\?-\?]+$&nbsp;<br style="line-height: normal" />验证简体和繁体：^[\一-\?]+$<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />验证简体：^[\一-\龥]+$</p>
<p style="line-height: normal"><span style="color: rgb(51,102,255); line-height: normal"><strong style="line-height: normal">使用方法如下</strong></span><br style="line-height: normal" />使用Regex对象先引用System.Text.RegularExpressions<br style="line-height: normal" />string s="^[0-9]*$";//正则表达式<br style="line-height: normal" />Regex reg=new Regex(s);<br style="line-height: normal" />if(!reg.IsMatch(this.textbox1.text))<br style="line-height: normal" />{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MessageBox.Show("请输入数字");<br style="line-height: normal" />}</p>
<p style="line-height: normal">&nbsp;</p>
<p style="line-height: normal">&nbsp;</p>
<p style="line-height: normal">各式各样的正则表达式参考大全（访问正则表达式实例库）：<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />^\d+$　　//匹配非负整数（正整数 + 0） 　<br style="line-height: normal" />//匹配整数 ^\d+(\.\d+)?$　　//匹配非负浮点数（正浮点数 + 0）<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$　　//匹配正浮点数<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />^((-\d+(\.\d+)?)|(0+(\.0+)?))$　　//匹配非正浮点数（负浮点数 + 0）<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$　　//匹配负浮点数<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />^(-?\d+)(\.\d+)?$　　//匹配浮点数<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />^[A-Za-z]+$&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //匹配由26个英文字母组成的字符串<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />^[A-Z]+$　&nbsp;&nbsp;&nbsp; //匹配由26个英文字母的大写组成的字符串<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />^[a-z]+$　　//匹配由26个英文字母的小写组成的字符串<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />^[A-Za-z0-9]+$　　//匹配由数字和26个英文字母组成的字符串<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />^\w+$　　//匹配由数字、26个英文字母或者下划线组成的字符串<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$　　　　//匹配email地址<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />^[a-zA-z]+://匹配(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$　　//匹配url<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />匹配中文字符的正则表达式： [\u4e00-\u9fa5]<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />匹配双字节字符(包括汉字在内)：[^\x00-\xff]<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />匹配空行的正则表达式：\n[\s| ]*\r<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />匹配HTML标记的正则表达式：/&lt;(.*)&gt;.*&lt;\/&gt;|&lt;(.*) \/&gt;/<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />匹配首尾空格的正则表达式：(^\s*)|(\s*$)<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />匹配Email地址的正则表达式：\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />匹配网址URL的正则表达式：^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />匹配帐号是否合法(字母开头，允许5-16字节，允许字母数字下划线)：^[a-zA-Z][a-zA-Z0-9_]{4,15}$<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />匹配国内电话号码：(\d{3}-|\d{4}-)?(\d{8}|\d{7})?<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />匹配腾讯QQ号：^[1-9]*[1-9][0-9]*$</p>
<p style="line-height: normal">下表是元字符及其在正则表达式上下文中的行为的一个完整列表，具体到每个正则表达式符号：<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />\ 将下一个字符标记为一个特殊字符、或一个原义字符、或一个后向引用、或一个八进制转义符。<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />^ 匹配输入字符串的开始位置。如果设置了 RegExp 对象的Multiline 属性，^ 也匹配 &#8217;\n&#8217; 或 &#8217;\r&#8217; 之后的位置。<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />$ 匹配输入字符串的结束位置。如果设置了 RegExp 对象的Multiline 属性，$ 也匹配 &#8217;\n&#8217; 或 &#8217;\r&#8217; 之前的位置。<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />* 匹配前面的子表达式零次或多次。<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />+ 匹配前面的子表达式一次或多次。+ 等价于 {1,}。<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />? 匹配前面的子表达式零次或一次。? 等价于 {0,1}。<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />{n} n 是一个非负整数，匹配确定的n 次。<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />{n,} n 是一个非负整数，至少匹配n 次。<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />{n,m} m 和 n 均为非负整数，其中n &lt;= m。最少匹配 n 次且最多匹配 m 次。在逗号和两个数之间不能有空格。<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />? 当该字符紧跟在任何一个其他限制符 (*, +, ?, {n}, {n,}, {n,m}) 后面时，匹配模式是非贪婪的。非贪婪模式尽可能少的匹配所搜索的字符串，而默认的贪婪模式则尽可能多的匹配所搜索的字符串。<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />. 匹配除 "\n" 之外的任何单个字符。要匹配包括 &#8217;\n&#8217; 在内的任何字符，请使用象 &#8217;[.\n]&#8217; 的模式。<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />(pattern) 匹配pattern 并获取这一匹配。 (?:pattern) 匹配pattern 但不获取匹配结果，也就是说这是一个非获取匹配，不进行存储供以后使用。 (?=pattern) 正向预查，在任何匹配 pattern 的字符串开始处匹配查找字符串。这是一个非获取匹配，也就是说，该匹配不需要获取供以后使用。 (?!pattern) 负向预查，与(?=pattern)作用相反 x|y 匹配 x 或 y。<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />[xyz] 字符集合。<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />[^xyz] 负值字符集合。<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />[a-z] 字符范围，匹配指定范围内的任意字符。<br style="line-height: normal" />[^a-z] 负值字符范围，匹配任何不在指定范围内的任意字符。<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />\b 匹配一个单词边界，也就是指单词和空格间的位置。<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />\B 匹配非单词边界。<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />\cx 匹配由x指明的控制字符。<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />\d 匹配一个数字字符。等价于 [0-9]。<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />\D 匹配一个非数字字符。等价于 [^0-9]。<br style="line-height: normal" />&nbsp;\f 匹配一个换页符。等价于 \x0c 和 \cL。<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />\n 匹配一个换行符。等价于 \x0a 和 \cJ。<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />\r 匹配一个回车符。等价于 \x0d 和 \cM。<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />\s 匹配任何空白字符，包括空格、制表符、换页符等等。等价于[ \f\n\r\t\v]。<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />\S 匹配任何非空白字符。等价于 [^ \f\n\r\t\v]。<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />\t 匹配一个制表符。等价于 \x09 和 \cI。<br style="line-height: normal" />\v 匹配一个垂直制表符。等价于 \x0b 和 \cK。<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />\w 匹配包括下划线的任何单词字符。等价于&#8217;[A-Za-z0-9_]&#8217;。<br style="line-height: normal" />\W 匹配任何非单词字符。等价于 &#8217;[^A-Za-z0-9_]&#8217;。<br style="line-height: normal" />\xn 匹配 n，其中 n 为十六进制转义值。十六进制转义值必须为确定的两个数字长。<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />\num 匹配 num，其中num是一个正整数。对所获取的匹配的引用。<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />\n 标识一个八进制转义值或一个后向引用。如果 \n 之前至少 n 个获取的子表达式，则 n 为后向引用。否则，如果 n 为八进制数字 (0-7)，则 n 为一个八进制转义值。<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />\nm 标识一个八进制转义值或一个后向引用。如果 \nm 之前至少有is preceded by at least nm 个获取得子表达式，则 nm 为后向引用。如果 \nm 之前至少有 n 个获取，则 n 为一个后跟文字 m 的后向引用。如果前面的条件都不满足，若 n 和 m 均为八进制数字 (0-7)，则 \nm 将匹配八进制转义值 nm。 \nml 如果 n 为八进制数字 (0-3)，且 m 和 l 均为八进制数字 (0-7)，则匹配八.</p>
<p style="line-height: normal">--------------------------------------------------------------------------------</p>
<p style="line-height: normal">&nbsp;</p>
<p style="line-height: normal"><br style="line-height: normal" />if(document.getElementById("personalemail").value==null || document.getElementById("personalemail").value==""){<br style="line-height: normal" />&nbsp;&nbsp;&nbsp;&nbsp; alert("邮箱不能为空");<br style="line-height: normal" />&nbsp;&nbsp;&nbsp;&nbsp; return false;<br style="line-height: normal" />&nbsp;}else if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.getElementById("personalemail").value))){<br style="line-height: normal" />&nbsp;&nbsp;alert("请输入一个有效的邮件地址");<br style="line-height: normal" />&nbsp;&nbsp;document.getElementById("personalemail").value="";<br style="line-height: normal" />&nbsp;&nbsp;return false;<br style="line-height: normal" />&nbsp;}</p>
<p style="line-height: normal">&nbsp;&nbsp;&nbsp; var partten = /^1[3,5]\d{9}$/;<br style="line-height: normal" />&nbsp;if(document.getElementById("cellphone").value==null || document.getElementById("cellphone").value==""){<br style="line-height: normal" />&nbsp;&nbsp;&nbsp; alert("手机号码不能为空");<br style="line-height: normal" />&nbsp;&nbsp;&nbsp; document.getElementById("cellphone").focus();<br style="line-height: normal" />&nbsp;&nbsp;&nbsp; return false;<br style="line-height: normal" />&nbsp;}else if(isNaN(document.getElementById("cellphone").value) &amp;&amp; !partten.test(document.getElementById("cellphone").value)){<br style="line-height: normal" />&nbsp;&nbsp;&nbsp;&nbsp; alert("手机号码只能是数字,并且只能以13或15开头并且长度是11位");<br style="line-height: normal" />&nbsp;&nbsp;&nbsp;&nbsp; document.getElementById("cellphone").focus();<br style="line-height: normal" />&nbsp;&nbsp;&nbsp;&nbsp; return false;<br style="line-height: normal" />&nbsp;}</p></div><img src ="http://www.blogjava.net/AndyZhang/aggbug/376384.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/AndyZhang/" target="_blank">SkyDream</a> 2012-04-23 16:01 <a href="http://www.blogjava.net/AndyZhang/archive/2012/04/23/376384.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>winform相对路径和绝对路径</title><link>http://www.blogjava.net/AndyZhang/archive/2012/04/23/376358.html</link><dc:creator>SkyDream</dc:creator><author>SkyDream</author><pubDate>Mon, 23 Apr 2012 05:39:00 GMT</pubDate><guid>http://www.blogjava.net/AndyZhang/archive/2012/04/23/376358.html</guid><description><![CDATA[<pre class="reply-text mb10" id="best-answer-content" style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 10px; word-spacing: 0px; font: 14px/22px Arial; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; zoom: 1; letter-spacing: normal; background-color: rgb(255,252,246); word-wrap: break-word; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">绝对路径：是从盘符开始的路径，形如
C:\windows\system32\cmd.exe
相对路径：是从当前路径开始的路径，假如当前路径为C:\windows
要描述上述路径，只需输入
system32\cmd.exe
实际上，严格的相对路径写法应为
.\system32\cmd.exe
其中，.表示当前路径，在通道情况下可以省略，只有在特殊的情况下不能省略。
假如当前路径为c:\program files
要调用上述命令，则需要输入
..\windows\system32\cmd.exe
其中，..为父目录。
当前路径如果为c:\program files\common files
则需要输入
..\..\windows\system32\cmd.exe</pre>winform的默认相对路径是相对于 bin文件夹下的debug文件夹的位置<br />例如bin文件夹下的file文件夹的a.mp3文件 的相对路径为 file/a.mp3<br />bin文件夹外的 file文件夹的a.mp3文件 的相对路径为 ../../file/a.mp3 <br /><br /><a style="word-spacing: 0px; font: 20px/30px 'Microsoft YaHei'; text-transform: none; color: rgb(102,102,102); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; text-decoration: none; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px" href="http://blog.csdn.net/lbuskeep/article/details/7252148"><span style="color: #3366ff">C# Winform中如何获取文件路径</span></a><br />
<p style="word-spacing: 0px; font: 14px/26px Arial; text-transform: none; color: rgb(51,51,51); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="color: #3366ff">获取文件名方法：</span></p>
<p style="word-spacing: 0px; font: 14px/26px Arial; text-transform: none; color: rgb(51,51,51); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="color: #3366ff">用System.IO.Path.GetFileName和System.IO.Path.GetFileNameWithoutExtension（无扩展名）的方法</span></p>
<p style="word-spacing: 0px; font: 14px/26px Arial; text-transform: none; color: rgb(51,51,51); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="color: #3366ff">获取文件路径方法：</span></p>
<p style="word-spacing: 0px; font: 14px/26px Arial; text-transform: none; color: rgb(51,51,51); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="color: #3366ff">//获取当前进程的完整路径，包含文件名(进程名)。</span><span id="more-1957"></span><br /><span style="color: #3366ff">string str = this.GetType().Assembly.Location;</span><br /><span style="color: #3366ff">result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)</span></p>
<p style="word-spacing: 0px; font: 14px/26px Arial; text-transform: none; color: rgb(51,51,51); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="color: #3366ff">//获取新的 Process 组件并将其与当前活动的进程关联的主模块的完整路径，包含文件名(进程名)。</span><br /><span style="color: #3366ff">string str = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;</span><br /><span style="color: #3366ff">result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)</span></p>
<p style="word-spacing: 0px; font: 14px/26px Arial; text-transform: none; color: rgb(51,51,51); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="color: #3366ff">//获取和设置当前目录（即该进程从中启动的目录）的完全限定路径。</span><br /><span style="color: #3366ff">string str = System.Environment.CurrentDirectory;</span><br /><span style="color: #3366ff">result: X:\xxx\xxx (.exe文件所在的目录)</span></p>
<p style="word-spacing: 0px; font: 14px/26px Arial; text-transform: none; color: rgb(51,51,51); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="color: #3366ff">//获取当前 Thread 的当前应用程序域的基目录，它由程序集冲突解决程序用来探测程序集。</span><br /><span style="color: #3366ff">string str = System.AppDomain.CurrentDomain.BaseDirectory;</span><br /><span style="color: #3366ff">result: X:\xxx\xxx\ (.exe文件所在的目录+&#8221;\&#8221;)</span></p>
<p style="word-spacing: 0px; font: 14px/26px Arial; text-transform: none; color: rgb(51,51,51); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="color: #3366ff">//获取和设置包含该应用程序的目录的名称。</span><br /><span style="color: #3366ff">string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;</span><br /><span style="color: #3366ff">result: X:\xxx\xxx\ (.exe文件所在的目录+&#8221;\&#8221;)</span></p>
<p style="word-spacing: 0px; font: 14px/26px Arial; text-transform: none; color: rgb(51,51,51); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="color: #3366ff">//获取启动了应用程序的可执行文件的路径，不包括可执行文件的名称。</span><br /><span style="color: #3366ff">string str = System.Windows.Forms.Application.StartupPath;</span><br /><span style="color: #3366ff">result: X:\xxx\xxx (.exe文件所在的目录)</span></p>
<p style="word-spacing: 0px; font: 14px/26px Arial; text-transform: none; color: rgb(51,51,51); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="color: #3366ff">//获取启动了应用程序的可执行文件的路径，包括可执行文件的名称。</span><br /><span style="color: #3366ff">string str = System.Windows.Forms.Application.ExecutablePath;</span><br /><span style="color: #3366ff">result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)</span></p>
<p style="word-spacing: 0px; font: 14px/26px Arial; text-transform: none; color: rgb(51,51,51); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="color: #3366ff">//获取应用程序的当前工作目录(不可靠)。</span><br /><span style="color: #3366ff">string str = System.IO.Directory.GetCurrentDirectory();</span><br /><span style="color: #3366ff">result: X:\xxx\xxx (.exe文件所在的目录)</span><br /><br /><a style="word-spacing: 0px; font: 20px/30px 'Microsoft YaHei'; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; text-decoration: none; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px" href="http://blog.csdn.net/lbuskeep/article/details/7252977"><span style="color: #3366ff">C# 获取路径中，文件名、目录、扩展名等</span></a><br /><br /></p>
<p style="word-spacing: 0px; font: 14px/26px Arial; text-transform: none; color: rgb(51,51,51); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="color: #3366ff">string path = "C:\\dir1\\dir2\\foo.txt";</span><br /><span style="color: #3366ff">string str = "GetFullPath：" + Path.GetFullPath(path) + "\r\n";</span><br /><span style="color: #3366ff">str += "GetDirectoryName：" + Path.GetDirectoryName(path) + "\r\n";</span><br /><span style="color: #3366ff">str += "GetFileName：" + Path.GetFileName(path) + "\r\n";</span><br /><span style="color: #3366ff">str += "GetFileNameWithoutExtension：" + Path.GetFileNameWithoutExtension(path) + "\r\n";</span><br /><span style="color: #3366ff">str += "GetExtension：" + Path.GetExtension(path) + "\r\n";</span><br /><span style="color: #3366ff">str += "GetPathRoot：" + Path.GetPathRoot(path) + "\r\n";</span><br /><span style="color: #3366ff">MessageBox.Show(str);</span></p>
<p style="word-spacing: 0px; font: 14px/26px Arial; text-transform: none; color: rgb(51,51,51); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"></p>
<p style="word-spacing: 0px; font: 14px/26px Arial; text-transform: none; color: rgb(51,51,51); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="color: #3366ff">结果：</span><span class="Apple-converted-space">&nbsp;<br /></span><span style="color: #3366ff">GetFullPath：C:\dir1\dir2\foo.txt</span><br /><span style="color: #3366ff">GetDirectoryName：C:\dir1\dir2</span><br /><span style="color: #3366ff">GetFileName：foo.txt</span><br /><span style="color: #3366ff">GetFileNameWithoutExtension：foo</span><br /><span style="color: #3366ff">GetExtension：.txt</span><br /><span style="color: #3366ff">GetPathRoot：C:\</span></p>
<p style="word-spacing: 0px; font: 14px/26px Arial; text-transform: none; color: rgb(51,51,51); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"></p>
<p style="word-spacing: 0px; font: 14px/26px Arial; text-transform: none; color: rgb(51,51,51); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"></p>
<p style="word-spacing: 0px; font: 14px/26px Arial; text-transform: none; color: rgb(51,51,51); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"></p>
<p style="word-spacing: 0px; font: 14px/26px Arial; text-transform: none; color: rgb(51,51,51); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="color: #3366ff">这里要说明 path 是</span><a style="color: rgb(51,102,153); text-decoration: none" href="http://www.js4j.com/tags.php?/%C8%E7%BA%CE/"><span style="color: #3366ff">如何</span></a><a style="color: rgb(51,102,153); text-decoration: none" href="http://www.js4j.com/"><img style="border-right: medium none; border-top: medium none; border-left: medium none; border-bottom: medium none" alt="C# 轻松获取路径中文件名、目录、扩展名等" src="http://www.js4j.com/www.js4j.com.gif" border="0" /></a><span style="color: #3366ff">判断目录和文件名的：它把最后一个 \ 后面的内容当作是文件名。</span><span class="Apple-converted-space">&nbsp;</span><span style="color: #3366ff">// 内容来自js4j.com//</span></p>
<ul style="word-spacing: 0px; font: 14px/26px Arial; text-transform: none; color: rgb(51,51,51); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><li><span style="color: #3366ff">C:\dir1\dir2\foo.txt 文件名是 foo.txt，目录名是 C:\dir1\dir2。</span></li><li><span style="color: #3366ff">C:\dir1\dir2\ 文件名是零长度字符串，目录名是 C:\dir1\dir2。</span></li><li><span style="color: #3366ff">C:\dir1\dir2 文件名是 dir2，目录名是 C:\dir1。</span></li></ul><img src ="http://www.blogjava.net/AndyZhang/aggbug/376358.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/AndyZhang/" target="_blank">SkyDream</a> 2012-04-23 13:39 <a href="http://www.blogjava.net/AndyZhang/archive/2012/04/23/376358.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>.NET性能分析最佳实践之：如何找出使用过多内存的.NET代码（进阶篇）</title><link>http://www.blogjava.net/AndyZhang/archive/2012/04/18/375149.html</link><dc:creator>SkyDream</dc:creator><author>SkyDream</author><pubDate>Wed, 18 Apr 2012 09:35:00 GMT</pubDate><guid>http://www.blogjava.net/AndyZhang/archive/2012/04/18/375149.html</guid><description><![CDATA[<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-size: medium">通过<a style="color: rgb(246,98,9); text-decoration: none" href="http://www.agilesharp.com/u/yanyangtian/Blog.aspx/t-225" target="_blank">上一篇</a>文章一些截图，相信朋友们已经基本知道了Profiler的使用方法，现在，如何程序中的方法很多，例如2000个，那么通过上面的方法来查看就相当的痛苦了。怎么办呢？</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-size: medium">一个比较不错的方法就是将信息导出成为报表的信息，然后在excel中进行分析。操作方法如下：</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-size: medium">&nbsp;</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><a style="color: rgb(246,98,9); text-decoration: none" href="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=250" target="_blank"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" alt="20120409212740.png" src="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=250" width="550" /></a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-size: medium">一旦上面的按钮点击之后，所有的调用树就会被显示出来，还没有将信息保存为csv格式，如下：</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-size: medium">&nbsp;</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><a style="color: rgb(246,98,9); text-decoration: none" href="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=251" target="_blank"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="409" alt="20120409212810.png" src="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=251" width="665" /></a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-size: medium">&nbsp;</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-size: medium">保存为了csv之后，我们通过excel打开，就可以进行分析了，如下：</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-size: medium">&nbsp;</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><a style="color: rgb(246,98,9); text-decoration: none" href="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=252" target="_blank"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" alt="20120409212854.png" src="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=252" width="550" /></a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-size: medium">&nbsp;</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-size: medium">&nbsp;</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-size: medium">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 除了采用上面的方法使用Profiler之外，我们还可以在代码中使用Profiler的功能。如果我们知道了哪些方法需要跟踪，我们可以在代码中开启Profiler。</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-size: medium">首先，我们要在自己的项目中引用&#8220;CLRProfilerControl.dll&#8221;，然后可以写如下的代码：</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-size: medium">&nbsp;</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><a style="color: rgb(246,98,9); text-decoration: none" href="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=253" target="_blank"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" alt="20120409212940.png" src="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=253" width="550" /></a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-size: medium">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 这样写完之后呢，我们就可以运行profiler来启动我们的程序。这里要注意，我们必须禁用&#8220;profile active&#8221;，因为我们已经运行我们的代码运行profiler的检查跟踪功能，如下：</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-size: medium">&nbsp;</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><a style="color: rgb(246,98,9); text-decoration: none" href="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=254" target="_blank"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="302" alt="20120409213130.png" src="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=254" width="640" /></a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-size: medium">此时，如果我们查看&#8220;histogram&#8221;，就会发现数据非常的少，如下：</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-size: medium">&nbsp;</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><a style="color: rgb(246,98,9); text-decoration: none" href="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=255" target="_blank"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="319" alt="20120409213147.png" src="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=255" width="602" /></a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-size: medium">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 如果我们查看&#8220;allocation graph（内存分配图）&#8221;，此时，我们就会看到一个非常干净的界面了。和之前一样，我们还是要点击&#8220;everything&#8221;来看所有的方法的占用的内存，如下：</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-size: medium">&nbsp;</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><a style="color: rgb(246,98,9); text-decoration: none" href="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=256" target="_blank"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="319" alt="20120409213215.png" src="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=256" width="590" /></a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-size: medium">到这里，Profiler的使用就介绍完了，我们总结一下吧：</span></p>
<ol style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px 0px 1.5em; word-spacing: 0px; font: 14px/1.4em Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><li style="padding-right: 0px; padding-left: 0px; font-size: 1em; padding-bottom: 0px; margin: 0px 0px 0.25em 30px; padding-top: 0px">&nbsp; 
<ol style="padding-right: 0px; padding-left: 40px; font-size: 1em; padding-bottom: 0px; margin: 1em 0px; line-height: 1.4em; padding-top: 0px"><li style="padding-right: 0px; padding-left: 0px; font-size: 1em; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px"><strong style="font-weight: bold"><span style="font-size: medium">Profiler可以用找出类，方法以及程序集占用内存的情况。</span></strong></li><li style="padding-right: 0px; padding-left: 0px; font-size: 1em; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px"><strong style="font-weight: bold"><span style="font-size: medium">不能用在已经运行的上线环境中。</span></strong></li><li style="padding-right: 0px; padding-left: 0px; font-size: 1em; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px"><strong style="font-weight: bold"><span style="font-size: medium">我们可以使用histogram查看内存的分配情况，通过call graph查看每个方法占用的内存。</span></strong></li><li style="padding-right: 0px; padding-left: 0px; font-size: 1em; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px"><strong style="font-weight: bold"><span style="font-size: medium">我们可以通过代码的方式更加有针对性的对某些方法进行查看。</span></strong></li></ol></li></ol><img src ="http://www.blogjava.net/AndyZhang/aggbug/375149.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/AndyZhang/" target="_blank">SkyDream</a> 2012-04-18 17:35 <a href="http://www.blogjava.net/AndyZhang/archive/2012/04/18/375149.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>.NET性能分析最佳实践之：如何找出使用过多内存的.NET代码（基础篇）</title><link>http://www.blogjava.net/AndyZhang/archive/2012/04/18/375147.html</link><dc:creator>SkyDream</dc:creator><author>SkyDream</author><pubDate>Wed, 18 Apr 2012 09:27:00 GMT</pubDate><guid>http://www.blogjava.net/AndyZhang/archive/2012/04/18/375147.html</guid><description><![CDATA[<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">在.NET应用中一个常常影响性能的因素就是代码消耗了过多的内存。很多的开发人员在编写代码的过程中常常不会关注性能，从而使得应用程序中到处存在性能瓶颈。很多的时候，开发人员关注的总是代码的执行时间的长短，而把真正的性能问题丢掉了一边。在本篇文章中，我们将会找出代码中的哪些功能消耗了多少内存。</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">本篇文章比较简单，我们会主要详细的介绍CLR Profiler这个工具。</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">&nbsp;</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><strong style="padding-right: 0px; padding-left: 0px; font-weight: bold; padding-bottom: 0px; margin: 0px; padding-top: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; color: rgb(255,0,0); line-height: 1.5; padding-top: 0px">基础篇：详解介绍Profiler的使用</span></strong></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: small; padding-bottom: 0px; margin: 0px; padding-top: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 大家可以从这里从微软的</span><a style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; color: rgb(7,93,179); padding-top: 0px; text-decoration: underline" href="http://www.microsoft.com/downloads/details.aspx?familyid=A362781C-3870-43BE-8926-862B40AA0CD0&amp;displaylang=en"><span style="padding-right: 0px; padding-left: 0px; font-size: small; padding-bottom: 0px; margin: 0px; padding-top: 0px">站点</span></a><span style="padding-right: 0px; padding-left: 0px; font-size: small; padding-bottom: 0px; margin: 0px; padding-top: 0px">下载CLR Profiler。一旦下载CLR Profiler之后，我们就可以解压，然后运行Bin文件夹中的&#8220;CLRProfiler.exe&#8221;。</span></span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">首先我们来介绍一下CLR Profiler的功能。</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: small; padding-bottom: 0px; margin: 0px; padding-top: 0px"><span style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CLR Profiler确实是一个不错的工具，通过使用它，我们可以了解一个.NET应用程序到底是如何使用内存的，基本上面它的功能可以分为两类,</span><span style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">如图所示：</span></span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">&nbsp;</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><a style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; color: rgb(7,93,179); padding-top: 0px; text-decoration: underline" href="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=236" target="_blank"><img style="border-right: 0px; padding-right: 0px; border-top: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; border-left: 0px; padding-top: 0px; border-bottom: 0px" alt="20120409210650.png" src="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=236" width="550" /></a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">1. 对一个.NET应用中的内存是如何分配的给出一个完整的描述。所以，我们可以看到每一种类型，方法所占用的内存情况。</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">2. 它告诉我们一个方法被调用了多少次。</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 这里需要注意的就是：不要再生产环境或上面的服务器站点中运行CLR Profiler，因为它会严重的影响程序的影响。例如，如果我们的应用程序中包含两个方法，fun1和fun2，此时当我们运行CLR Profiler的时候，它会将一些逻辑注入到应用程序中，通过下面的一个图就可以很清楚的看出这个问题：</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><a style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; color: rgb(7,93,179); padding-top: 0px; text-decoration: underline" href="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=237" target="_blank"><img style="border-right: 0px; padding-right: 0px; border-top: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; border-left: 0px; padding-top: 0px; border-bottom: 0px" alt="20120409210755.png" src="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=237" width="550" /></a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">&nbsp;</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">说了这么多，我们还是来看看，如何使用CLR Profiler。</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 首先，在使用CLR Profiler之前，我们要清楚：要使用这个工具来干嘛？基本上，有两点理由：查看内存的分配与使用的情况；查看方法被调用次数。</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">启动了CRLProfiler.exe之后，选择要检查的程序，如下图：</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">&nbsp;</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><a style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; color: rgb(7,93,179); padding-top: 0px; text-decoration: underline" href="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=238" target="_blank"><img style="border-right: 0px; padding-right: 0px; border-top: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; border-left: 0px; padding-top: 0px; border-bottom: 0px" height="357" alt="20120409210842.png" src="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=238" width="678" /></a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">此时，应用程序就开始运行，我们就可以在Profiler中看到一些信息，例如应用程序占用的内容，每一代对象占用的大小等，如下：</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: small; padding-bottom: 0px; margin: 0px; padding-top: 0px">&nbsp;</span><a style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; color: rgb(7,93,179); padding-top: 0px; text-decoration: underline" href="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=239" target="_blank"><span style="padding-right: 0px; padding-left: 0px; font-size: small; padding-bottom: 0px; margin: 0px; padding-top: 0px"><img style="border-right: 0px; padding-right: 0px; border-top: 0px; display: block; padding-left: 0px; padding-bottom: 0px; margin: 0px auto; border-left: 0px; padding-top: 0px; border-bottom: 0px" height="345" alt="20120409210929.png" src="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=239" width="596" /></span></a></span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 其实上面的那个应用程序非常的简单，只是作为一个demo演示而言。上面的程序的功能就是在一个按钮的事件中调用了两个方法：<strong style="padding-right: 0px; padding-left: 0px; font-weight: bold; padding-bottom: 0px; margin: 0px; padding-top: 0px">UseSimpleStrings</strong>和<strong style="padding-right: 0px; padding-left: 0px; font-weight: bold; padding-bottom: 0px; margin: 0px; padding-top: 0px">UseStringBuilders</strong>。这个两个方法都是在拼接字符串（这也是常常被用来做例子的一个场景），我们分别让它们拼接1000个字符串，如下：</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">&nbsp;</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><a style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; color: rgb(7,93,179); padding-top: 0px; text-decoration: underline" href="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=240" target="_blank"><img style="border-right: 0px; padding-right: 0px; border-top: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; border-left: 0px; padding-top: 0px; border-bottom: 0px" height="318" alt="20120409211022.png" src="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=240" width="609" /></a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">其中UseSimpleStrings如下：</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">&nbsp;</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><a style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; color: rgb(7,93,179); padding-top: 0px; text-decoration: underline" href="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=241" target="_blank"><img style="border-right: 0px; padding-right: 0px; border-top: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; border-left: 0px; padding-top: 0px; border-bottom: 0px" alt="20120409211053.png" src="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=241" width="550" /></a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">UseStringBuilders如下：</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">&nbsp;</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><a style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; color: rgb(7,93,179); padding-top: 0px; text-decoration: underline" href="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=242" target="_blank"><img style="border-right: 0px; padding-right: 0px; border-top: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; border-left: 0px; padding-top: 0px; border-bottom: 0px" alt="20120409211127.png" src="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=242" width="550" /></a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 现在，我们可以尝试着使用Profiler去看看每个方法使用多少的内存。我们在程序中点击按钮，好让Profiler去收集信息。然后，我们在点击Profiler的&#8220;histogram&#8221;按钮，此时我们就可以看到程序中的每一种类型的内存占用情况，如下：</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">&nbsp;</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><a style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; color: rgb(7,93,179); padding-top: 0px; text-decoration: underline" href="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=243" target="_blank"><img style="border-right: 0px; padding-right: 0px; border-top: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; border-left: 0px; padding-top: 0px; border-bottom: 0px" height="406" alt="20120409211321.png" src="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=243" width="695" /></a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">如果我们想看每一个方法在运行过程中占用的内容，可以点击&#8220;Allocation Graph&#8221;，如下：</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">&nbsp;</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><a style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; color: rgb(7,93,179); padding-top: 0px; text-decoration: underline" href="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=244" target="_blank"><img style="border-right: 0px; padding-right: 0px; border-top: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; border-left: 0px; padding-top: 0px; border-bottom: 0px" height="149" alt="20120409211413.png" src="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=244" width="592" /></a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">通过这个操作，我们可以看到下面的图：</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">&nbsp;</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><a style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; color: rgb(7,93,179); padding-top: 0px; text-decoration: underline" href="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=245" target="_blank"><img style="border-right: 0px; padding-right: 0px; border-top: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; border-left: 0px; padding-top: 0px; border-bottom: 0px" height="392" alt="20120409211458.png" src="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=245" width="675" /></a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 乍一看，可能感觉界面非常的混乱，特别是在应用程序很大的时候。为了更加看到我们想看的方法，我们可以通过在界面的中点击右键，点击&#8220;Find Routine&#8221;，然后选择输入过滤的条件，如下：</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">&nbsp;</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><a style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; color: rgb(7,93,179); padding-top: 0px; text-decoration: underline" href="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=246" target="_blank"><img style="border-right: 0px; padding-right: 0px; border-top: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; border-left: 0px; padding-top: 0px; border-bottom: 0px" height="166" alt="20120409211523.png" src="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=246" width="607" /></a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">这样就可以快速的定位，如下：</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">&nbsp;</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><a style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; color: rgb(7,93,179); padding-top: 0px; text-decoration: underline" href="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=247" target="_blank"><img style="border-right: 0px; padding-right: 0px; border-top: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; border-left: 0px; padding-top: 0px; border-bottom: 0px" height="265" alt="20120409211559.png" src="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=247" width="633" /></a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">然后在方法上面双击，就进一步的展开，查看细节，如下：</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">&nbsp;</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><a style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; color: rgb(7,93,179); padding-top: 0px; text-decoration: underline" href="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=248" target="_blank"><img style="border-right: 0px; padding-right: 0px; border-top: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; border-left: 0px; padding-top: 0px; border-bottom: 0px" height="184" alt="20120409211642.png" src="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=248" width="657" /></a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在上图中，我们只是看到了&#8220;UseSimpleStrings&#8221;方法的使用内存的情况，因为这个界面显示的信息比较的粗糙，只是把一些内存使用比较多的方法列出来了，我们可以通过点击&#8220;0(everything)&#8221;查看所以的方法，此时看到如下：</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">&nbsp;</span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><a style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; color: rgb(7,93,179); padding-top: 0px; text-decoration: underline" href="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=249" target="_blank"><img style="border-right: 0px; padding-right: 0px; border-top: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; border-left: 0px; padding-top: 0px; border-bottom: 0px" height="321" alt="20120409211717.png" src="http://www.agilesharp.com/Services/BlogAttachment.ashx?AttachmentID=249" width="607" /></a></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="padding-right: 0px; padding-left: 0px; font-size: medium; padding-bottom: 0px; margin: 0px; line-height: 1.5; padding-top: 0px">通过上面的图，我们一目了然的直到内存的使用情况</span></p> <img src ="http://www.blogjava.net/AndyZhang/aggbug/375147.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/AndyZhang/" target="_blank">SkyDream</a> 2012-04-18 17:27 <a href="http://www.blogjava.net/AndyZhang/archive/2012/04/18/375147.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>经典SQL语句大全</title><link>http://www.blogjava.net/AndyZhang/archive/2012/04/17/374867.html</link><dc:creator>SkyDream</dc:creator><author>SkyDream</author><pubDate>Tue, 17 Apr 2012 03:00:00 GMT</pubDate><guid>http://www.blogjava.net/AndyZhang/archive/2012/04/17/374867.html</guid><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 下列语句部分是Mssql语句，不可以在access中使用。&nbsp;　　SQL分类：　　DDL&#8212;数据定义语言(CREATE，ALTER，DROP，DECLARE)&nbsp;　　DML&#8212;数据操纵语言(SELECT，DELETE，UPDATE，INSERT)&nbsp;　　DCL&#8212;数据控制语言(GRANT，REVOKE，COMMIT，ROLLBACK)　　首先,...&nbsp;&nbsp;<a href='http://www.blogjava.net/AndyZhang/archive/2012/04/17/374867.html'>阅读全文</a><img src ="http://www.blogjava.net/AndyZhang/aggbug/374867.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/AndyZhang/" target="_blank">SkyDream</a> 2012-04-17 11:00 <a href="http://www.blogjava.net/AndyZhang/archive/2012/04/17/374867.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>