技术探索
  • 在C#中使用TcpClient与TcpListener发送与接收消息

    2013-05-28
    2150
    C#语言
    本文介绍在C#中如何利用TcpClient与TcpListener进行简单的消息发送和接收。在使用时要注意,如果是本机发送与接收,发送消息时IP用127.0.0.1,如果是非本机接收,请在接收端打开相应的端口(本例中为13)。using System.IO;using System.Net;using System.Net.Sockets;using System.Threading.Tasks;//接收方public Form1(){InitializeComponent();Task.Factory.StartNew(() =>{TcpListener listener = null;TcpClient client = null;string message = string.Empty;while (true)//循环接收消息,否则只能接收一次{try{listener =
  • C#遍历所有缓存

    2013-05-25
    4898
    C#语言
    本文介绍C#中遍历两种缓存的方法,其中第二种是.NET4.0新增的。本方法可用于清除所有的缓存。1、HttpRuntime.Cache System.Collections.IDictionaryEnumerator cacheEnum = HttpRuntime.Cache.GetEnumerator(); while(cacheEnum.MoveNext()) { //cacheEnum.Key.ToString()为缓存名称,cacheEnum.Value为缓存值 }2、System.Runtime.Caching.ObjectCache ObjectCache cache = MemoryCache.Default IEnumerable<KeyValuePair<string, object>> items = cache.AsEnumerable();
  • C#生成图片验证码

    2013-05-24
    929
    C#语言
    本文介绍生成C#验证码的方法,可将本方法放在一个 ashx 文件里,并继承自 System.Web.SessionState.IRequiresSessionState 以保存 session 。using System;using System.Drawing;using System.Drawing.Drawing2D;#region Createprivate void Create(string code){if (code == null || code.Trim() == String.Empty) return;Bitmap image = new Bitmap((int)Math.Ceiling((code.Length * 11.5)), 21);Graphics g = Graphics.FromImage(image);try{Random random = new
  • C#获取计算机网卡的MAC地址

    2013-05-20
    866
    C#语言
    使用以下方法,可以获取到网卡的MAC地址。如果代码运行在Web上,则获取的是Web服务器的MAC;如果是客户端,则是本机的MAC地址。ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");ManagementObjectCollection moc = mc.GetInstances();string str = string.Empty;foreach (ManagementObject mo in moc){if ((bool)mo["IPEnabled"] == true) { str = mo["MacAddress"].ToString(); break; }}//其中str即为mac地址。
  • .NET4.0新增的Task功能初试

    2013-05-18
    4276
    C#语言
    .NET4.0有一个新增的Task功能可以执行多线程操作,并按需返回指定类型的值。使用非常方便,比之前的线程池、回调等方法好用得多。本文简单介绍这一功能的使用,供初学者参考。引用命名空间using System.Threading.Tasks启动执行并返回结果值var a = Task<string>.Factory.StartNew(()=>{//这里是执行方法string a = "a";return a;};//或者以下也可以启动var a = Task.Factory.StartNew(()=>{return "a";};//等待执行完成,若非要等待所有方法全部执行完成后再进行后续操作,也可以省略Task.WaitAll(a);//获得返回值,上例中输出为 "a"。MessageBox.Show(a
  • 用C#计算文件的MD5值

    2013-05-15
    1249
    C#语言
    本文提供一种使用C#计算文件md5值的方法,经测试,处理一个超过1G的文件耗时不超过4秒,速度还是很不错的。 string filePath = "c:\a.jpg";using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)){MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();md5.ComputeHash(fs);fs.Close();byte[] b = md5.Hash;string md5Value = BitConverter.ToString(b).Replace("-", string.Empty);}其中md5Valu
  • 遍历枚举中的所有成员,并按键值对加入IDictionary

    2013-05-14
    2030
    C#语言
    本文介绍如何遍历枚举中的所有成员,并提供枚举名称、枚举值以及枚举名称的字符串形式间的互相转化方法。有如下枚举类型:public enum EnumBookType : byte{Unknown,Science,History}IDictionary〈string, byte〉 booktype = new Dictionary〈string, byte〉();string[] t = Enum.GetNames(typeof(EnumBookType));foreach(string f in t){byte d = (byte)Enum.Parse(typeof(EnumBookType), f);booktype.Add(f, d);}如果要获取字符串“Science”的枚举值,则可以用 (byte)Enum.Parse(typeof(EnumBookType), "Scie
  • 利用cmd.exe命令清除本机ie浏览器特殊内容

    2013-05-07
    1855
    C#语言
    有时候我们可能需要在自己的程序中清除本机IE浏览器保存的一些特殊内容,由于其“特殊性”,所以用一般的方法可能并不理想。下面这个方法可以真正完成这一操作,经测试,唯一发现的问题是一些杀毒软件可能会报错,但任务仍可顺利完成(如卡巴斯基)。#region ClearSpecialContentOfIE////// 清除IE浏览器保存的特殊内容。////// 1:历史记录,2:Cookies,8:Internet临时文件,16:表单数据,32:密码,255:全部删除,4351:全部删除(含设置)public static void ClearSpecialContentOfIE(int flag){string cmd = string.Format("RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess {0}", flag.ToSt