俗话说,一个好汉十个帮,众人拾柴火焰高等都说明一个道理,有更多的资源,更丰富的积累,都是助你走向成功,走向顶峰的推动力。

本篇的公用类库的介绍主题是程序开发中常用到的一些辅助类,在帮助文档中归类到其他目录下面,本篇主要介绍有注册表的操作、反射操作、正则表达式操作、Zip压缩操作、AD操作、字符串Unicode转换操作等常见但有些凌乱的类库。

本篇继续继续整理优化已有的共用类库,并继续发表随笔介绍公用类库的接口方法以及详细使用操作,力求给自己继续优化,积攒更丰富的公用类库资源,加深了解的同时,也给大家展现公用类库好的方面。

厚积薄发,丰富的公用类库积累,助你高效进行系统开发(12)--- 网络相关操作辅助类

厚积薄发,丰富的公用类库积累,助你高效进行系统开发(11)---各种线程相关操作类
厚积薄发,丰富的公用类库积累,助你高效进行系统开发(10)---各种线程同步的集合类
厚积薄发,丰富的公用类库积累,助你高效进行系统开发(9)----各种常用辅助类
厚积薄发,丰富的公用类库积累,助你高效进行系统开发(8)----非对称加密、BASE64加密、MD5等常用加密处理

厚积薄发,丰富的公用类库积累,助你高效进行系统开发(7)-----声音播放、硬件信息、键盘模拟及钩子、鼠标模拟及钩子等设备相关

厚积薄发,丰富的公用类库积累,助你高效进行系统开发(6)----全屏截图、图标获取、图片打印、页面预览截屏、图片复杂操作等

厚积薄发,丰富的公用类库积累,助你高效进行系统开发(5)----热键、多线程、窗体动画冻结等窗体操作

厚积薄发,丰富的公用类库积累,助你高效进行系统开发(4)----CSV、Excel、INI文件、独立存储等文件相关

厚积薄发,丰富的公用类库积累,助你高效进行系统开发(3)----数据库相关操作

厚积薄发,丰富的公用类库积累,助你高效进行系统开发(2)----常用操作

厚积薄发,丰富的公用类库积累,助你高效进行系统开发(1)----开篇总结

1、 注册表操作辅助类RegistryHelper,通过默认指定注册表的前缀路径,减少调用复杂性。

1)辅助类提供的方法接口如下所示:

/// <summary>    
///获取注册表项的值。如果该键不存在,则返回空字符串。/// </summary>    
/// <param name="key">注册表键</param>    
/// <returns>指定键返回的值</returns>    
public static string GetValue(stringkey)/// <summary>    
///获取注册表项的值。如果该键不存在,则返回空字符串。/// </summary>    
/// <param name="softwareKey">注册表键的前缀路径</param>    
/// <param name="key">注册表键</param>    
/// <returns>指定键返回的值</returns>    
public static string GetValue(string softwareKey, stringkey)/// <summary>    
///获取注册表项的值。如果该键不存在,则返回空字符串。/// </summary>    
/// <param name="softwareKey">注册表键的前缀路径</param>    
/// <param name="key">注册表键</param>    
/// <param name="rootRegistry">开始的根节点(Registry.CurrentUser或者Registry.LocalMachine等)</param>    
/// <returns>指定键返回的值</returns>    
public static string GetValue(string softwareKey, stringkey, RegistryKey rootRegistry)/// <summary>    
///保存键值到注册表/// </summary>    
/// <param name="key">注册表键</param>    
/// <param name="value">键的值内容</param>    
/// <returns>如果保存成功返回true,否则为false</returns>    
public static bool SaveValue(string key, stringvalue)/// <summary>    
///保存键值到注册表/// </summary>    
/// <param name="softwareKey">注册表键的前缀路径</param>    
/// <param name="key">注册表键</param>    
/// <param name="value">键的值内容</param>    
/// <returns>如果保存成功返回true,否则为false</returns>    
public static bool SaveValue(string softwareKey, string key, stringvalue)/// <summary>    
///保存键值到注册表/// </summary>    
/// <param name="softwareKey">注册表键的前缀路径</param>    
/// <param name="key">注册表键</param>    
/// <param name="value">键的值内容</param>    
/// <param name="rootRegistry">开始的根节点(Registry.CurrentUser或者Registry.LocalMachine等)</param>    
/// <returns>如果保存成功返回true,否则为false</returns>    
public static bool SaveValue(string softwareKey, string key, stringvalue, RegistryKey rootRegistry)#endregion   
  
