博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
关于最近用过的一些类
阅读量:5314 次
发布时间:2019-06-14

本文共 10206 字,大约阅读时间需要 34 分钟。

学习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
ExpandedBlockStart.gifContractedBlock.gif
{
 9    class HotKey
10ExpandedSubBlockStart.gifContractedSubBlock.gif    {
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        
28ExpandedSubBlockStart.gifContractedSubBlock.gif        {            
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
ExpandedBlockStart.gifContractedBlock.gif
{
 14    class Decrypt
 15ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 16
 17        //默认密钥向量
 18ExpandedSubBlockStart.gifContractedSubBlock.gif        private static byte[] Keys = 0x550x320xF80xEA0x930xC30xF90xB2 };
 19ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <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)
 26ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 27            try
 28ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 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)
 40ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 41                MessageBox.Show("Message=" + e.Message);
 42                return encryptString;
 43            }
 44        }
 45
 46ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <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)
 53ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 54            try
 55ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 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)
 67ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 68                MessageBox.Show("Message=" + e.Message);
 69                return decryptString;
 70            }
 71        }
 72
 73        public static string Formate(string decryptString, string decryptKey)
 74ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 75            string transactSn = string.Empty;
 76            if (decryptString != "")
 77ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 78                string sn = EncryptDES(EncryptDES(EncryptDES(decryptString, decryptKey), decryptKey),decryptKey);
 79                
 80                sn = Get_MD5_Method1(sn);
 81                transactSn = sn.Substring(04+ "-" + sn.Substring(44+
 82                "-" + sn.Substring(84+ "-" + sn.Substring(124);
 83                return transactSn;
 84            }
 85            else
 86                return transactSn;
 87        }
 88
 89        public static string Get_MD5_Method1(string strSource)
 90ExpandedSubBlockStart.gifContractedSubBlock.gif        { //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, 48);
 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
ExpandedBlockStart.gifContractedBlock.gif
{
 7    class FontStyle
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 9ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
10        /// 繁体转简体
11        /// </summary>
12        /// <param name="str">需转换的繁体文字</param>
13        /// <returns></returns>
14        public static string Traditional2Simplified(string str)
15ExpandedSubBlockStart.gifContractedSubBlock.gif        
16            return (Microsoft.VisualBasic.Strings.StrConv(str, Microsoft.VisualBasic.VbStrConv.SimplifiedChinese, 0));
17
18        }
19ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
20        /// 简体转繁体 
21        /// </summary>
22        /// <param name="str">需转换的简体文字</param>
23        /// <returns></returns>
24        public static string Simplified2Traditional(string str)
25ExpandedSubBlockStart.gifContractedSubBlock.gif        
26            return (Microsoft.VisualBasic.Strings.StrConv(str as String, Microsoft.VisualBasic.VbStrConv.TraditionalChinese, 0));
27        }
28
29    }
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
ExpandedBlockStart.gifContractedBlock.gif
{
 10ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//**/
 11ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//**/
 12ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//**/
 13ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary> 
 14    /// IniFiles的类 
 15    /// </summary> 
 16    public class IniFiles
 17ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 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)
 26ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 27            // 判断文件是否存在 
 28            FileInfo fileInfo = new FileInfo(AFileName);
 29            //Todo:搞清枚举的用法 
 30            if ((!fileInfo.Exists))
 31ExpandedSubBlockStart.gifContractedSubBlock.gif            // ¦ ¦ (FileAttributes.Directory in fileInfo.Attributes)) 
 32                //文件不存在,建立文件 
 33                System.IO.StreamWriter sw = new System.IO.StreamWriter(AFileName, false, System.Text.Encoding.Default);
 34                try
 35ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 36                    sw.Write("#表格配置档案");
 37                    sw.Close();
 38                }
 39                catch
 40ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 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)
 49ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 50            if (!WritePrivateProfileString(Section, Ident, Value, FileName))
 51ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 52
 53                throw (new ApplicationException("写Ini文件出错"));
 54            }
 55        }
 56        //读取INI文件指定 
 57        public string ReadString(string Section, string Ident, string Default)
 58ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 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)
 69ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 70            string intStr = ReadString(Section, Ident, Convert.ToString(Default));
 71            try
 72ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 73                return Convert.ToInt32(intStr);
 74            }
 75            catch (Exception ex)
 76ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 77                Console.WriteLine(ex.Message);
 78                return Default;
 79            }
 80        }
 81
 82        //写整数 
 83        public void WriteInteger(string Section, string Ident, int Value)
 84ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 85            WriteString(Section, Ident, Value.ToString());
 86        }
 87
 88        //读布尔 
 89        public bool ReadBool(string Section, string Ident, bool Default)
 90ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 91            try
 92ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 93                return Convert.ToBoolean(ReadString(Section, Ident, Convert.ToString(Default)));
 94            }
 95            catch (Exception ex)
 96ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 97                Console.WriteLine(ex.Message);
 98                return Default;
 99            }
