《C# 大量函数》由会员分享,可在线阅读,更多相关《C# 大量函数(93页珍藏版)》请在金锄头文库上搜索。
1、using System;using System.Collections.Generic;using System.Text;using Microsoft.Win32; /对注册表操作using System.Collections; /使用Arraylistusing System.Security.Cryptography;/加密解密using System.IO; /文件操作using System.Runtime.InteropServices;/调用DLL DllImportusing System.Management; /获取硬件信息using System.Net; /获取
2、IP地址是用到using System.Drawing; /image using System.Net.NetworkInformation; /ping 用到using System.Text.RegularExpressions; /正则using System.Data;using System.Data.SqlClient;using Microsoft.VisualBasic; /简体转繁体时用到using System.Web; /html UrlEncodeusing System.Net.Sockets;using System.Drawing.Drawing2D; /图像操
3、作using System.Drawing.Imaging; /图像操作namespace 函数库/注册表操作public class GF_RegReadWrite/ / 读取路径为keypath,键名为keyname的注册表键值,缺省返回def/ / / 路径/ 键名/ 默认为null/ static public bool GetRegVal(RegistryKey rootkey, string keypath, string keyname, out string rtn)rtn = ;tryRegistryKey key = rootkey.OpenSubKey(keypath);
4、 rtn = key.GetValue(keyname).ToString();key.Close();return true;catchreturn false;/ / 设置路径为keypath,键名为keyname的注册表键值为keyval/ / / / / / static public bool SetRegVal(RegistryKey rootkey, string keypath, string keyname, string keyval)tryRegistryKey key = rootkey.OpenSubKey(keypath, true);if (key = null)
5、key = rootkey.CreateSubKey(keypath);key.SetValue(keyname, (object)keyval);key.Close();return true;catchreturn false;/ 创建路径为keypath的键private RegistryKey CreateRegKey(RegistryKey rootkey, string keypath)tryreturn rootkey.CreateSubKey(keypath); catchreturn null;/ 删除路径为keypath的子项private bool DelRegSubKe
6、y(RegistryKey rootkey, string keypath)tryrootkey.DeleteSubKey(keypath);return true;catchreturn false;/ 删除路径为keypath的子项及其附属子项private bool DelRegSubKeyTree(RegistryKey rootkey, string keypath)tryrootkey.DeleteSubKeyTree(keypath);return true;catchreturn false;/ 删除路径为keypath下键名为keyname的键值private bool De
7、lRegKeyVal(RegistryKey rootkey, string keypath, string keyname)tryRegistryKey key = rootkey.OpenSubKey(keypath, true);key.DeleteValue(keyname);return true;catch return false;/类型转换public class GF_Convert/ / 字符串 转换 char数组/ / / / public static char string2chararray(string in_str, int in_len)char ch = n
8、ew charin_len;in_str.ToCharArray().CopyTo(ch, 0);return ch;/ / char数组 转换 字符串/ / / public static string chararray2string(char in_str)string out_str;out_str = new string(in_str);int i = out_str.IndexOf(0, 0);if (i = -1)i = 16;return out_str.Substring(0, i);/ / byte数组 转换 字符串/ / / public static string b
9、ytearray2string(byte in_str) string out_str;out_str = System.Text.Encoding.Default.GetString(in_str);return out_str.Substring(0, out_str.IndexOf(0, 0);/ / 字符串 转换 byte数组 注意转换出来会使原来的bytearray长度变短/ / / public static byte string2bytearray(string in_str)return System.Text.Encoding.Default.GetBytes(in_str
10、);/ / 字符串 转换 byte数组 长度为传如的长度/ / 传入字符串/ 目标字节数组长度/ public static byte string2bytearray(string in_str, int iLen)byte bytes = new byteiLen;byte bsources=System.Text.Encoding.Default.GetBytes(in_str);Array.Copy(bsources, bytes, bsources.Length);return bytes;/ / 将字符串编码为Base64字符串/ / / public static string
11、Base64Encode(string str)byte barray;barray = Encoding.Default.GetBytes(str);return Convert.ToBase64String(barray); / / 将Base64字符串解码为普通字符串/ / / public static string Base64Decode(string str)byte barray;trybarray = Convert.FromBase64String(str);return Encoding.Default.GetString(barray);catchreturn str;
12、/ / 图片 转换 byte数组/ / / / public static byte image_Image2Byte(Image pic, System.Drawing.Imaging.ImageFormat fmt)MemoryStream mem = new MemoryStream();pic.Save(mem, fmt);mem.Flush();return mem.ToArray();/ / byte数组 转换 图片/ / / public static Image image_Byte2Image(byte bytes)MemoryStream mem = new MemoryS
13、tream(bytes, true);mem.Read(bytes, 0, bytes.Length); mem.Flush();Image aa = Image.FromStream(mem);return aa;/ / ip 转换 长整形/ / / public static long IP2Long(string strIP)long ip = new long4;string s = strIP.Split(.);ip0 = long.Parse(s0);ip1 = long.Parse(s1);ip2 = long.Parse(s2);ip3 = long.Parse(s3);ret
14、urn (ip0 / 长整形 转换 IP/ / / public static string Long2IP(long longIP)StringBuilder sb = new StringBuilder();sb.Append(longIP 24);sb.Append(.);/将高8位置0,然后右移16为sb.Append(longIP & 0x00FFFFFF) 16);sb.Append(.); sb.Append(longIP & 0x0000FFFF) 8);sb.Append(.);sb.Append(longIP & 0x000000FF);return sb.ToString();/ / 将8位日期型整型数据转换为日期字符串数据/ / 整型日期/ 是否以中文年月日输出/ public static string FormatDate(int date, bool chnType)string dateStr = date.ToString();if (date / string型转换为bool型/ / 要转换的字符串/ 缺省值/ 转换后的bool类型结果public static bool StrToBool(object expression, bool defValue)if (expression != null)return StrToBoo