#region 自动启动程序设置    
   
/// <summary>    
///检查是否随系统启动/// </summary>    
/// <returns></returns>    
public static boolCheckStartWithWindows()/// <summary>    
///设置随系统启动/// </summary>    
/// <param name="startWin"></param>    
public static void SetStartWithWindows(boolstartWin)#endregion  

2)辅助类的使用例子代码如下所示

private void btnRegistryHelper_Click(objectsender, EventArgs e)    
{
string result = string.Empty;
result
+= "使用RegistryHelper注册表访问辅助类:" + "\r\n";string softwareKey = @"Software\DeepLand\OrderWater";bool sucess = RegistryHelper.SaveValue(softwareKey, "Test", DateTime.Now.ToString());if(sucess)
{
result
+= RegistryHelper.GetValue(softwareKey, "Test") + "\r\n";
}

RegistryHelper.SaveValue(softwareKey,
"Test", "测试内容", Microsoft.Win32.Registry.LocalMachine);
result
+= RegistryHelper.GetValue(softwareKey, "Test", Microsoft.Win32.Registry.LocalMachine);

MessageUtil.ShowTips(result);
}

演示代码效果如下所示。


2、 反射操作辅助类ReflectionUtil,如获取或设置字段、属性的值等反射信息。

本辅助类主要是用来方便实现反射操作辅助类,如获取或设置字段、属性的值等反射信息。 在通用的赋值操作(属性或者字段)、执行方法或者需要获取特定对象属性的时候,就需要大量用到反射操作。

1)辅助类提供的方法接口如下所示:

#region 属性字段设置    
/// <summary>    
///绑定标识/// </summary>    
public static BindingFlags bf = BindingFlags.DeclaredOnly | BindingFlags.Public |BindingFlags.NonPublic| BindingFlags.Instance |BindingFlags.Static;/// <summary>    
///执行方法/// </summary>    
/// <param name="obj">对象实例</param>    
/// <param name="methodName">方法名称</param>    
/// <param name="args">参数</param>    
/// <returns></returns>    
public static object InvokeMethod(object obj, string methodName, object[] args)/// <summary>    
///设置对象实例的字段值/// </summary>    
/// <param name="obj">对象实例</param>    
/// <param name="name">字段名称</param>    
/// <param name="value">字段值</param>    
public static void SetField(object obj, string name, objectvalue)/// <summary>    
///获取对象实例的字段值/// </summary>    
/// <param name="obj">对象实例</param>    
/// <param name="name">字段名称</param>    
/// <returns></returns>    
public static object GetField(object obj, stringname)/// <summary>    
///获取对象实例的字段集合/// </summary>    
/// <param name="obj">对象实例</param>    
/// <returns></returns>    
public static FieldInfo[] GetFields(objectobj)/// <summary>    
///设置对象实例的属性值/// </summary>    
/// <param name="obj">对象实例</param>    
/// <param name="name">属性名称</param>    
/// <param name="value">属性值</param>    
public static void SetProperty(object obj, string name, objectvalue)/// <summary>    
///获取对象实例的属性值/// </summary>    
/// <param name="obj">对象实例</param>    
/// <param name="name">属性名称</param>    
/// <returns></returns>    
public static object GetProperty(object obj, stringname)/// <summary>    
///获取对象实例的属性列表/// </summary>    
/// <param name="obj">对象实例</param>    
/// <returns></returns>    
public static PropertyInfo[] GetProperties(objectobj)/// <summary>    
///把对象的属性和值,输出一个键值的字符串,如A=1&B=test/// </summary>    
/// <param name="obj">实体对象</param>    
/// <param name="includeEmptyProperties">是否包含空白属性的键值</param>    
/// <returns></returns>    
public static string ToNameValuePairs(object obj, bool includeEmptyProperties = true)#endregion   
  
#region 获取Description    
   