100        }
101
102        //写Bool 
103        public void WriteBool(string Section, string Ident, bool Value)
104ExpandedSubBlockStart.gifContractedSubBlock.gif        {
105            WriteString(Section, Ident, Convert.ToString(Value));
106        }
107
108        //从Ini文件中,将指定的Section名称中的所有Ident添加到列表中 
109        public void ReadSection(string Section, StringCollection Idents)
110ExpandedSubBlockStart.gifContractedSubBlock.gif        {
111            Byte[] Buffer = new Byte[16384];
112            //Idents.Clear(); 
113
114            int bufLen = GetPrivateProfileString(Section, nullnull, Buffer, Buffer.GetUpperBound(0),
115            FileName);
116            //对Section进行解析 
117            GetStringsFromBuffer(Buffer, bufLen, Idents);
118        }
119
120        private void GetStringsFromBuffer(Byte[] Buffer, int bufLen, StringCollection Strings)
121ExpandedSubBlockStart.gifContractedSubBlock.gif        {
122            Strings.Clear();
123            if (bufLen != 0)
124ExpandedSubBlockStart.gifContractedSubBlock.gif            {
125                int start = 0;
126                for (int i = 0; i < bufLen; i++)
127ExpandedSubBlockStart.gifContractedSubBlock.gif                {
128                    if ((Buffer[i] == 0&& ((i - start) > 0))
129ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
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)
139ExpandedSubBlockStart.gifContractedSubBlock.gif        {
140            //Note:必须得用Bytes来实现,StringBuilder只能取到第一个Section 
141            byte[] Buffer = new byte[65535];
142            int bufLen = 0;
143            bufLen = GetPrivateProfileString(nullnullnull, Buffer,
144            Buffer.GetUpperBound(0), FileName);
145            GetStringsFromBuffer(Buffer, bufLen, SectionList);
146        }
147        //读取指定的Section的所有Value到列表中 
148        public void ReadSectionValues(string Section, NameValueCollection Values)
149ExpandedSubBlockStart.gifContractedSubBlock.gif        {
150            StringCollection KeyList = new StringCollection();
151            ReadSection(Section, KeyList);
152            Values.Clear();
153            foreach (string key in KeyList)
154ExpandedSubBlockStart.gifContractedSubBlock.gif            {
155                Values.Add(key, ReadString(Section, key, ""));
156
157            }
158        }
159              //清除某个Section 
160        public void EraseSection(string Section)
161ExpandedSubBlockStart.gifContractedSubBlock.gif        {
162            // 
163            if (!WritePrivateProfileString(Section, nullnull, FileName))
164ExpandedSubBlockStart.gifContractedSubBlock.gif            {
165                throw (new ApplicationException("无法清除Ini文件中的Section"));
166            }
167        }
168        //删除某个Section下的键 
169        public void DeleteKey(string Section, string Ident)
170ExpandedSubBlockStart.gifContractedSubBlock.gif        {
171            WritePrivateProfileString(Section, Ident, null, FileName);
172        }
173        //Note:对于Win9X,来说需要实现UpdateFile方法将缓冲中的数据写入文件 
174        //在Win NT, 2000和XP上,都是直接写文件,没有缓冲,所以,无须实现UpdateFile 
175        //执行完对Ini文件的修改之后,应该调用本方法更新缓冲区。 
176        public void UpdateFile()
177ExpandedSubBlockStart.gifContractedSubBlock.gif        {
178            WritePrivateProfileString(nullnullnull, FileName);
179        }
180
181        //检查某个Section下的某个键值是否存在 
182        public bool ValueExists(string Section, string Ident)
183ExpandedSubBlockStart.gifContractedSubBlock.gif        {
184            // 
185            StringCollection Idents = new StringCollection();
186            ReadSection(Section, Idents);
187            return Idents.IndexOf(Ident) > -1;
188        }
189
190        //确保资源的释放 
191        ~IniFiles()
192ExpandedSubBlockStart.gifContractedSubBlock.gif        {
193            UpdateFile();
194        }
195    }
196}
197
198

 

 

 

转载于:https://www.cnblogs.com/coolkiss/archive/2008/09/26/1299845.html

你可能感兴趣的文章
python升级安装后的yum的修复
查看>>
Vim配置Node.js开发工具
查看>>
web前端面试题2017
查看>>
ELMAH——可插拔错误日志工具
查看>>
MySQL学习笔记(四)
查看>>
【Crash Course Psychology】2. Research & Experimentation笔记
查看>>
两数和
查看>>
移动设备和SharePoint 2013 - 第3部分:推送通知
查看>>
SOPC Builder中SystemID
查看>>
MySQL数据库备份工具mysqldump的使用(转)
查看>>
NTP服务器配置
查看>>
【转】OO无双的blocking/non-blocking执行时刻
查看>>
ul li剧中对齐
查看>>
关于 linux 的 limit 的设置
查看>>
HDU(4528),BFS,2013腾讯编程马拉松初赛第五场(3月25日)
查看>>
vim中文帮助教程
查看>>
MySQL基础3
查看>>
云计算数据与信息安全防护
查看>>
全局设置导航栏
查看>>
RxJS & Angular
查看>>