学习C#已经有两个多月,在网上查了很多资料,也整理了很多类。大部分来自网络,然后自己修改一下
发个帖整理一下,希望对园友有所帮助
1 热键注册类
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.Runtime.InteropServices; 5 using System.Windows.Forms; 6 7 namespace IDCard 8 { 9 class HotKey10 { 11 //如果函数执行成功,返回值不为0。 12 //如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。13 [DllImport("user32.dll", SetLastError = true)] 14 public static extern bool RegisterHotKey(15 IntPtr hWnd, //要定义热键的窗口的句柄 16 int id, //定义热键ID(不能与其它ID重复) 17 KeyModifiers fsModifiers, //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效 18 Keys vk //定义热键的内容 19 ); 20 [DllImport("user32.dll", SetLastError = true)] 21 public static extern bool UnregisterHotKey( 22 IntPtr hWnd, //要取消热键的窗口的句柄 23 int id //要取消热键的ID 24 ); 25 //定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值) 26 [Flags()]27 public enum KeyModifiers 28 { 29 None = 0, 30 Alt = 1, 31 Ctrl = 2, 32 Shift = 4, 33 WindowsKey = 8 34 }35 }36} 37
2 加密解密类
1 using System; 2 using System.Data; 3 using System.Data.OleDb; 4 using System.Collections.Generic; 5 using System.Text; 6 using System.IO; 7 using System.Security.Cryptography; 8 using Microsoft.Win32; 9 using System.Configuration; 10 using System.Windows.Forms; 11 12 namespace IDCard 13 { 14 class Decrypt 15 { 16 17 //默认密钥向量 18 private static byte[] Keys = { 0x55, 0x32, 0xF8, 0xEA, 0x93, 0xC3, 0xF9, 0xB2 }; 19 /**//// <summary> 20 /// DES加密字符串 21 /// </summary> 22 /// <param name="encryptString">待加密的字符串</param> 23 /// <param name="encryptKey">加密密钥,要求为8位</param> 24 /// <returns>加密成功返回加密后的字符串,失败返回源串</returns> 25 public static string EncryptDES(string encryptString, string encryptKey) 26 { 27 try 28 { 29 byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey); 30 byte[] rgbIV = Keys; 31 byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString); 32 DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider(); 33 MemoryStream mStream = new MemoryStream(); 34 CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write); 35 cStream.Write(inputByteArray, 0, inputByteArray.Length); 36 cStream.FlushFinalBlock(); 37 return Convert.ToBase64String(mStream.ToArray()); 38 } 39 catch (Exception e) 40 { 41 MessageBox.Show("Message=" + e.Message); 42 return encryptString; 43 } 44 } 45 46 /**//// <summary> 47 /// DES解密字符串 48 /// </summary> 49 /// <param name="decryptString">待解密的字符串</param> 50 /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param> 51 /// <returns>解密成功返回解密后的字符串,失败返源串</returns> 52 public static string DecryptDES(string decryptString, string decryptKey) 53 { 54 try 55 { 56 byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey); 57 byte[] rgbIV = Keys; 58 byte[] inputByteArray = Convert.FromBase64String(decryptString); 59 DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider(); 60 MemoryStream mStream = new MemoryStream(); 61 CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); 62 cStream.Write(inputByteArray, 0, inputByteArray.Length); 63 cStream.FlushFinalBlock(); 64 return Encoding.UTF8.GetString(mStream.ToArray()); 65 } 66 catch (Exception e) 67 { 68 MessageBox.Show("Message=" + e.Message); 69 return decryptString; 70 } 71 } 72 73 public static string Formate(string decryptString, string decryptKey) 74 { 75 string transactSn = string.Empty; 76 if (decryptString != "") 77 { 78 string sn = EncryptDES(EncryptDES(EncryptDES(decryptString, decryptKey), decryptKey),decryptKey); 79 80 sn = Get_MD5_Method1(sn); 81 transactSn = sn.Substring(0, 4) + "-" + sn.Substring(4, 4) + 82 "-" + sn.Substring(8, 4) + "-" + sn.Substring(12, 4); 83 return transactSn; 84 } 85 else 86 return transactSn; 87 } 88 89 public static string Get_MD5_Method1(string strSource) 90 { //new9 91 System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); 92 //获取密文字节数组 93 byte[] bytResult = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(strSource)); 94 //转换成字符串,并取9到25位 95 string strResult = BitConverter.ToString(bytResult, 4, 8); 96 //转换成字符串,32位 97 //string strResult = BitConverter.ToString(bytResult); 98 //BitConverter转换出来的字符串会在每个字符中间产生一个分隔符,需要去除掉 99 strResult = strResult.Replace("-", "");100 return strResult;101 }102 }103} 104
3 繁简体转换
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using Microsoft.VisualBasic; 5 namespace IDCard 6 { 7 class FontStyle 8 { 9 /**//// <summary>10 /// 繁体转简体11 /// </summary>12 /// <param name="str">需转换的繁体文字</param>13 /// <returns></returns>14 public static string Traditional2Simplified(string str)15 { 16 return (Microsoft.VisualBasic.Strings.StrConv(str, Microsoft.VisualBasic.VbStrConv.SimplifiedChinese, 0));1718 }19 /**//// <summary>20 /// 简体转繁体 21 /// </summary>22 /// <param name="str">需转换的简体文字</param>23 /// <returns></returns>24 public static string Simplified2Traditional(string str)25 { 26 return (Microsoft.VisualBasic.Strings.StrConv(str as String, Microsoft.VisualBasic.VbStrConv.TraditionalChinese, 0));27 }2829 }30}
4 读取系统配置文件
1 using System; 2 using System.IO; 3 using System.Runtime.InteropServices; 4 using System.Text; 5 using System.Collections; 6 using System.Collections.Specialized; 7 8 namespace IDCard 9 { 10 /**//**/ 11 /**//**/ 12 /**//**/ 13 /**//// <summary> 14 /// IniFiles的类 15 /// </summary> 16 public class IniFiles 17 { 18 public string FileName; //INI文件名 19 //声明读写INI文件的API函数 20 [DllImport("kernel32")] 21 private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath); 22 [DllImport("kernel32")] 23 private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath); 24 //类的构造函数,传递INI文件名 25 public IniFiles(string AFileName) 26 { 27 // 判断文件是否存在 28 FileInfo fileInfo = new FileInfo(AFileName); 29 //Todo:搞清枚举的用法 30 if ((!fileInfo.Exists)) 31 { // ¦ ¦ (FileAttributes.Directory in fileInfo.Attributes)) 32 //文件不存在,建立文件 33 System.IO.StreamWriter sw = new System.IO.StreamWriter(AFileName, false, System.Text.Encoding.Default); 34 try 35 { 36 sw.Write("#表格配置档案"); 37 sw.Close(); 38 } 39 catch 40 { 41 throw (new ApplicationException("Ini文件不存在")); 42 } 43 } 44 //必须是完全路径,不能是相对路径 45 FileName = fileInfo.FullName; 46 } 47 //写INI文件 48 public void WriteString(string Section, string Ident, string Value) 49 { 50 if (!WritePrivateProfileString(Section, Ident, Value, FileName)) 51 { 52 53 throw (new ApplicationException("写Ini文件出错")); 54 } 55 } 56 //读取INI文件指定 57 public string ReadString(string Section, string Ident, string Default) 58 { 59 Byte[] Buffer = new Byte[65535]; 60 int bufLen = GetPrivateProfileString(Section, Ident, Default, Buffer, Buffer.GetUpperBound(0), FileName); 61 //必须设定0(系统默认的代码页)的编码方式,否则无法支持中文 62 string s = Encoding.GetEncoding(0).GetString(Buffer); 63 s = s.Substring(0, bufLen); 64 return s.Trim(); 65 } 66 67 //读整数 68 public int ReadInteger(string Section, string Ident, int Default) 69 { 70 string intStr = ReadString(Section, Ident, Convert.ToString(Default)); 71 try 72 { 73 return Convert.ToInt32(intStr); 74 } 75 catch (Exception ex) 76 { 77 Console.WriteLine(ex.Message); 78 return Default; 79 } 80 } 81 82 //写整数 83 public void WriteInteger(string Section, string Ident, int Value) 84 { 85 WriteString(Section, Ident, Value.ToString()); 86 } 87 88 //读布尔 89 public bool ReadBool(string Section, string Ident, bool Default) 90 { 91 try 92 { 93 return Convert.ToBoolean(ReadString(Section, Ident, Convert.ToString(Default))); 94 } 95 catch (Exception ex) 96 { 97 Console.WriteLine(ex.Message); 98 return Default; 99 }100 }101102 //写Bool 103 public void WriteBool(string Section, string Ident, bool Value)104 { 105 WriteString(Section, Ident, Convert.ToString(Value));106 }107108 //从Ini文件中,将指定的Section名称中的所有Ident添加到列表中 109 public void ReadSection(string Section, StringCollection Idents)110 { 111 Byte[] Buffer = new Byte[16384];112 //Idents.Clear(); 113114 int bufLen = GetPrivateProfileString(Section, null, null, Buffer, Buffer.GetUpperBound(0),115 FileName);116 //对Section进行解析 117 GetStringsFromBuffer(Buffer, bufLen, Idents);118 }119120 private void GetStringsFromBuffer(Byte[] Buffer, int bufLen, StringCollection Strings)121 { 122 Strings.Clear();123 if (bufLen != 0)124 { 125 int start = 0;126 for (int i = 0; i < bufLen; i++)127 { 128 if ((Buffer[i] == 0) && ((i - start) > 0))129 { 130 String s = Encoding.GetEncoding(0).GetString(Buffer, start, i - start);131 Strings.Add(s);132 start = i + 1;133 }134 }135 }136 }137 //从Ini文件中,读取所有的Sections的名称 138 public void ReadSections(StringCollection SectionList)139 { 140 //Note:必须得用Bytes来实现,StringBuilder只能取到第一个Section 141 byte[] Buffer = new byte[65535];142 int bufLen = 0;143 bufLen = GetPrivateProfileString(null, null, null, Buffer,144 Buffer.GetUpperBound(0), FileName);145 GetStringsFromBuffer(Buffer, bufLen, SectionList);146 }147 //读取指定的Section的所有Value到列表中 148 public void ReadSectionValues(string Section, NameValueCollection Values)149 { 150 StringCollection KeyList = new StringCollection();151 ReadSection(Section, KeyList);152 Values.Clear();153 foreach (string key in KeyList)154 { 155 Values.Add(key, ReadString(Section, key, ""));156157 }158 }159 //清除某个Section 160 public void EraseSection(string Section)161 { 162 // 163 if (!WritePrivateProfileString(Section, null, null, FileName))164 { 165 throw (new ApplicationException("无法清除Ini文件中的Section"));166 }167 }168 //删除某个Section下的键 169 public void DeleteKey(string Section, string Ident)170 { 171 WritePrivateProfileString(Section, Ident, null, FileName);172 }173 //Note:对于Win9X,来说需要实现UpdateFile方法将缓冲中的数据写入文件 174 //在Win NT, 2000和XP上,都是直接写文件,没有缓冲,所以,无须实现UpdateFile 175 //执行完对Ini文件的修改之后,应该调用本方法更新缓冲区。 176 public void UpdateFile()177 { 178 WritePrivateProfileString(null, null, null, FileName);179 }180181 //检查某个Section下的某个键值是否存在 182 public bool ValueExists(string Section, string Ident)183 { 184 // 185 StringCollection Idents = new StringCollection();186 ReadSection(Section, Idents);187 return Idents.IndexOf(Ident) > -1;188 }189190 //确保资源的释放 191 ~IniFiles()192 { 193 UpdateFile();194 }195 }196} 197 198