/// <summary>    
///获取枚举字段的Description属性值/// </summary>    
/// <param name="value">The value.</param>    
/// <returns>return description or value.ToString()</returns>    
public static stringGetDescription(Enum value)/// <summary>    
///Get The Enum Field Description using Description Attribute and///objects to format the Description./// </summary>    
/// <param name="value">Enum For Which description is required.</param>    
/// <param name="args">An Object array containing zero or more objects to format.</param>    
/// <returns>return null if DescriptionAttribute is not found or return type description</returns>    
public static string GetDescription(Enum value, params object[] args)/// <summary>    
///获取字段的Description属性值/// </summary>    
/// <param name="member">Specified Member for which Info is Required</param>    
/// <returns>return null if DescriptionAttribute is not found or return type description</returns>    
public static stringGetDescription(MemberInfo member)/// <summary>    
///Get The Type Description using Description Attribute and///objects to format the Description./// </summary>    
/// <param name="member">Specified Member for which Info is Required</param>    
/// <param name="args">An Object array containing zero or more objects to format.</param>    
/// <returns>return<see cref="String.Empty"/>if DescriptionAttribute is///not found or return type description</returns>    
public static string GetDescription(MemberInfo member, params object[] args)#endregion   
  
#region 获取Attribute信息    
   
/// <summary>    
///获取指定对象实例的attributes内容/// </summary>    
/// <param name="attributeType">The attribute Type for which the custom attributes are to be returned.</param>    
/// <param name="assembly">the assembly in which the specified attribute is defined</param>    
/// <returns>Attribute as Object or null if not found.</returns>    
public static objectGetAttribute(Type attributeType, Assembly assembly)/// <summary>    
///获取指定对象实例的attributes内容/// </summary>    
/// <param name="attributeType">The attribute Type for which the custom attributes are to be returned.</param>    
/// <param name="type">the type on which the specified attribute is defined</param>    
/// <returns>Attribute as Object or null if not found.</returns>    
public static objectGetAttribute(Type attributeType, MemberInfo type)/// <summary>    
///Gets the specified object attributes for type as specified by type with option to serach parent/// </summary>    
/// <param name="attributeType">The attribute Type for which the custom attributes are to be returned.</param>    
/// <param name="type">the type on which the specified attribute is defined</param>    
/// <param name="searchParent">if set to<see langword="true"/>[search parent].</param>    
/// <returns>    
///Attribute as Object or null if not found./// </returns>    
public static object GetAttribute(Type attributeType, MemberInfo type, boolsearchParent)/// <summary>    
///Gets the collection of all specified object attributes for type as specified by type/// </summary>    
/// <param name="attributeType">The attribute Type for which the custom attributes are to be returned.</param>    
/// <param name="type">the type on which the specified attribute is defined</param>    
/// <returns>Attribute as Object or null if not found.</returns>    
public static object[] GetAttributes(Type attributeType, MemberInfo type)/// <summary>    
///Gets the collection of all specified object attributes for type as specified by type with option to serach parent/// </summary>    
/// <param name="attributeType">The attribute Type for which the custom attributes are to be returned.</param>    
/// <param name="type">the type on which the specified attribute is defined</param>    
/// <param name="searchParent">The attribute Type for which the custom attribute is to be returned.</param>    
/// <returns>    
///Attribute as Object or null if not found./// </returns>    
public static object[] GetAttributes(Type attributeType, MemberInfo type, boolsearchParent)#endregion   
  
#region 资源获取    
   
/// <summary>    
///根据资源名称获取图片资源流/// </summary>    
/// <param name="ResourceName"></param>    
/// <returns></returns>    
public static Stream GetImageResource(stringResourceName)/// <summary>    
///获取程序集资源的位图资源/// </summary>    
/// <param name="assemblyType">程序集中的某一对象类型</param>    
/// <param name="resourceHolder">资源的根名称。例如,名为“MyResource.en-US.resources”的资源文件的根名称为“MyResource”。</param>    
/// <param name="imageName">资源项名称</param>    
public static Bitmap LoadBitmap(Type assemblyType, string resourceHolder, stringimageName)/// <summary>    
///获取程序集资源的文本资源/// </summary>    
/// <param name="assemblyType">程序集中的某一对象类型</param>    
/// <param name="resName">资源项名称</param>    
/// <param name="resourceHolder">资源的根名称。例如,名为“MyResource.en-US.resources”的资源文件的根名称为“MyResource”。</param>    
public static string GetStringRes(Type assemblyType, string resName, stringresourceHolder)/// <summary>    
///获取程序集嵌入资源的文本形式/// </summary>    
/// <param name="assemblyType">程序集中的某一对象类型</param>    
/// <param name="charset">字符集编码</param>    
/// <param name="ResName">嵌入资源相对路径</param>    
/// <returns>如没找到该资源则返回空字符</returns>    
public static string GetManifestString(Type assemblyType, string charset, stringResName)#endregion   
  
