<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>.NET开发者</title>
	<atom:link href="http://www.dotnetdev.cn/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dotnetdev.cn</link>
	<description>专注.NET技术</description>
	<lastBuildDate>Tue, 07 Jun 2011 00:58:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>MD5的16位和32位加密算法</title>
		<link>http://www.dotnetdev.cn/2011/05/md5%e7%9a%8416%e4%bd%8d%e5%92%8c32%e4%bd%8d%e5%8a%a0%e5%af%86%e7%ae%97%e6%b3%95/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=md5%25e7%259a%258416%25e4%25bd%258d%25e5%2592%258c32%25e4%25bd%258d%25e5%258a%25a0%25e5%25af%2586%25e7%25ae%2597%25e6%25b3%2595</link>
		<comments>http://www.dotnetdev.cn/2011/05/md5%e7%9a%8416%e4%bd%8d%e5%92%8c32%e4%bd%8d%e5%8a%a0%e5%af%86%e7%ae%97%e6%b3%95/#comments</comments>
		<pubDate>Fri, 20 May 2011 07:24:48 +0000</pubDate>
		<dc:creator>xsi640</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.dotnetdev.cn/?p=676</guid>
		<description><![CDATA[MD5是一种常用的加密算法，是不可逆的。经常用在登录密码的加密等。下面给出两种加密算法，很简单，直接看代码。 No related posts.
No related posts.]]></description>
			<content:encoded><![CDATA[<p>MD5是一种常用的加密算法，是不可逆的。经常用在登录密码的加密等。下面给出两种加密算法，很简单，直接看代码。</p>
<pre class="brush: csharp; title: ; notranslate">/// &lt;summary&gt;
/// MD5 16位加密算法
/// &lt;/summary&gt;
/// &lt;param name=&quot;source&quot;&gt;&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
private static string ConvertMd5By16Bit(string source)
{
    MD5 md5 = MD5.Create();
    byte[] bs = Encoding.UTF8.GetBytes(source);
    bs = md5.ComputeHash(bs);
    return BitConverter.ToString(bs, 4, 8).Replace(&quot;-&quot;, &quot;&quot;).ToLower();
}
/// &lt;summary&gt;
/// MD5 32位加密算法
/// &lt;/summary&gt;
/// &lt;param name=&quot;source&quot;&gt;&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
private static string ConvertMd5By32Bit(string source)
{
    MD5 md5 = MD5.Create();
    byte[] bs = Encoding.UTF8.GetBytes(source);
    bs = md5.ComputeHash(bs);
    return BitConverter.ToString(bs).Replace(&quot;-&quot;, &quot;&quot;).ToLower();
}</pre>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetdev.cn/2011/05/md5%e7%9a%8416%e4%bd%8d%e5%92%8c32%e4%bd%8d%e5%8a%a0%e5%af%86%e7%ae%97%e6%b3%95/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>全浏览器兼容的Copy复制到剪贴板js</title>
		<link>http://www.dotnetdev.cn/2011/05/%e5%85%a8%e6%b5%8f%e8%a7%88%e5%99%a8%e5%85%bc%e5%ae%b9%e7%9a%84copy%e5%a4%8d%e5%88%b6%e5%88%b0%e5%89%aa%e8%b4%b4%e6%9d%bfjs/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=%25e5%2585%25a8%25e6%25b5%258f%25e8%25a7%2588%25e5%2599%25a8%25e5%2585%25bc%25e5%25ae%25b9%25e7%259a%2584copy%25e5%25a4%258d%25e5%2588%25b6%25e5%2588%25b0%25e5%2589%25aa%25e8%25b4%25b4%25e6%259d%25bfjs</link>
		<comments>http://www.dotnetdev.cn/2011/05/%e5%85%a8%e6%b5%8f%e8%a7%88%e5%99%a8%e5%85%bc%e5%ae%b9%e7%9a%84copy%e5%a4%8d%e5%88%b6%e5%88%b0%e5%89%aa%e8%b4%b4%e6%9d%bfjs/#comments</comments>
		<pubDate>Fri, 20 May 2011 06:46:44 +0000</pubDate>
		<dc:creator>xsi640</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[copy]]></category>

		<guid isPermaLink="false">http://www.dotnetdev.cn/?p=668</guid>
		<description><![CDATA[我们知道在IE、火狐浏览器中点击某个按钮复制一段文本，有时会有弹出提示或不起作用的时候，今天，我们就来解决这个问题。 原理：借助Flash的力量，将内容复制到剪贴板。 由于本人不会Flash就在网上找到了一个类库，帮我们完成这个操作。Zero Clipboard 先看一下最后实现的效果。 代码： 具体说明，直接看注释吧。代码下载 Related posts: Javascript中的Json序列化和反序列化 javascript中的prototype属性。
Related posts:<ol>
<li><a href='http://www.dotnetdev.cn/2010/01/javascript%e4%b8%ad%e7%9a%84json%e5%ba%8f%e5%88%97%e5%8c%96%e5%92%8c%e5%8f%8d%e5%ba%8f%e5%88%97%e5%8c%96/' rel='bookmark' title='Javascript中的Json序列化和反序列化'>Javascript中的Json序列化和反序列化</a></li>
<li><a href='http://www.dotnetdev.cn/2011/03/javascript%e4%b8%ad%e7%9a%84prototype%e5%b1%9e%e6%80%a7%e3%80%82/' rel='bookmark' title='javascript中的prototype属性。'>javascript中的prototype属性。</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>我们知道在IE、火狐浏览器中点击某个按钮复制一段文本，有时会有弹出提示或不起作用的时候，今天，我们就来解决这个问题。</p>
<p>原理：借助Flash的力量，将内容复制到剪贴板。</p>
<p>由于本人不会Flash就在网上找到了一个类库，帮我们完成这个操作。<a title="Zero Clipboard" href="http://code.google.com/p/zeroclipboard/" target="_blank">Zero Clipboard</a></p>
<p>先看一下最后实现的效果。</p>
<p><a href="http://www.dotnetdev.cn/wp-content/uploads/2011/05/02.png" rel="lightbox[668]"><img class="alignnone size-full wp-image-669" title="全浏览器兼容的Copy" src="http://www.dotnetdev.cn/wp-content/uploads/2011/05/02.png" alt="" width="312" height="265" /></a></p>
<p>代码：</p>
<pre class="brush: csharp; title: ; notranslate">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;New Document &lt;/title&gt;
    &lt;script type=&quot;text/javascript&quot; src=&quot;ZeroClipboard.js&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/javascript&quot;&gt;
        window.onload = function () {
            //创建ZeroClipboard对象
            var clip = new ZeroClipboard.Client();
            //设置鼠标停留在复制按钮上是手型
            clip.setHandCursor(true);
            //鼠标按下时，复制到剪贴板。。。ps:很费解，为什么不是click非是mouseOver
            clip.addEventListener('mouseOver', function (client) {
                clip.setText(document.getElementById('inputText').value);
            });
            //设置复制到剪贴板完成时，输出
            clip.addEventListener('complete', function (client, text) {
                alert(&quot;复制成功!:\n&quot; + text);
            });
            //设置按钮的Dom对象
            clip.glue('btnCopy');
        }
    &lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;div id=&quot;flash_copy&quot;&gt;&lt;/div&gt;
    &lt;input type=&quot;text&quot; id=&quot;inputText&quot; /&gt;
    &lt;input type=&quot;button&quot; value=&quot;复制到剪贴板&quot; id=&quot;btnCopy&quot; /&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
<p>具体说明，直接看注释吧。<a title="全浏览器兼容的Copy复制到剪贴板JS" href="http://dl.dbank.com/c02jm3q5g2" target="_blank">代码下载</a></p>
<p>Related posts:<ol>
<li><a href='http://www.dotnetdev.cn/2010/01/javascript%e4%b8%ad%e7%9a%84json%e5%ba%8f%e5%88%97%e5%8c%96%e5%92%8c%e5%8f%8d%e5%ba%8f%e5%88%97%e5%8c%96/' rel='bookmark' title='Javascript中的Json序列化和反序列化'>Javascript中的Json序列化和反序列化</a></li>
<li><a href='http://www.dotnetdev.cn/2011/03/javascript%e4%b8%ad%e7%9a%84prototype%e5%b1%9e%e6%80%a7%e3%80%82/' rel='bookmark' title='javascript中的prototype属性。'>javascript中的prototype属性。</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetdev.cn/2011/05/%e5%85%a8%e6%b5%8f%e8%a7%88%e5%99%a8%e5%85%bc%e5%ae%b9%e7%9a%84copy%e5%a4%8d%e5%88%b6%e5%88%b0%e5%89%aa%e8%b4%b4%e6%9d%bfjs/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>HttpWebRequest方式下载文件</title>
		<link>http://www.dotnetdev.cn/2011/05/httpwebrequest%e6%96%b9%e5%bc%8f%e4%b8%8b%e8%bd%bd%e6%96%87%e4%bb%b6/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=httpwebrequest%25e6%2596%25b9%25e5%25bc%258f%25e4%25b8%258b%25e8%25bd%25bd%25e6%2596%2587%25e4%25bb%25b6</link>
		<comments>http://www.dotnetdev.cn/2011/05/httpwebrequest%e6%96%b9%e5%bc%8f%e4%b8%8b%e8%bd%bd%e6%96%87%e4%bb%b6/#comments</comments>
		<pubDate>Mon, 09 May 2011 07:33:17 +0000</pubDate>
		<dc:creator>xsi640</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[下载]]></category>

		<guid isPermaLink="false">http://www.dotnetdev.cn/?p=666</guid>
		<description><![CDATA[就是使用HttpWebRequest和HttpWebResponse两个对象下载文件，很容易看懂。 Related posts: C#使用SharpZipLib类库压缩、解压缩单个文件 C#中的扩展方法 用C#得到访问IP出口的网卡
Related posts:<ol>
<li><a href='http://www.dotnetdev.cn/2011/04/c%e4%bd%bf%e7%94%a8sharpziplib%e7%b1%bb%e5%ba%93%e5%8e%8b%e7%bc%a9%e3%80%81%e8%a7%a3%e5%8e%8b%e7%bc%a9%e5%8d%95%e4%b8%aa%e6%96%87%e4%bb%b6/' rel='bookmark' title='C#使用SharpZipLib类库压缩、解压缩单个文件'>C#使用SharpZipLib类库压缩、解压缩单个文件</a></li>
<li><a href='http://www.dotnetdev.cn/2011/04/c%e4%b8%ad%e7%9a%84%e6%89%a9%e5%b1%95%e6%96%b9%e6%b3%95/' rel='bookmark' title='C#中的扩展方法'>C#中的扩展方法</a></li>
<li><a href='http://www.dotnetdev.cn/2011/04/%e7%94%a8c%e5%be%97%e5%88%b0%e8%ae%bf%e9%97%aeip%e5%87%ba%e5%8f%a3%e7%9a%84%e7%bd%91%e5%8d%a1/' rel='bookmark' title='用C#得到访问IP出口的网卡'>用C#得到访问IP出口的网卡</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>就是使用HttpWebRequest和HttpWebResponse两个对象下载文件，很容易看懂。</p>
<pre class="brush: csharp; title: ; notranslate">/// &lt;summary&gt;
/// 下载文件并保存到本地目录。
/// &lt;/summary&gt;
/// &lt;param name=&quot;url&quot;&gt;&lt;/param&gt;
/// &lt;param name=&quot;localPath&quot;&gt;&lt;/param&gt;
/// &lt;param name=&quot;webProxy&quot;&gt;&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
public static bool DownloadFile(string url, string localPath, IWebProxy webProxy)
{
    bool flag = false;
    try
    {
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
        req.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
        req.Proxy = webProxy;
        HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
        long totalBytes = resp.ContentLength;
        using (Stream sResp = resp.GetResponseStream())
        {
            if (!Directory.Exists(localPath.Substring(0, localPath.LastIndexOf('\\'))))
            {
                Directory.CreateDirectory(localPath.Substring(0, localPath.LastIndexOf('\\')));
            }
            using (Stream sFile = new FileStream(localPath, FileMode.Create))
            {
                long totalDownloadBytes = 0;
                byte[] bs = new byte[1024];
                for (int size = sResp.Read(bs, 0, bs.Length); size &gt; 0; size = sResp.Read(bs, 0, bs.Length))
                {
                    totalDownloadBytes += size;
                    sFile.Write(bs, 0, size);
                }
            }
        }
        flag = true;
    }
    catch (Exception ex)
    {
        throw ex;
    }
    return flag;
}</pre>
<p>Related posts:<ol>
<li><a href='http://www.dotnetdev.cn/2011/04/c%e4%bd%bf%e7%94%a8sharpziplib%e7%b1%bb%e5%ba%93%e5%8e%8b%e7%bc%a9%e3%80%81%e8%a7%a3%e5%8e%8b%e7%bc%a9%e5%8d%95%e4%b8%aa%e6%96%87%e4%bb%b6/' rel='bookmark' title='C#使用SharpZipLib类库压缩、解压缩单个文件'>C#使用SharpZipLib类库压缩、解压缩单个文件</a></li>
<li><a href='http://www.dotnetdev.cn/2011/04/c%e4%b8%ad%e7%9a%84%e6%89%a9%e5%b1%95%e6%96%b9%e6%b3%95/' rel='bookmark' title='C#中的扩展方法'>C#中的扩展方法</a></li>
<li><a href='http://www.dotnetdev.cn/2011/04/%e7%94%a8c%e5%be%97%e5%88%b0%e8%ae%bf%e9%97%aeip%e5%87%ba%e5%8f%a3%e7%9a%84%e7%bd%91%e5%8d%a1/' rel='bookmark' title='用C#得到访问IP出口的网卡'>用C#得到访问IP出口的网卡</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetdev.cn/2011/05/httpwebrequest%e6%96%b9%e5%bc%8f%e4%b8%8b%e8%bd%bd%e6%96%87%e4%bb%b6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>为Windows添加日志事件</title>
		<link>http://www.dotnetdev.cn/2011/05/%e4%b8%bawindows%e6%b7%bb%e5%8a%a0%e6%97%a5%e5%bf%97%e4%ba%8b%e4%bb%b6/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=%25e4%25b8%25bawindows%25e6%25b7%25bb%25e5%258a%25a0%25e6%2597%25a5%25e5%25bf%2597%25e4%25ba%258b%25e4%25bb%25b6</link>
		<comments>http://www.dotnetdev.cn/2011/05/%e4%b8%bawindows%e6%b7%bb%e5%8a%a0%e6%97%a5%e5%bf%97%e4%ba%8b%e4%bb%b6/#comments</comments>
		<pubDate>Fri, 06 May 2011 06:55:53 +0000</pubDate>
		<dc:creator>xsi640</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[日志]]></category>

		<guid isPermaLink="false">http://www.dotnetdev.cn/?p=657</guid>
		<description><![CDATA[废话不说了，大家直接看代码就明白了。 事件查看器结果： 这样，我们可以很方便的将我们的应用程序日志加到系统中，方便管理和查看 Related posts: 让.Net程序会说话！
Related posts:<ol>
<li><a href='http://www.dotnetdev.cn/2011/04/%e8%ae%a9-net%e7%a8%8b%e5%ba%8f%e4%bc%9a%e8%af%b4%e8%af%9d%ef%bc%81/' rel='bookmark' title='让.Net程序会说话！'>让.Net程序会说话！</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>废话不说了，大家直接看代码就明白了。</p>
<pre class="brush: csharp; title: ; notranslate">using (EventLog log = new EventLog())   //初始化EventLog实例
{
    log.Source = &quot;.Net开发者&quot;; //设置日志的来源
    log.Log = &quot;.Net开发者&quot;;    //设置日志的名称

    //写入日志
    log.WriteEntry(&quot;欢迎光临 .Net开发者!&quot;,      //日志内容
        EventLogEntryType.Information);         //日志级别
}</pre>
<p>事件查看器结果：</p>
<p><a href="http://www.dotnetdev.cn/wp-content/uploads/2011/05/01.png" rel="lightbox"><img class="alignnone size-thumbnail wp-image-658" title="为Windows添加日志事件" src="http://www.dotnetdev.cn/wp-content/uploads/2011/05/01-150x150.png" alt="" width="150" height="150" /></a></p>
<p>这样，我们可以很方便的将我们的应用程序日志加到系统中，方便管理和查看</p>
<p>Related posts:<ol>
<li><a href='http://www.dotnetdev.cn/2011/04/%e8%ae%a9-net%e7%a8%8b%e5%ba%8f%e4%bc%9a%e8%af%b4%e8%af%9d%ef%bc%81/' rel='bookmark' title='让.Net程序会说话！'>让.Net程序会说话！</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetdev.cn/2011/05/%e4%b8%bawindows%e6%b7%bb%e5%8a%a0%e6%97%a5%e5%bf%97%e4%ba%8b%e4%bb%b6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C#使用SharpZipLib类库压缩、解压缩单个文件</title>
		<link>http://www.dotnetdev.cn/2011/04/c%e4%bd%bf%e7%94%a8sharpziplib%e7%b1%bb%e5%ba%93%e5%8e%8b%e7%bc%a9%e3%80%81%e8%a7%a3%e5%8e%8b%e7%bc%a9%e5%8d%95%e4%b8%aa%e6%96%87%e4%bb%b6/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=c%25e4%25bd%25bf%25e7%2594%25a8sharpziplib%25e7%25b1%25bb%25e5%25ba%2593%25e5%258e%258b%25e7%25bc%25a9%25e3%2580%2581%25e8%25a7%25a3%25e5%258e%258b%25e7%25bc%25a9%25e5%258d%2595%25e4%25b8%25aa%25e6%2596%2587%25e4%25bb%25b6</link>
		<comments>http://www.dotnetdev.cn/2011/04/c%e4%bd%bf%e7%94%a8sharpziplib%e7%b1%bb%e5%ba%93%e5%8e%8b%e7%bc%a9%e3%80%81%e8%a7%a3%e5%8e%8b%e7%bc%a9%e5%8d%95%e4%b8%aa%e6%96%87%e4%bb%b6/#comments</comments>
		<pubDate>Wed, 27 Apr 2011 01:39:19 +0000</pubDate>
		<dc:creator>xsi640</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[压缩]]></category>

		<guid isPermaLink="false">http://www.dotnetdev.cn/?p=655</guid>
		<description><![CDATA[C#使用SharpZipLib类库压缩、解压缩单个文件，废话不说了，直接看代码吧， 类库下载地址：http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx Related posts: GZip和Deflate两种数据压缩算法 HttpWebRequest方式下载文件 C#中的扩展方法
Related posts:<ol>
<li><a href='http://www.dotnetdev.cn/2011/03/gzip%e5%92%8cdeflate%e4%b8%a4%e7%a7%8d%e6%95%b0%e6%8d%ae%e5%8e%8b%e7%bc%a9%e7%ae%97%e6%b3%95/' rel='bookmark' title='GZip和Deflate两种数据压缩算法'>GZip和Deflate两种数据压缩算法</a></li>
<li><a href='http://www.dotnetdev.cn/2011/05/httpwebrequest%e6%96%b9%e5%bc%8f%e4%b8%8b%e8%bd%bd%e6%96%87%e4%bb%b6/' rel='bookmark' title='HttpWebRequest方式下载文件'>HttpWebRequest方式下载文件</a></li>
<li><a href='http://www.dotnetdev.cn/2011/04/c%e4%b8%ad%e7%9a%84%e6%89%a9%e5%b1%95%e6%96%b9%e6%b3%95/' rel='bookmark' title='C#中的扩展方法'>C#中的扩展方法</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>C#使用SharpZipLib类库压缩、解压缩单个文件，废话不说了，直接看代码吧，</p>
<p>类库下载地址：http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx</p>
<pre class="brush: csharp; title: ; notranslate">/// &lt;summary&gt;
/// 使用SharpZipLib压缩Zip文件
/// &lt;/summary&gt;
/// &lt;param name=&quot;srcFile&quot;&gt;源文件&lt;/param&gt;
/// &lt;param name=&quot;dstFile&quot;&gt;压缩后的Zip文件&lt;/param&gt;
/// &lt;param name=&quot;bufferSize&quot;&gt;缓冲大小&lt;/param&gt;
public static void Zip(string srcFile, string dstFile, int bufferSize)
{
    using (FileStream fileStreamIn = new FileStream(srcFile, FileMode.Open, FileAccess.Read))
    {
        using (FileStream fileStreamOut = new FileStream(dstFile, FileMode.Create, FileAccess.Write))
        {
            using (ZipOutputStream zipOutStream = new ZipOutputStream(fileStreamOut))
            {
                byte[] buffer = new byte[bufferSize];
                ZipEntry entry = new ZipEntry(Path.GetFileName(srcFile));
                zipOutStream.PutNextEntry(entry);
                int size;
                do
                {
                    size = fileStreamIn.Read(buffer, 0, buffer.Length);
                    zipOutStream.Write(buffer, 0, size);
                } while (size &gt; 0);
                zipOutStream.Flush();
            }
        }
    }
}

/// &lt;summary&gt;
/// 使用SharpZipLib解压缩Zip文件
/// &lt;/summary&gt;
/// &lt;param name=&quot;srcFile&quot;&gt;Zip源文件&lt;/param&gt;
/// &lt;param name=&quot;dstFile&quot;&gt;解压出来的文件&lt;/param&gt;
/// &lt;param name=&quot;bufferSize&quot;&gt;缓冲大小&lt;/param&gt;
public static void UnZip(string srcFile, string dstFile, int bufferSize)
{
    using (FileStream fileStreamIn = new FileStream(srcFile, FileMode.Open, FileAccess.Read))
    {
        using (ZipInputStream zipInStream = new ZipInputStream(fileStreamIn))
        {
            ZipEntry entry = zipInStream.GetNextEntry();
            using (FileStream fileStreamOut = new FileStream(dstFile + @&quot;\&quot; + entry.Name, FileMode.Create, FileAccess.Write))
            {
                int size;
                byte[] buffer = new byte[bufferSize];
                do
                {
                    size = zipInStream.Read(buffer, 0, buffer.Length);
                    fileStreamOut.Write(buffer, 0, size);
                } while (size &gt; 0);
                fileStreamOut.Flush();
            }
        }
    }
}

/// &lt;summary&gt;
/// 测试Zip文件是否完整
/// &lt;/summary&gt;
/// &lt;param name=&quot;srcFile&quot;&gt;Zip源文件&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
public static bool Test(string srcFile)
{
    bool flag = false;
    using (ZipFile zf = new ZipFile(srcFile))
    {
        flag = zf.TestArchive(true);
    }
    return flag;
}</pre>
<p>Related posts:<ol>
<li><a href='http://www.dotnetdev.cn/2011/03/gzip%e5%92%8cdeflate%e4%b8%a4%e7%a7%8d%e6%95%b0%e6%8d%ae%e5%8e%8b%e7%bc%a9%e7%ae%97%e6%b3%95/' rel='bookmark' title='GZip和Deflate两种数据压缩算法'>GZip和Deflate两种数据压缩算法</a></li>
<li><a href='http://www.dotnetdev.cn/2011/05/httpwebrequest%e6%96%b9%e5%bc%8f%e4%b8%8b%e8%bd%bd%e6%96%87%e4%bb%b6/' rel='bookmark' title='HttpWebRequest方式下载文件'>HttpWebRequest方式下载文件</a></li>
<li><a href='http://www.dotnetdev.cn/2011/04/c%e4%b8%ad%e7%9a%84%e6%89%a9%e5%b1%95%e6%96%b9%e6%b3%95/' rel='bookmark' title='C#中的扩展方法'>C#中的扩展方法</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetdev.cn/2011/04/c%e4%bd%bf%e7%94%a8sharpziplib%e7%b1%bb%e5%ba%93%e5%8e%8b%e7%bc%a9%e3%80%81%e8%a7%a3%e5%8e%8b%e7%bc%a9%e5%8d%95%e4%b8%aa%e6%96%87%e4%bb%b6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C#中的延时加载Lazy(Of T)</title>
		<link>http://www.dotnetdev.cn/2011/04/c%e4%b8%ad%e7%9a%84%e5%bb%b6%e6%97%b6%e5%8a%a0%e8%bd%bdlazyof-t/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=c%25e4%25b8%25ad%25e7%259a%2584%25e5%25bb%25b6%25e6%2597%25b6%25e5%258a%25a0%25e8%25bd%25bdlazyof-t</link>
		<comments>http://www.dotnetdev.cn/2011/04/c%e4%b8%ad%e7%9a%84%e5%bb%b6%e6%97%b6%e5%8a%a0%e8%bd%bdlazyof-t/#comments</comments>
		<pubDate>Tue, 26 Apr 2011 07:25:56 +0000</pubDate>
		<dc:creator>xsi640</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[延时加载]]></category>

		<guid isPermaLink="false">http://www.dotnetdev.cn/?p=650</guid>
		<description><![CDATA[.Net提供了一个叫Lazy&#60;T&#62;的对象，可以让我们很方便的延时创建大型或消耗资源的对象，可以很好的提高应用程序的性能。 如何实现呢？看下面代码： 运行结果： 默认设置下，这个类的所有成员都是线程安全的。我们可以很方便的使用这个类实现对某个对象的延迟加载。 Related posts: C#中的扩展方法 .net 4.0新特性之ExpandoObject 使用SqlBulkCopy提高批量插入数据库性能
Related posts:<ol>
<li><a href='http://www.dotnetdev.cn/2011/04/c%e4%b8%ad%e7%9a%84%e6%89%a9%e5%b1%95%e6%96%b9%e6%b3%95/' rel='bookmark' title='C#中的扩展方法'>C#中的扩展方法</a></li>
<li><a href='http://www.dotnetdev.cn/2010/08/net-4-0%e6%96%b0%e7%89%b9%e6%80%a7%e4%b9%8bexpandoobject/' rel='bookmark' title='.net 4.0新特性之ExpandoObject'>.net 4.0新特性之ExpandoObject</a></li>
<li><a href='http://www.dotnetdev.cn/2011/04/%e4%bd%bf%e7%94%a8sqlbulkcopy%e6%8f%90%e9%ab%98%e6%89%b9%e9%87%8f%e6%8f%92%e5%85%a5%e6%95%b0%e6%8d%ae%e5%ba%93%e6%80%a7%e8%83%bd/' rel='bookmark' title='使用SqlBulkCopy提高批量插入数据库性能'>使用SqlBulkCopy提高批量插入数据库性能</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>.Net提供了一个叫Lazy&lt;T&gt;的对象，可以让我们很方便的延时创建大型或消耗资源的对象，可以很好的提高应用程序的性能。</p>
<p>如何实现呢？看下面代码：</p>
<pre class="brush: csharp; title: ; notranslate">class Program
{
    static void Main(string[] args)
    {
        Lazy&lt;Test&gt; lazy = new Lazy&lt;Test&gt;(); //封装要延时加载的对象
        Console.WriteLine(&quot;创建Lazy对象&quot;);
        Console.WriteLine(&quot;是否创建对象：&quot; + lazy.IsValueCreated);
        lazy.Value.Run();   //调用对象中的方法
    }
}

public class Test
{
    public Test()
    {
        Console.WriteLine(&quot;创建Test对象&quot;);
    }

    public void Run()
    {
        Console.WriteLine(&quot;跑！&quot;);
    }
}</pre>
<p>运行结果：</p>
<p><a href="http://www.dotnetdev.cn/wp-content/uploads/2011/04/lazy.png" rel="lightbox[650]"><img class="alignnone size-full wp-image-651" title="C#中的延时加载Lazy" src="http://www.dotnetdev.cn/wp-content/uploads/2011/04/lazy.png" alt="C#中的延时加载Lazy" width="213" height="133" /></a></p>
<p>默认设置下，这个类的所有成员都是线程安全的。我们可以很方便的使用这个类实现对某个对象的延迟加载。</p>
<p>Related posts:<ol>
<li><a href='http://www.dotnetdev.cn/2011/04/c%e4%b8%ad%e7%9a%84%e6%89%a9%e5%b1%95%e6%96%b9%e6%b3%95/' rel='bookmark' title='C#中的扩展方法'>C#中的扩展方法</a></li>
<li><a href='http://www.dotnetdev.cn/2010/08/net-4-0%e6%96%b0%e7%89%b9%e6%80%a7%e4%b9%8bexpandoobject/' rel='bookmark' title='.net 4.0新特性之ExpandoObject'>.net 4.0新特性之ExpandoObject</a></li>
<li><a href='http://www.dotnetdev.cn/2011/04/%e4%bd%bf%e7%94%a8sqlbulkcopy%e6%8f%90%e9%ab%98%e6%89%b9%e9%87%8f%e6%8f%92%e5%85%a5%e6%95%b0%e6%8d%ae%e5%ba%93%e6%80%a7%e8%83%bd/' rel='bookmark' title='使用SqlBulkCopy提高批量插入数据库性能'>使用SqlBulkCopy提高批量插入数据库性能</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetdev.cn/2011/04/c%e4%b8%ad%e7%9a%84%e5%bb%b6%e6%97%b6%e5%8a%a0%e8%bd%bdlazyof-t/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C#中的扩展方法</title>
		<link>http://www.dotnetdev.cn/2011/04/c%e4%b8%ad%e7%9a%84%e6%89%a9%e5%b1%95%e6%96%b9%e6%b3%95/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=c%25e4%25b8%25ad%25e7%259a%2584%25e6%2589%25a9%25e5%25b1%2595%25e6%2596%25b9%25e6%25b3%2595</link>
		<comments>http://www.dotnetdev.cn/2011/04/c%e4%b8%ad%e7%9a%84%e6%89%a9%e5%b1%95%e6%96%b9%e6%b3%95/#comments</comments>
		<pubDate>Tue, 26 Apr 2011 06:52:49 +0000</pubDate>
		<dc:creator>xsi640</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[未分类]]></category>

		<guid isPermaLink="false">http://www.dotnetdev.cn/?p=648</guid>
		<description><![CDATA[扩展方法让大家很容易的向现有类型中添加方法（不破坏源类的内容）。 写法，看代码： 很容易吧，需要注意的是，扩展方法必须是在非泛型静态类中定义，并且扩展方法必须是静态的，方法的第一个参数必须是this [类型]。使用扩展方法，可以很容易的为我们已有的类添加方法，如给String类添加个ToSource方法等。 Related posts: C#中的延时加载Lazy(Of T) C#使用SharpZipLib类库压缩、解压缩单个文件 .net 4.0新特性之ExpandoObject
Related posts:<ol>
<li><a href='http://www.dotnetdev.cn/2011/04/c%e4%b8%ad%e7%9a%84%e5%bb%b6%e6%97%b6%e5%8a%a0%e8%bd%bdlazyof-t/' rel='bookmark' title='C#中的延时加载Lazy(Of T)'>C#中的延时加载Lazy(Of T)</a></li>
<li><a href='http://www.dotnetdev.cn/2011/04/c%e4%bd%bf%e7%94%a8sharpziplib%e7%b1%bb%e5%ba%93%e5%8e%8b%e7%bc%a9%e3%80%81%e8%a7%a3%e5%8e%8b%e7%bc%a9%e5%8d%95%e4%b8%aa%e6%96%87%e4%bb%b6/' rel='bookmark' title='C#使用SharpZipLib类库压缩、解压缩单个文件'>C#使用SharpZipLib类库压缩、解压缩单个文件</a></li>
<li><a href='http://www.dotnetdev.cn/2010/08/net-4-0%e6%96%b0%e7%89%b9%e6%80%a7%e4%b9%8bexpandoobject/' rel='bookmark' title='.net 4.0新特性之ExpandoObject'>.net 4.0新特性之ExpandoObject</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>扩展方法让大家很容易的向现有类型中添加方法（不破坏源类的内容）。</p>
<p>写法，看代码：</p>
<pre class="brush: csharp; title: ; notranslate">class Program
{
    static void Main(string[] args)
    {
        Human human = new Human();
        human.Name = &quot;张三&quot;;
        human.Age = 18;

        human.GetName();
        human.GetAge(&quot;岁&quot;);
    }
}

public class Human
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static class HumanExtension
{
    /// &lt;summary&gt;
    /// 扩展Human类的方法GetName
    /// &lt;/summary&gt;
    /// &lt;param name=&quot;human&quot;&gt;&lt;/param&gt;
    public static void GetName(this Human human)
    {
        Console.WriteLine(&quot;姓名：&quot; + human.Name);
    }
    /// &lt;summary&gt;
    /// 扩展Human类的方法GetAge
    /// &lt;/summary&gt;
    /// &lt;param name=&quot;human&quot;&gt;&lt;/param&gt;
    /// &lt;param name=&quot;unit&quot;&gt;单位&lt;/param&gt;
    public static void GetAge(this Human human, string unit)
    {
        Console.WriteLine(&quot;年龄：&quot; + human.Age + unit);
    }
}</pre>
<p>很容易吧，需要注意的是，扩展方法必须是在非泛型静态类中定义，并且扩展方法必须是静态的，方法的第一个参数必须是this [类型]。使用扩展方法，可以很容易的为我们已有的类添加方法，如给String类添加个ToSource方法等。</p>
<p>Related posts:<ol>
<li><a href='http://www.dotnetdev.cn/2011/04/c%e4%b8%ad%e7%9a%84%e5%bb%b6%e6%97%b6%e5%8a%a0%e8%bd%bdlazyof-t/' rel='bookmark' title='C#中的延时加载Lazy(Of T)'>C#中的延时加载Lazy(Of T)</a></li>
<li><a href='http://www.dotnetdev.cn/2011/04/c%e4%bd%bf%e7%94%a8sharpziplib%e7%b1%bb%e5%ba%93%e5%8e%8b%e7%bc%a9%e3%80%81%e8%a7%a3%e5%8e%8b%e7%bc%a9%e5%8d%95%e4%b8%aa%e6%96%87%e4%bb%b6/' rel='bookmark' title='C#使用SharpZipLib类库压缩、解压缩单个文件'>C#使用SharpZipLib类库压缩、解压缩单个文件</a></li>
<li><a href='http://www.dotnetdev.cn/2010/08/net-4-0%e6%96%b0%e7%89%b9%e6%80%a7%e4%b9%8bexpandoobject/' rel='bookmark' title='.net 4.0新特性之ExpandoObject'>.net 4.0新特性之ExpandoObject</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetdev.cn/2011/04/c%e4%b8%ad%e7%9a%84%e6%89%a9%e5%b1%95%e6%96%b9%e6%b3%95/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>c#发送电子邮件</title>
		<link>http://www.dotnetdev.cn/2011/04/c%e5%8f%91%e9%80%81%e7%94%b5%e5%ad%90%e9%82%ae%e4%bb%b6/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=c%25e5%258f%2591%25e9%2580%2581%25e7%2594%25b5%25e5%25ad%2590%25e9%2582%25ae%25e4%25bb%25b6</link>
		<comments>http://www.dotnetdev.cn/2011/04/c%e5%8f%91%e9%80%81%e7%94%b5%e5%ad%90%e9%82%ae%e4%bb%b6/#comments</comments>
		<pubDate>Wed, 20 Apr 2011 06:53:21 +0000</pubDate>
		<dc:creator>xsi640</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[电子邮件]]></category>

		<guid isPermaLink="false">http://www.dotnetdev.cn/?p=623</guid>
		<description><![CDATA[大家都经常发送电子邮件，但是，如何使用C#来发送电子邮件呢？直接看下面代码吧，注释写的很清楚了。 Related posts: C#中的延时加载Lazy(Of T)
Related posts:<ol>
<li><a href='http://www.dotnetdev.cn/2011/04/c%e4%b8%ad%e7%9a%84%e5%bb%b6%e6%97%b6%e5%8a%a0%e8%bd%bdlazyof-t/' rel='bookmark' title='C#中的延时加载Lazy(Of T)'>C#中的延时加载Lazy(Of T)</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>大家都经常发送电子邮件，但是，如何使用C#来发送电子邮件呢？直接看下面代码吧，注释写的很清楚了。</p>
<pre class="brush: csharp; title: ; notranslate">MailMessage mail = new MailMessage();
//收件人电子邮件地址
mail.To.Add(&quot;xsi64@126.com&quot;);
//发件人电子邮件地址
mail.From = new MailAddress(&quot;xsi64@gmail.com&quot;);
//电子邮件主题
mail.Subject = &quot;测试发送电子邮件&quot;;
//电子邮件内容
mail.Body = &quot;欢迎光临&lt;a href='http://www.dotnetdev.cn'&gt;.Net开发者Blog&lt;/a&gt;!&quot;;
//邮件内容支持HTML
mail.IsBodyHtml = true;

//向邮件内容中添加一个图片标签，制定id为&quot;image1&quot;
mail.Body += &quot;&lt;br/&gt;&lt;img alt=\&quot;\&quot; src=\&quot;cid:image1\&quot;&gt;&quot;;
//new一个电子邮件嵌入资源对象，路径是F:\test.jpg
LinkedResource imgLink = new LinkedResource(@&quot;F:\test.jpg&quot;);
//将该附件id设为image1,
imgLink.ContentId = &quot;image1&quot;;
//该资源对象传输编码设为Base64
imgLink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;

//创建一个电子邮件查看格式为&quot;text/html&quot;
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(mail.Body, null, &quot;text/html&quot;);
//将嵌入内容的图片附件，放入嵌入资源集
htmlView.LinkedResources.Add(imgLink);
//放入电子邮件内容中
mail.AlternateViews.Add(htmlView);

//向电子邮件中，添加一个附件，路径为&quot;f:\test.txt&quot;
mail.Attachments.Add(new Attachment(@&quot;F:\test.txt&quot;));

//新建一个smtpClient
SmtpClient smtp = new SmtpClient();
//设置smtp服务器地址
smtp.Host = &quot;smtp.xxx.com&quot;;
//设置smtp服务器认证方式
smtp.Credentials = new NetworkCredential(&quot;xxx登录名&quot;, &quot;xxx登陆密码&quot;);
//设置stmp服务器是否启用了ssl加密
smtp.EnableSsl = true;

//发送该邮件
smtp.Send(mail);</pre>
<p>Related posts:<ol>
<li><a href='http://www.dotnetdev.cn/2011/04/c%e4%b8%ad%e7%9a%84%e5%bb%b6%e6%97%b6%e5%8a%a0%e8%bd%bdlazyof-t/' rel='bookmark' title='C#中的延时加载Lazy(Of T)'>C#中的延时加载Lazy(Of T)</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetdev.cn/2011/04/c%e5%8f%91%e9%80%81%e7%94%b5%e5%ad%90%e9%82%ae%e4%bb%b6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>使用C#读取系统默认图标</title>
		<link>http://www.dotnetdev.cn/2011/04/%e4%bd%bf%e7%94%a8c%e8%af%bb%e5%8f%96%e7%b3%bb%e7%bb%9f%e9%bb%98%e8%ae%a4%e5%9b%be%e6%a0%87/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=%25e4%25bd%25bf%25e7%2594%25a8c%25e8%25af%25bb%25e5%258f%2596%25e7%25b3%25bb%25e7%25bb%259f%25e9%25bb%2598%25e8%25ae%25a4%25e5%259b%25be%25e6%25a0%2587</link>
		<comments>http://www.dotnetdev.cn/2011/04/%e4%bd%bf%e7%94%a8c%e8%af%bb%e5%8f%96%e7%b3%bb%e7%bb%9f%e9%bb%98%e8%ae%a4%e5%9b%be%e6%a0%87/#comments</comments>
		<pubDate>Mon, 18 Apr 2011 02:35:08 +0000</pubDate>
		<dc:creator>xsi640</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.dotnetdev.cn/?p=620</guid>
		<description><![CDATA[比如，如果我们已知扩展名，如何获得该扩展名的默认系统图标呢？我们只要使用下面Win32函数就可以获得该图标的句柄，然后，再通过句柄获得图像文件。 代码下载 Related posts: 用C#得到访问IP出口的网卡 C#中的扩展方法 使用c#实现java中的native2ascii功能，解决中文乱码
Related posts:<ol>
<li><a href='http://www.dotnetdev.cn/2011/04/%e7%94%a8c%e5%be%97%e5%88%b0%e8%ae%bf%e9%97%aeip%e5%87%ba%e5%8f%a3%e7%9a%84%e7%bd%91%e5%8d%a1/' rel='bookmark' title='用C#得到访问IP出口的网卡'>用C#得到访问IP出口的网卡</a></li>
<li><a href='http://www.dotnetdev.cn/2011/04/c%e4%b8%ad%e7%9a%84%e6%89%a9%e5%b1%95%e6%96%b9%e6%b3%95/' rel='bookmark' title='C#中的扩展方法'>C#中的扩展方法</a></li>
<li><a href='http://www.dotnetdev.cn/2011/03/%e4%bd%bf%e7%94%a8c%e5%ae%9e%e7%8e%b0java%e4%b8%ad%e7%9a%84native2ascii%e5%8a%9f%e8%83%bd%ef%bc%8c%e8%a7%a3%e5%86%b3%e4%b8%ad%e6%96%87%e4%b9%b1%e7%a0%81/' rel='bookmark' title='使用c#实现java中的native2ascii功能，解决中文乱码'>使用c#实现java中的native2ascii功能，解决中文乱码</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>比如，如果我们已知扩展名，如何获得该扩展名的默认系统图标呢？我们只要使用下面Win32函数就可以获得该图标的句柄，然后，再通过句柄获得图像文件。</p>
<pre class="brush: csharp; title: ; notranslate">[DllImport(&quot;Shell32&quot;)]
extern static IntPtr SHGetFileInfo(string pszPath, int dwFileAttributes, out SHFILEINFO psfi, int cbFileInfo, FileInfoFlags uFlags);</pre>
<p><a href="http://www.dotnetdev.cn/wp-content/uploads/2011/04/icons.png" rel="lightbox[620]"><img class="alignnone size-full wp-image-621" title="使用C#获得系统默认图标" src="http://www.dotnetdev.cn/wp-content/uploads/2011/04/icons.png" alt="使用C#获得系统默认图标" width="247" height="183" /></a></p>
<p><a title="使用C#读取系统默认图标" href="http://dl.dbank.com/c0ejkqgsyl">代码下载</a></p>
<p>Related posts:<ol>
<li><a href='http://www.dotnetdev.cn/2011/04/%e7%94%a8c%e5%be%97%e5%88%b0%e8%ae%bf%e9%97%aeip%e5%87%ba%e5%8f%a3%e7%9a%84%e7%bd%91%e5%8d%a1/' rel='bookmark' title='用C#得到访问IP出口的网卡'>用C#得到访问IP出口的网卡</a></li>
<li><a href='http://www.dotnetdev.cn/2011/04/c%e4%b8%ad%e7%9a%84%e6%89%a9%e5%b1%95%e6%96%b9%e6%b3%95/' rel='bookmark' title='C#中的扩展方法'>C#中的扩展方法</a></li>
<li><a href='http://www.dotnetdev.cn/2011/03/%e4%bd%bf%e7%94%a8c%e5%ae%9e%e7%8e%b0java%e4%b8%ad%e7%9a%84native2ascii%e5%8a%9f%e8%83%bd%ef%bc%8c%e8%a7%a3%e5%86%b3%e4%b8%ad%e6%96%87%e4%b9%b1%e7%a0%81/' rel='bookmark' title='使用c#实现java中的native2ascii功能，解决中文乱码'>使用c#实现java中的native2ascii功能，解决中文乱码</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetdev.cn/2011/04/%e4%bd%bf%e7%94%a8c%e8%af%bb%e5%8f%96%e7%b3%bb%e7%bb%9f%e9%bb%98%e8%ae%a4%e5%9b%be%e6%a0%87/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>用C#得到访问IP出口的网卡</title>
		<link>http://www.dotnetdev.cn/2011/04/%e7%94%a8c%e5%be%97%e5%88%b0%e8%ae%bf%e9%97%aeip%e5%87%ba%e5%8f%a3%e7%9a%84%e7%bd%91%e5%8d%a1/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=%25e7%2594%25a8c%25e5%25be%2597%25e5%2588%25b0%25e8%25ae%25bf%25e9%2597%25aeip%25e5%2587%25ba%25e5%258f%25a3%25e7%259a%2584%25e7%25bd%2591%25e5%258d%25a1</link>
		<comments>http://www.dotnetdev.cn/2011/04/%e7%94%a8c%e5%be%97%e5%88%b0%e8%ae%bf%e9%97%aeip%e5%87%ba%e5%8f%a3%e7%9a%84%e7%bd%91%e5%8d%a1/#comments</comments>
		<pubDate>Wed, 13 Apr 2011 09:40:41 +0000</pubDate>
		<dc:creator>xsi640</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[网卡]]></category>

		<guid isPermaLink="false">http://www.dotnetdev.cn/?p=617</guid>
		<description><![CDATA[﻿﻿﻿随着计算机硬件设备的高速发展，很多用户的计算机上可能安装着好几块网卡，但是我们如何知道用户访问外网时所使用的网卡呢？ 我先给出这两个win32函数 这下有思路了吧，先通过GetBestInterface函数获得网卡的接口索引，再通过GetAdaptersInfo获得该网卡的信息。 虽然，有了思路，不过实现起来对于没有做过C++开发的朋友还是比较困难的，在这里，我给出了代码大家看看吧。   使用上面的方法就可以很容易的得到访问IP出口的网卡了。 代码下载 Related posts: C#使用SharpZipLib类库压缩、解压缩单个文件 C#中的扩展方法 使用SqlBulkCopy提高批量插入数据库性能
Related posts:<ol>
<li><a href='http://www.dotnetdev.cn/2011/04/c%e4%bd%bf%e7%94%a8sharpziplib%e7%b1%bb%e5%ba%93%e5%8e%8b%e7%bc%a9%e3%80%81%e8%a7%a3%e5%8e%8b%e7%bc%a9%e5%8d%95%e4%b8%aa%e6%96%87%e4%bb%b6/' rel='bookmark' title='C#使用SharpZipLib类库压缩、解压缩单个文件'>C#使用SharpZipLib类库压缩、解压缩单个文件</a></li>
<li><a href='http://www.dotnetdev.cn/2011/04/c%e4%b8%ad%e7%9a%84%e6%89%a9%e5%b1%95%e6%96%b9%e6%b3%95/' rel='bookmark' title='C#中的扩展方法'>C#中的扩展方法</a></li>
<li><a href='http://www.dotnetdev.cn/2011/04/%e4%bd%bf%e7%94%a8sqlbulkcopy%e6%8f%90%e9%ab%98%e6%89%b9%e9%87%8f%e6%8f%92%e5%85%a5%e6%95%b0%e6%8d%ae%e5%ba%93%e6%80%a7%e8%83%bd/' rel='bookmark' title='使用SqlBulkCopy提高批量插入数据库性能'>使用SqlBulkCopy提高批量插入数据库性能</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>﻿﻿﻿随着计算机硬件设备的高速发展，很多用户的计算机上可能安装着好几块网卡，但是我们如何知道用户访问外网时所使用的网卡呢？</p>
<p>我先给出这两个win32函数</p>
<pre class="brush: csharp; title: ; notranslate">/// &lt;summary&gt;
/// 给出目的地的IP地址,返回网卡的接口索引
/// &lt;/summary&gt;
/// &lt;param name=&quot;DestAddr&quot;&gt;&lt;/param&gt;
/// &lt;param name=&quot;BestIfIndex&quot;&gt;&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
[DllImport(&quot;iphlpapi.dll&quot;, CharSet = CharSet.Auto)]
public static extern int GetBestInterface(UInt32 DestAddr, out UInt32 BestIfIndex);
/// &lt;summary&gt;
/// 获得所有网卡信息
/// &lt;/summary&gt;
/// &lt;param name=&quot;pAdapterInfo&quot;&gt;&lt;/param&gt;
/// &lt;param name=&quot;pBufOutLen&quot;&gt;&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
[DllImport(&quot;iphlpapi.dll&quot;, CharSet = CharSet.Ansi)]
public static extern int GetAdaptersInfo(IntPtr pAdapterInfo, ref Int64 pBufOutLen);</pre>
<p>这下有思路了吧，先通过GetBestInterface函数获得网卡的接口索引，再通过GetAdaptersInfo获得该网卡的信息。</p>
<p>虽然，有了思路，不过实现起来对于没有做过C++开发的朋友还是比较困难的，在这里，我给出了代码大家看看吧。</p>
<pre class="brush: csharp; title: ; notranslate">IPHostEntry hostInfo = Dns.GetHostEntry(&quot;www.google.com&quot;);
IPAddress address = hostInfo.AddressList[0];//解析域名到IP地址
byte[] byteAddress = address.GetAddressBytes();
UInt32 ipaddr = BitConverter.ToUInt32(byteAddress, 0);//转换
UInt32 interfaceindex = 0;
GetBestInterface(ipaddr, out interfaceindex);//获得接口索引
Console.WriteLine(&quot;www.google.com的网卡接口索引:&quot; + interfaceindex);</pre>
<p> </p>
<pre class="brush: csharp; title: ; notranslate">public static List&lt;NetworkAdapter&gt; GetAdapters()
{
    List&lt;NetworkAdapter&gt; result = new List&lt;NetworkAdapter&gt;();

    long structSize = Marshal.SizeOf(typeof(IP_ADAPTER_INFO));
    IntPtr pArray = Marshal.AllocHGlobal(new IntPtr(structSize));
    int ret = GetAdaptersInfo(pArray, ref structSize);//获得所有网卡信息
    if (ret == ERROR_BUFFER_OVERFLOW)
    {
        pArray = Marshal.ReAllocHGlobal(pArray, new IntPtr(structSize));
        ret = GetAdaptersInfo(pArray, ref structSize);
    }

    if (ret == 0)
    {
        IntPtr pEntry = pArray;
        do
        {
            //当前网卡信息
            IP_ADAPTER_INFO entry = (IP_ADAPTER_INFO)Marshal.PtrToStructure(pEntry, typeof(IP_ADAPTER_INFO));

            NetworkAdapter networkAdapter = new NetworkAdapter();
            networkAdapter.Index = entry.Index;//接口索引
            string tmpString = string.Empty;
            switch (entry.Type)
            {
                case MIB_IF_TYPE_ETHERNET: tmpString = &quot;Ethernet&quot;; break;
                case MIB_IF_TYPE_TOKENRING: tmpString = &quot;Token Ring&quot;; break;
                case MIB_IF_TYPE_FDDI: tmpString = &quot;FDDI&quot;; break;
                case MIB_IF_TYPE_PPP: tmpString = &quot;PPP&quot;; break;
                case MIB_IF_TYPE_LOOPBACK: tmpString = &quot;Loopback&quot;; break;
                case MIB_IF_TYPE_SLIP: tmpString = &quot;Slip&quot;; break;
                default: tmpString = &quot;Other/Unknown&quot;; break;
            }
            networkAdapter.AdapterType = tmpString;//网卡类型
            networkAdapter.Name = entry.AdapterName;//网卡名称
            networkAdapter.Description = entry.AdapterDescription;//网卡描述
            networkAdapter.IsEnableDHCP = entry.DhcpEnabled == 1 ? true : false;//是否启用DHCP
            if (entry.DhcpEnabled == 1)
            {
                networkAdapter.DHCPAddress = entry.DhcpServer.IpAddress.Address;
                DateTime pdatDate = new DateTime(1970, 1, 1).AddSeconds(entry.LeaseObtained).ToLocalTime();
                networkAdapter.DHCPLeaseObtained = pdatDate;//DHCP租约开始时间
                pdatDate = new DateTime(1970, 1, 1).AddSeconds(entry.LeaseExpires).ToLocalTime();
                networkAdapter.DHCPLeaseExpires = pdatDate;//DHCP租约结束时间
            }
            networkAdapter.IPAddress = entry.IpAddressList.IpAddress.Address;//IP地址
            networkAdapter.SubNetMask = entry.IpAddressList.IpMask.Address;//子网掩码
            networkAdapter.DefaultGateway = entry.GatewayList.IpAddress.Address;//网关

            tmpString = string.Empty;
            for (int i = 0; i &lt; entry.AddressLength - 1; i++)
            {
                tmpString += string.Format(&quot;{0:X2}-&quot;, entry.Address[i]);
            }
            tmpString = string.Format(&quot;{0}{1:X2}&quot;, tmpString, entry.Address[entry.AddressLength - 1]);
            networkAdapter.MACAddress = tmpString;//MAC地址

            networkAdapter.HasWINS = entry.HaveWins;//是否有Wins
            if (entry.HaveWins)
            {
                networkAdapter.PrimaryWINSServer = entry.PrimaryWinsServer.IpAddress.Address;//主Wins地址
                networkAdapter.SecondaryWINSServer = entry.SecondaryWinsServer.IpAddress.Address;//辅Wins地址
            }
            result.Add(networkAdapter);
            pEntry = entry.Next;

        }
        while (pEntry != IntPtr.Zero);

        Marshal.FreeHGlobal(pArray);

    }
    else
    {
        Marshal.FreeHGlobal(pArray);
    }
    return result;
}</pre>
<p>使用上面的方法就可以很容易的得到访问IP出口的网卡了。</p>
<p><a title="用C#得到访问IP出口的网卡" href="http://dl.dbank.com/c0z0rzh7qd" target="_blank">代码下载</a></p>
<p>Related posts:<ol>
<li><a href='http://www.dotnetdev.cn/2011/04/c%e4%bd%bf%e7%94%a8sharpziplib%e7%b1%bb%e5%ba%93%e5%8e%8b%e7%bc%a9%e3%80%81%e8%a7%a3%e5%8e%8b%e7%bc%a9%e5%8d%95%e4%b8%aa%e6%96%87%e4%bb%b6/' rel='bookmark' title='C#使用SharpZipLib类库压缩、解压缩单个文件'>C#使用SharpZipLib类库压缩、解压缩单个文件</a></li>
<li><a href='http://www.dotnetdev.cn/2011/04/c%e4%b8%ad%e7%9a%84%e6%89%a9%e5%b1%95%e6%96%b9%e6%b3%95/' rel='bookmark' title='C#中的扩展方法'>C#中的扩展方法</a></li>
<li><a href='http://www.dotnetdev.cn/2011/04/%e4%bd%bf%e7%94%a8sqlbulkcopy%e6%8f%90%e9%ab%98%e6%89%b9%e9%87%8f%e6%8f%92%e5%85%a5%e6%95%b0%e6%8d%ae%e5%ba%93%e6%80%a7%e8%83%bd/' rel='bookmark' title='使用SqlBulkCopy提高批量插入数据库性能'>使用SqlBulkCopy提高批量插入数据库性能</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetdev.cn/2011/04/%e7%94%a8c%e5%be%97%e5%88%b0%e8%ae%bf%e9%97%aeip%e5%87%ba%e5%8f%a3%e7%9a%84%e7%bd%91%e5%8d%a1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