#region 创建对应实例    
/// <summary>    
///创建对应实例/// </summary>    
/// <param name="type">类型</param>    
/// <returns>对应实例</returns>    
public static object CreateInstance(stringtype)/// <summary>    
///创建对应实例/// </summary>    
/// <param name="type">类型</param>    
/// <returns>对应实例</returns>    
public static objectCreateInstance(Type type)#endregion  

2)辅助类的使用例子代码如下所示,例子是Socket框架里面用来反序列化对象的操作,通过反射把字符串的值赋值到对象里面。

/// <summary>    
///转换Socket接收到的信息为对象信息/// </summary>    
/// <param name="data">Socket接收到的信息</param>    
public BaseEntity(stringdata)    
{
string[] dataArray =NetStringUtil.UnPack(data);if (dataArray != null && dataArray.Length > 0)
{
int i = 0;
FieldInfo[] fieldArray
= ReflectionUtil.GetFields(this);if (fieldArray == null || dataArray.Length !=fieldArray.Length)
{
throw new ArgumentException("收到的信息和字段信息不一致");
}
if (fieldArray != null)
{
foreach (FieldInfo info infieldArray)
{
string strValue = dataArray[i++];try{if (!string.IsNullOrEmpty(strValue))
{
ReflectionUtil.SetField(
this, info.Name, strValue);
}
}
catch(Exception ex)
{
Communication.Model.Log.WriteError(
string.Format("转换数据发生错误:{0}【{1}】",
ex.Message, data));
break;
}
}
}
}
}

例如两个对象的属性基本相同,可以进行对象的拷贝赋值。

#region 表结构一样,复制相关的内容    PilotInfo pilotInfo= BLLFactory<Pilot>.Instance.FindByID(txtPilot.PilotID);if (pilotInfo != null)    
{
PropertyInfo[] outPropertyArray
=ReflectionUtil.GetProperties(info);
PropertyInfo[] pilotPropArray
=ReflectionUtil.GetProperties(pilotInfo);if (pilotPropArray != null && outPropertyArray != null)
{
foreach (PropertyInfo propInfo inpilotPropArray)
{
try{object value =ReflectionUtil.GetProperty(pilotInfo, propInfo.Name);foreach (PropertyInfo outInfo inoutPropertyArray)
{
if (outInfo.Name ==propInfo.Name)
{
ReflectionUtil.SetProperty(info, propInfo.Name, value);
}
}
}
catch(Exception ex)
{
}
}
}
}
#endregion

3、程序集反射创建对象实例辅助类 Reflect

本辅助类主要是用来方便实现程序集反射创建对象实例的操作。 该程序集反射春节对象实例的操作,在我的WInform开发框架中用于对象的创建操作。

1)辅助类提供的方法接口如下所示:

/// <summary>    
///程序集反射创建对象实例辅助类/// </summary>    
/// <typeparam name="T">对象类型</typeparam>    
public class Reflect<T> where T : class{/// <summary>    
    ///缓存的hash表集合/// </summary>    
    public staticHashtable ObjCache/// <summary>    
    ///根据参数创建对象实例/// </summary>    
    /// <param name="sName">对象全局名称</param>    
    /// <param name="sFilePath">文件路径</param>    
    /// <returns></returns>    
    public static T Create(string sName, stringsFilePath)/// <summary>    
    ///根据参数创建对象实例/// </summary>    
    /// <param name="sName">对象全局名称</param>    
    /// <param name="sFilePath">文件路径</param>    
    /// <param name="bCache">缓存集合</param>    
    /// <returns></returns>    
    public static T Create(string sName, string sFilePath, boolbCache)/// <summary>    
    ///创建程序集对象/// </summary>    
    /// <param name="sFilePath">文件路径</param>    
    /// <returns></returns>    
    public static Assembly CreateAssembly(stringsFilePath)    
}

2)辅助类的使用例子代码如下所示(摘自我的Winform开发框架里面的对象反射操作)。
在下面的例子中,通过传入的类型T,在程序集中构造制定类型的对象,并放入到缓存当中。

public class BLLFactory<T> where T : class{private static Hashtable objCache = newHashtable();private static object syncRoot = newObject();public staticT Instance    
{
get{string CacheKey = typeof(T).FullName;
T bll
= (T)objCache[CacheKey];  //从缓存读取 if (bll == null)
{
lock(syncRoot)
{
if (bll == null)
{
bll
= Reflect<T>.Create(typeof(T).FullName, "WHC.WareHouseMis"); //反射创建,并缓存//bll = Reflect<T>.Create(typeof(T).FullName, System.Reflection.Assembly.GetExecutingAssembly().GetName().Name);//反射创建,并缓存 objCache.Add(typeof(T).FullName, bll);
}
}
}
returnbll;
}
}
}

4、 正则表达式辅助类 CRegex

本辅助类主要是用来方便实现对正则表达式的封装使用,通过利用该类库,可以简化C#对正则表达式的操作,但不能替代掌握正则表达式的知识。
正则表达式可以通过The Reguator等正则表达式调试工具来验证编写的表达式,基本的操作是匹配,替换,分割等,调试工具界面如下:

1)软件包含下面基础通用的操作正则表达式方法的定义,以及一些封装好的正则表达式操作,如获取HTML标题、Body等内容。

/// <summary>    
///内容是否匹配指定的表达式/// </summary>    
/// <param name="sInput">输入内容</param>    
/// <param name="sRegex">表达式字符串</param>    
/// <returns></returns>    
public static bool IsMatch(string sInput, stringsRegex)/// <summary>    
///多个匹配内容/// </summary>    
/// <param name="sInput">输入内容</param>    
/// <param name="sRegex">表达式字符串</param>    
/// <param name="iGroupIndex">第几个分组, 从1开始, 0代表不分组</param>    
public static List<string> GetList(string sInput, string sRegex, intiGroupIndex)/// <summary>    
///多个匹配内容/// </summary>    
/// <param name="sInput">输入内容</param>    
/// <param name="sRegex">表达式字符串</param>    
/// <param name="sGroupName">分组名, ""代表不分组</param>    
public static List<string> GetList(string sInput, string sRegex, stringsGroupName)/// <summary>    
///单个匹配内容/// </summary>    
/// <param name="sInput">输入内容</param>    
/// <param name="sRegex">表达式字符串</param>    
/// <param name="iGroupIndex">分组序号, 从1开始, 0不分组</param>    
public static string GetText(string sInput, string sRegex, intiGroupIndex)/// <summary>    
///单个匹配内容/// </summary>    
/// <param name="sInput">输入内容</param>    
/// <param name="sRegex">表达式字符串</param>    
/// <param name="sGroupName">分组名, ""代表不分组</param>    
public static string GetText(string sInput, string sRegex, stringsGroupName)/// <summary>    
///替换指定内容/// </summary>    
/// <param name="sInput">输入内容</param>    
/// <param name="sRegex">表达式字符串</param>    
/// <param name="sReplace">替换值</param>    
/// <param name="iGroupIndex">分组序号, 0代表不分组</param>    
public static string Replace(string sInput, string sRegex, string sReplace, intiGroupIndex)/// <summary>    
///替换指定内容/// </summary>    
/// <param name="sInput">输入内容</param>    
/// <param name="sRegex">表达式字符串</param>    
/// <param name="sReplace">替换值</param>    
/// <param name="sGroupName">分组名, "" 代表不分组</param>    
public static string Replace(string sInput, string sRegex, string sReplace, stringsGroupName)/// <summary>    
///分割指定内容/// </summary>    
/// <param name="sInput">输入内容</param>    
/// <param name="sRegex">表达式字符串</param>    
/// <param name="iStrLen">最小保留字符串长度</param>    
public static List<string> Split(string sInput, string sRegex, int iStrLen)  

5、 压缩文本、字节或者文件的压缩辅助类GZipUtil。

本辅助类主要是用来方便实现压缩文本、字节或者文件的压缩操作。本辅助类使用.NET内置的类库实现压缩操作。

1)辅助类提供的方法接口如下所示:

/// <summary>    
///压缩字符串/// </summary>    
/// <param name="text">待压缩的文本</param>    
/// <returns>压缩后的文本内容</returns>    
public static string Compress(stringtext)/// <summary>    
///解压字符串/// </summary>    
/// <param name="compressedText">待解压缩的文本内容</param>    
/// <returns>解压后的原始文本内容</returns>    
public static string Decompress(stringcompressedText)/// <summary>    
///压缩流对象/// </summary>    
/// <typeparam name="T">对象类型</typeparam>    
/// <param name="stream">流数据</param>    
/// <param name="mode">压缩类型</param>    
/// <returns></returns>    
public static T GZip<T>(Stream stream, CompressionMode mode) whereT : Stream/// <summary>    
///压缩字节/// </summary>    
/// <param name="bytData">待压缩字节</param>    
/// <returns>压缩后的字节数组</returns>    
public static byte[] Compress(byte[] bytData)/// <summary>    
///解压字节/// </summary>    
/// <param name="bytData">待解压缩字节</param>    
/// <returns>解压后的原始字节内容</returns>    
public static byte[] Decompress(byte[] bytData)/// <summary>    
///压缩Object对象到字节数组/// </summary>    
/// <param name="obj">待压缩对象</param>    
/// <returns>压缩后的字节数组</returns>    
public static byte[] ObjectToGZip(objectobj)/// <summary>    
///从压缩的字节数组转换到Object对象/// </summary>    
/// <param name="byteArray">待解压缩的字节数据</param>    
/// <returns>对象</returns>    
public static object GZipToObject(byte[] byteArray)/// <summary>    
///压缩文件/// </summary>    
/// <param name="lpSourceFolder">包括在zip文件中的文件夹路径,所有文件,包括子文件夹中的文件将包括在内。</param>    
/// <param name="lpDestFolder">写入到Zip文件的目标文件夹</param>    
/// <param name="zipFileName">zip文件名称</param>    
public static GZipResult Compress(string lpSourceFolder, string lpDestFolder, stringzipFileName)/// <summary>    
///压缩文件/// </summary>    
/// <param name="lpSourceFolder">包括在zip文件中的源文件夹路径</param>    
/// <param name="searchPattern">搜索模式 (例如 "*.*" or "*.txt" or "*.gif") 以标识那些文件将被包含到Zip文件里面</param>    
/// <param name="searchOption">指定是搜索当前目录,还是搜索当前目录及其所有子目录</param>    
/// <param name="lpDestFolder">写入zip目标文件夹的路径</param>    
/// <param name="zipFileName">Zip文件名</param>    
/// <param name="deleteTempFile">布尔值,如果为true则删除中间临时文件,false则在lpDestFolder保留临时文件(调试用)</param>    
public static GZipResult Compress(string lpSourceFolder, string searchPattern, SearchOption searchOption, string lpDestFolder, string zipFileName, booldeleteTempFile)/// <summary>    
///压缩文件/// </summary>    
/// <param name="files">在zip文件中包含的FileInfo对象数组</param>    
/// <param name="folders">文件夹字符串数组</param>    
/// <param name="lpBaseFolder">    
///基础文件夹,在创建的zip文件中存储的文件的相对路径。例如, 如果lpBaseFolder 是 'C:\zipTest\Files\',///当存在一个文件 'C:\zipTest\Files\folder1\sample.txt' 在数组'files'里面, 则 sample.txt 的相对路径是 'folder1/sample.txt'/// </param>    
/// <param name="lpDestFolder">写入Zip文件的文件夹</param>    
/// <param name="zipFileName">Zip文件名</param>    
public static GZipResult Compress(FileInfo[] files, string[] folders, string lpBaseFolder, string lpDestFolder, stringzipFileName)/// <summary>    
///压缩文件/// </summary>    
/// <param name="files">在zip文件中包含的FileInfo对象数组</param>    
/// <param name="lpBaseFolder">    
///基础文件夹,在创建的zip文件中存储的文件的相对路径。例如, 如果lpBaseFolder 是 'C:\zipTest\Files\',///当存在一个文件 'C:\zipTest\Files\folder1\sample.txt' 在数组'files'里面, 则 sample.txt 的相对路径是 'folder1/sample.txt'/// </param>    
/// <param name="lpDestFolder">写入Zip文件的文件夹</param>    
/// <param name="zipFileName">Zip文件名</param>    
public static GZipResult Compress(FileInfo[] files, string lpBaseFolder, string lpDestFolder, stringzipFileName)/// <summary>    
///压缩文件/// </summary>    
/// <param name="files">在zip文件中包含的FileInfo对象数组</param>    
/// <param name="lpBaseFolder">    
///基础文件夹,在创建的zip文件中存储的文件的相对路径。例如, 如果lpBaseFolder 是 'C:\zipTest\Files\',///当存在一个文件 'C:\zipTest\Files\folder1\sample.txt' 在数组'files'里面, 则 sample.txt 的相对路径是 'folder1/sample.txt'/// </param>    
/// <param name="lpDestFolder">写入Zip文件的文件夹</param>    
/// <param name="zipFileName">Zip文件名</param>    
/// <param name="deleteTempFile">布尔值,如果为true则删除中间临时文件,false则在lpDestFolder保留临时文件(调试用)</param>    
public static GZipResult Compress(FileInfo[] files, string lpBaseFolder, string lpDestFolder, string zipFileName, booldeleteTempFile)/// <summary>    
///解压文件/// </summary>    
/// <param name="lpSourceFolder">zip文件的源目录</param>    
/// <param name="lpDestFolder">解压到的目录</param>    
/// <param name="zipFileName">Zip文件名</param>    
/// <returns></returns>    
public static GZipResult Decompress(string lpSourceFolder, string lpDestFolder, stringzipFileName)/// <summary>    
///解压文件/// </summary>    
/// <param name="lpSourceFolder">zip文件的源目录</param>    
/// <param name="lpDestFolder">解压到的目录</param>    
/// <param name="zipFileName">Zip文件名</param>    
/// <param name="writeFiles"></param>    
/// <param name="addExtension">增加后缀名</param>    
/// <returns></returns>    
public static GZipResult Decompress(string lpSourceFolder, string lpDestFolder, string zipFileName, bool writeFiles, stringaddExtension)/// <summary>    
///解压文件/// </summary>    
/// <param name="lpSourceFolder">zip文件的源目录</param>    
/// <param name="lpDestFolder">解压到的目录</param>    
/// <param name="zipFileName">Zip文件名</param>    
/// <param name="deleteTempFile">布尔值,如果为true则删除中间临时文件,false则在lpDestFolder保留临时文件(调试用)</param>    
/// <param name="writeFiles"></param>    
/// <param name="addExtension">增加后缀名</param>    
/// <param name="htFiles"></param>    
/// <param name="bufferSize"></param>    
/// <returns></returns>    
public static GZipResult Decompress(string lpSrcFolder, string lpDestFolder, string zipFileName, bool deleteTempFile, bool writeFiles, string addExtension, Hashtable htFiles, int bufferSize)   

2)辅助类的使用例子代码如下所示

//压缩解压缩文本内容
string zippedContent = GZipUtil.Compress("wuhuacong");string original =GZipUtil.Decompress(zippedContent);    

GZipUtil.Compress(Application.StartupPath, Application.StartupPath,
"cityroad.zip");
GZipUtil.Decompress(Application.StartupPath, Path.Combine(Application.StartupPath,
"cityroad"), "cityroad.zip");

MessageUtil.ShowTips(
"操作完成");

6、字符串Unicode转换操作辅助类 UnicodeHelper。

1)辅助类提供的方法接口如下所示:

/// <summary>    
///将原始字串转换为unicode,格式为\u.\u./// </summary>    
/// <param name="str">待转换字符串</param>    
/// <returns></returns>    
public static string StringToUnicode(stringstr)/// <summary>    
///将Unicode字串\u.\u.格式字串转换为原始字符串/// </summary>    
/// <param name="str">待转换字符串</param>    
/// <returns></returns>    
public static string UnicodeToString(stringstr)/// <summary>    
///GB2312转换成unicode编码/// </summary>    
/// <param name="str">待转换字符串</param>    
/// <returns></returns>    
public static string GBToUnicode(stringstr)/// <summary>    
///转换一个字符,输入如"Π"中的"03a0"/// </summary>    
/// <param name="unicodeSingle">待转换字符串</param>    
/// <returns></returns>    
public static string ConvertSingle(stringunicodeSingle)/// <summary>    
///unicode编码转换成GB2312汉字/// </summary>    
/// <param name="str">待转换字符串</param>    
/// <returns></returns>    
public static string UnicodeToGB(string str)  

2)辅助类的使用例子代码如下所示

private void btnUnicodeHelper_Click(objectsender, EventArgs e)    
{
string str = "\u821e\u7fbd\u6e05\u548c \u5c71\u7f8a\u4e4b\u89d2";string test =UnicodeHelper.UnicodeToString(str);string result = test + "\r\n";
result
+= UnicodeHelper.StringToUnicode(test) + "\r\n";

MessageUtil.ShowTips(result);
}

标签: none

添加新评论