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

自从上篇随笔《厚积薄发,丰富的公用类库积累,助你高效进行系统开发(1)》一文以来,得到同行很多人的鼎力支持和关注,并且在大家的推动下,这篇文章也是上榜博客头条、编辑推荐、10天内推荐排行等荣誉,很多人对这些类库很是感兴趣,也希望进一步详细介绍相关类库的使用。本随笔系列将逐步介绍相关的类库的详细使用,并逐步整理成CHM的帮助文档,作为类库使用的指引手册,同时我会对类库进行进一步的丰富、提炼和优化,随笔将逐步发送,感谢大家的支持和鼓励。

1、程序配置管理辅助类 AppConfig 

实现效果
     1、 本辅助类主要是用来方便获取或设置系统配置项的辅助类,实现快速读写配置文件的内容,可以用于读取*.exe.Config文件或者Web.config的文件内容,或者可以读取指定文件的配置项。

    2、 辅助类默认从当前目录中按顺序检索Web.Config和*.exe.Config文件。如果找到一个,则使用它作为默认的配置文件,不需要指定文件路径。 

    3、 读取的文件格式是一般的XML配置文件格式,如下所示。 

  1. <? xml version="1.0" encoding="utf-8"  ?>    
    < configuration >    
      
    < configSections >    
        
    < section  name ="dataConfiguration"    
                 type
    ="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data" />    
      
    </ configSections >    
      
    < connectionStrings >    
        
    < add  name ="DataAccess"  providerName ="System.Data.SqlClient"    
             connectionString
    ="Persist Security Info=False;Data Source=(local);Initial Catalog=Warehouse;User ID=sa;Password=123456" />    
      
    </ connectionStrings >    
      
    < dataConfiguration  defaultDatabase ="DataAccess" />    
       
      
    < appSettings >    
        
    <!-- 软件名称 -->    
        
    < add  key ="ApplicationName"  value ="深田之星仓库管理系统" />    
        
    <!-- 开发商名称 -->    
        
    < add  key ="Manufacturer"  value ="广州爱启迪技术有限公司" />    
        
    <!-- 字典、权限组件的数据库类型:access、sqlserver等,默认为sqlserver可不写 -->    
        
    < add  key ="ComponentDbType"  value ="sqlserver" />    
      
    </ appSettings >    
    </ configuration >   

实现代码

    1、 读取配置项目。

AppConfig config  =   new  AppConfig();    
string  Manufacturer  =  config.AppConfigGet( " Manufacturer " );    
string  ApplicationName  =  config.AppConfigGet( " ApplicationName " );    
string  AppWholeName  =   string .Format( " {0}-{1} " , Manufacturer, ApplicationName);  

  2、 读取复杂的连接字符串配置,如上面的EnterpriseLibrary的连接字符串 DataAccess 配置项目的ConnectString。 
///   <summary>     
///  测试数据库是否正常连接    
///   </summary>     
///   <returns></returns>     
public   static   bool  TestConnection( string  connectionString)    
{    
    
bool  result  =   false ;    
   
    
using  (DbConnection connection  =   new  SqlConnection(connectionString))    
    {    
        connection.Open();    
        
if  (connection.State  ==  System.Data.ConnectionState.Open)    
        {    
            result 
=   true ;    
        }    
    }    
   
    
return  result;    
}    
   
public   static   bool  TestConnection()    
{    
    AppConfig config 
=   new  AppConfig();    
    
return  TestConnection(config.GetConnectionString( " DataAccess " ));    
}  

 

3、 写入配置项内容。
AppConfig config  =   new  AppConfig();    
// 保存地址(标准调用)    
config.AppConfigSet( " PictureRootDir " this .txtPath.Text);    
   
// 保存EnterpriseLibray数据库配置项(自定义设置)    
string  connectionString  =   string .Format( " Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}Database1.mdb;User ID=Admin;Jet OLEDB:Database Password=; " , System.AppDomain.CurrentDomain.BaseDirectory);    
config.SetConnectionString(
" DataAccess " , connectionString);    
// 更新配置文件,以便起效    
ConfigurationManager.RefreshSection( " dataConfiguration " );    
ConfigurationManager.RefreshSection(
" connectionStrings " );   
ConfigurationManager.RefreshSection("appSettings"); 

  

2、消息提示对话框辅助类 MessageUtil

 实现效果

   1、本辅助类主要是用来方便实现标准统一的消息提示对话框,由于各种按钮的样式、标题都进行了统一分装,因此调用的时候,这些参数都可以不用考虑,省却很多繁琐的参数指定,是Winform开发中非常常用的一个类库辅助类。
 

   2、封装的消息提示对话框包括个各种常用的对话框,如下所示:

 

实现代码

MessageUtil.ShowTips( " 提示信息对话框 " );    
MessageUtil.ShowWarning(
" 警告消息提示框 " );    
MessageUtil.ShowError(
" 错误消息提示框 " );    
   
MessageUtil.ShowYesNoAndTips(
" 提示对话框,有Yes/No按钮 " );    
MessageUtil.ShowYesNoAndWarning(
" 警告对话框,有Yes/No按钮 " );    
MessageUtil.ShowYesNoAndError(
" 错误对话框,有Yes/No按钮 " );    
MessageUtil.ShowYesNoCancelAndTips(
" 提示对话框,有Yes/No/Cancel按钮 " );    
   
MessageUtil.ConfirmYesNo(
" 确认对话框,有Yes/No对话框 " );  
MessageUtil.ConfirmYesNoCancel("确认对话框,有Yes/No/Cancel对话框"); 

 

 3、日历类辅助类 CCalendar

  实现效果

     1、 本辅助类主要是用来方便显示日期时间、农历、生肖的日历类,并可以计算农历很多属性,并且通过一个函数提供一个全面的日期信息,可以用于装饰界面的效果。

     2、  其效果如下所示

      

  

 实现代码  

CCalendar cal = new CCalendar();  
this
.lblCalendar.Text  =  cal.GetDateInfo(System.DateTime.Now).Fullinfo;  


一般节日会提示相关的节日信息,另外可以通过修改XML文件,实现对节日、时间提示等信息调整。 

<?xml version="1.0" encoding="gb2312" ?>   
<
HELLO >    
   
  
<!--  公历节日开始  -->    
  
< AD >    
    
< feast  day ="0101"  name ="元旦"  sayhello ="yes" >    
      
< hello > 新年好!祝您在新的一年里身体健康,事业进步! </ hello >    
      
<!--  从网站根目录算起  -->    
      
< img > ./img/theme/0101.gif </ img >    
    
</ feast >    
    
< feast  day ="0202"  name ="世界湿地日"  sayhello ="no" >    
      
< img ></ img >    
      
< hello ></ hello >    
    
</ feast >    
    
< feast  day ="0210"  name ="国际气象节"  sayhello ="no" >    
      
< img ></ img >    
      
< hello ></ hello >    
    
</ feast >    
    
< feast  day ="0214"  name ="情人节"  sayhello ="yes" >    
      
< hello > 祝天下有情人终成眷属! </ hello >    
      
< img > ./img/theme/0214.gif </ img >    
    
</ feast >    
    
< feast  day ="0301"  name ="世界图书日"  sayhello ="no" >    
      
< img ></ img >    
      
< hello ></ hello >    
    
</ feast >    
.............

 

4、托盘图标辅助类 NotifyIconHelper 

 实现效果

    1、 本辅助类主要是用来方便动态设置托盘图标。该辅助类用于一些实时连接要求或者状态变化能够及时通过图表来显示的程序,通过闪动的图标及文本,可以有效提示用户相关的程序状态,提供用户的使用体验。

    2、  动态设置托盘图标,其效果如下所示

    

 

 实现步骤

    1、在主窗体添加一个NotifyIcon组件,并设置好相关的属性,如下所示

    2、  在代码引用相关的代码实现动态调用。


 实现代码

public   partial   class  FrmMain : Form    
{    
    
private  NotifyIconHelper notifyHelper;    
    
private   const   string  ClientName  =   " 数据采集终端 " ; // PC式采集器终端    
   
    
public  frmMain()    
    {    
        InitializeComponent();    
   
        
this .Text  =  ClientName;    
        
// 初始化托盘图标    
        notifyHelper  =   new  NotifyIconHelper( this .notifyIcon1);    
        notifyHelper.Icon_Conntected 
=  Resources.Connected;    
        notifyHelper.Icon_Shrink1 
=  Resources.shrink1;    
        notifyHelper.Icon_Shrink2 
=  Resources.shrink2;    
        notifyHelper.Icon_UnConntect 
=  Resources.unConnected;    
        notifyHelper.Text_Conntected 
=   string .Format( " {0}:终端已经连接 " , ClientName);    
        notifyHelper.Text_UnConntect 
=   string .Format( " {0}:终端未连接 " , ClientName);    
        notifyHelper.NotifyStatus 
=  NotifyIconHelper.Status.TwinkleNotice;    
    }    
   
    
///   <summary>     
    
///  设置托盘图标的状态    
    
///   </summary>     
    
///   <param name="status"></param>     
     public   void  SetNotifyStatus(NotifyIconHelper.Status status)    
    {                
        notifyHelper.NotifyStatus 
=  status;    
   
        
if  (status  ==  NotifyIconHelper.Status.Offline)    
        {    
            
this .Invoke( new  MethodInvoker( delegate ()    
            {    
                
this .Icon  =  notifyHelper.Icon_UnConntect;    
            }));                    
        }    
        
else   if  (status  ==  NotifyIconHelper.Status.Online)    
        {    
            
this .Invoke( new  MethodInvoker( delegate ()    
            {    
                
this .Icon  =  notifyHelper.Icon_Conntected;    
            }));       
        }    
    }  

 

5、DataTable操作辅助类 DataTableHelper 

实现效果

   1、本辅助类主要是用来方便对DataTable进行相关操作的辅助类,该类是非常常用的工具类,常用与数据显示、转换和报表生成等操作中。

   2、  提供的操作,包括有创建表、DataTable和实体类集合IList<T>相互转化、表排序、表过滤等操作,以求达到快速进行DataTable操作的目的。

实现代码

    1、 根据字段创建表对象,多个列用逗号(,)分开,默认为表对象的列为string。

string  columns  =   @" 流水号,备注,供货商,操作员,库房名称,备件编号(pm码),备件名称,图号,规格型号,材质,备件属类,备件类别,
单位,最新单价(元),入库数量,总价,入库日期,来源,库位,部门,使用位置
" ;    
DataTable dt 
=  DataTableHelper.CreateTable(columns);   

2、根据字段创建表对象,多个列用逗号(,)分开。如需要制定列的类型,在字段后加上“|int”格式的字符。 
string  tableColumns  =   " ID|int,ItemNo,ItemName,StockQuantity|int,Manufacture,MapNo,Specification,Material,ItemBigType,ItemType,
Unit,Price | decimal ,Source,StoragePos,UsagePos,Note,WareHouse,Dept " ;    
DataTable dt  =  DataTableHelper.CreateTable(tableColumns);  

3、 实体类转DataTable对象操作ListToDataTable,其对应的反操作函数DataTableToList使用方法类似。
string   where   =  GetSearchSql();    
IList
< CustomerInfo >  list  =  BLLFactory < Customer > .Instance.Find( where this .winGridViewPager1.PagerInfo);    
DataTable dt 
=  DataTableHelper.ToDataTable < CustomerInfo > (list);    
this .winGridViewPager1.DataSource  =  dt.DefaultView;    
this .winGridViewPager1.dataGridView1.Refresh(); 
 

4、 DataTable对象排序操作SortedTable,可对表多列进行排序操作。

string   where   =  GetSearchSql();    
IList < CustomerInfo >  list  =  BLLFactory < Customer > .Instance.Find( where this .winGridViewPager1.PagerInfo);    
DataTable dt  =  DataTableHelper.ToDataTable < CustomerInfo > (list);    
DataTable dtSort  =  DataTableHelper.SortedTable(dt,  new   string []{ " Number asc " " Type desc " });


最新公用类库DLL+XML注释文件下载地址是:

http://files.cnblogs.com/wuhuacong/WHC.OrderWater.Commons.rar

 

由于篇幅原因,下篇继续整理发布出来,谢谢支持。

WHC.OrderWater.Commons 伍华聪 公共类源码 价值680元 ----------Database-------------- 1.DataTable帮类(DataTableHelper.cs) 2.Access数据库文件操作类(JetAccessUtil.cs) 5.查询条件组合辅类(SearchCondition.cs) 6.查询信息实体类(SearchInfo.cs) 8.Sql命令操作函数(可用于安装程序的时候数据库脚本执行)(SqlScriptHelper.cs) ----------Device-------------- 声音播放辅类(AudioHelper.cs) 摄像头操作类,包括开启、关闭、抓图、设置等功能(Camera.cs) 提供用于操作【剪切板】的方法(ClipboardHelper.cs) 获取电脑信息(Computer.cs) 提供用户硬件唯一信息的辅类(FingerprintHelper.cs) 读取指定盘符的硬盘序列号(HardwareInfoHelper.cs) 提供访问键盘当前状态的属性(KeyboardHelper.cs) 全局键盘钩子。这可以用来在全球范围内捕捉键盘输入。(KeyboardHook.cs) 模拟鼠标点击(MouseHelper.cs) 全局鼠标钩子。这可以用来在全球范围内捕获鼠标输入。(MouseHook.cs) MP3文件播放操作类(MP3Helper.cs) 关联文件(ExtensionAttachUtil.cs) 注册文件关联的辅类(FileAssociationsHelper.cs) 打开、保存文件对话框操作类(FileDialogHelper.cs) 常用的文件操作类FileUtil(FileUtil.cs) INI文件操作类(INIFileUtil.cs) 独立存储操作类(IsolatedStorageHelper.cs) 序列号操作类(Serializer.cs) 获取一个对象,它提供用于访问经常引用的目录的属性。(SpecialDirectories.cs) 简单的Word操作对象(WordCombineUtil.cs) 这个类提供了一些实用的方法来转换XML和对象。(XmlConvertor.cs) XML操作类(XmlHelper.cs) ----------Format-------------- 参数验证的通用验证程序。(ArgumentValidation.cs) 这个类提供了实用方法的字节数组和图像之间的转换。(ByteImageConvertor.cs) byte字节数组操作类(BytesTools.cs) 处理数据类型转换,数制转换、编码转换相关的类(ConvertHelper.cs) CRC校验辅类(CRCUtils.cs) 枚举操作公共类(EnumHelper.cs) 身份证操作类(IDCardHelper.cs) 检测字符编码的类(IdentifyEncoding.cs) RGB颜色操作类(MyColors.cs) 日期操作类(MyDateTime.cs) 转换人民币大小金额辅类(RMBUtil.cs) 常用的字符串常量(StringConstants.cs) 简要说明TextHelper。(StringUtil.cs) 获取中文字首字拼写,随机发生器,按指定概率随机执行操作(Util.cs) 各种输入格式验证辅类(ValidateUtil.cs) ----------Network-------------- Cookie操作类(CookieManger.cs) FTP操作类(FTPHelper.cs) HTML操作类(HttpHelper.cs) 网页抓取帮(HttpWebRequestHelper.cs) Net(NetworkUtil.cs) IE代理设置辅类(ProxyHelper.cs) ----------Winform-------------- 跨线程的控件安全访问方式(CallCtrlWithThreadSafety.cs) CheckBoxList(CheckBoxListUtil.cs) 窗口管理类(ChildWinManagement.cs) ChildWinManagement 由马丁·米勒http://msdn.microsoft.com/en-us/library/ms996492.aspx提供一个简单的方法打印工作的一个RichTextBox一个帮手(ExRichTextBoxPrintHelper.cs) 显示,隐藏或关闭动画形式。(FormAnimator.cs) 对窗体进行冻结、解冻操作类(FreezeWindowUtil.cs) 窗体全屏操作类(FullScreenHelper.cs) GDI操作类(GDI.cs) 提供静态方法来读取这两个文件夹和文件的系统图标。(IconReaderHelper.cs) 图片对象比较、缩放、缩略图、水印、压缩、转换、编码等操作类(ImageHelper.cs) 输入法帮,全角 转换为半角(ImeHelper.cs) Winform提示框 的摘要说明。(MessageUtil.cs) 包含互操作方法调用的应用程序中使用。(NativeMethods.cs) 托盘图标辅类(NotifyIconHelper.cs) 打印机类(POSPrinter.cs) 图片、光标、图标、位图等资源操作类(ResourceHelper.cs) RTF字符格式辅类(RTFUtility.cs) 串口开发类(SerialPortUtil.cs) 设置文本属性提供一个ToolStripStatusLabel(SafeToolStripLabel.cs) 只运行一个实例及系统自动启动辅类(StartupHelper.cs) Web页面预览效果图片抓取辅类(WebPageCapture.cs) 供Asp.Net直接调用的包装类(WebPreview.cs) 计算机重启、关电源、注销、关闭显示器辅类(WindowsExitHelper.cs) 简单写了点,还有很多,希望能对大家有帮 ================================================================================================ 本资料共包含以下附件: WHC.OrderWater.Commons.rar 公共类文档.docx
----------Database-------------- 1.DataTable帮类(DataTableHelper.cs) 2.Access数据库文件操作类(JetAccessUtil.cs) 5.查询条件组合辅类(SearchCondition.cs) 6.查询信息实体类(SearchInfo.cs) 8.Sql命令操作函数(可用于安装程序的时候数据库脚本执行)(SqlScriptHelper.cs) ----------Device-------------- 声音播放辅类(AudioHelper.cs) 摄像头操作类,包括开启、关闭、抓图、设置等功能(Camera.cs) 提供用于操作【剪切板】的方法(ClipboardHelper.cs) 获取电脑信息(Computer.cs) 提供用户硬件唯一信息的辅类(FingerprintHelper.cs) 读取指定盘符的硬盘序列号(HardwareInfoHelper.cs) 提供访问键盘当前状态的属性(KeyboardHelper.cs) 全局键盘钩子。这可以用来在全球范围内捕捉键盘输入。(KeyboardHook.cs) 模拟鼠标点击(MouseHelper.cs) 全局鼠标钩子。这可以用来在全球范围内捕获鼠标输入。(MouseHook.cs) MP3文件播放操作类(MP3Helper.cs) 关联文件(ExtensionAttachUtil.cs) 注册文件关联的辅类(FileAssociationsHelper.cs) 打开、保存文件对话框操作类(FileDialogHelper.cs) 常用的文件操作类FileUtil(FileUtil.cs) INI文件操作类(INIFileUtil.cs) 独立存储操作类(IsolatedStorageHelper.cs) 序列号操作类(Serializer.cs) 获取一个对象,它提供用于访问经常引用的目录的属性。(SpecialDirectories.cs) 简单的Word操作对象(WordCombineUtil.cs) 这个类提供了一些实用的方法来转换XML和对象。(XmlConvertor.cs) XML操作类(XmlHelper.cs) ----------Format-------------- 参数验证的通用验证程序。(ArgumentValidation.cs) 这个类提供了实用方法的字节数组和图像之间的转换。(ByteImageConvertor.cs) byte字节数组操作类(BytesTools.cs) 处理数据类型转换,数制转换、编码转换相关的类(ConvertHelper.cs) CRC校验辅类(CRCUtils.cs) 枚举操作公共类(EnumHelper.cs) 身份证操作类(IDCardHelper.cs) 检测字符编码的类(IdentifyEncoding.cs) RGB颜色操作类(MyColors.cs) 日期操作类(MyDateTime.cs) 转换人民币大小金额辅类(RMBUtil.cs) 常用的字符串常量(StringConstants.cs) 简要说明TextHelper。(StringUtil.cs) 获取中文字首字拼写,随机发生器,按指定概率随机执行操作(Util.cs) 各种输入格式验证辅类(ValidateUtil.cs) ----------Network-------------- Cookie操作类(CookieManger.cs) FTP操作类(FTPHelper.cs) HTML操作类(HttpHelper.cs) 网页抓取帮(HttpWebRequestHelper.cs) Net(NetworkUtil.cs) IE代理设置辅类(ProxyHelper.cs) ----------Winform-------------- 跨线程的控件安全访问方式(CallCtrlWithThreadSafety.cs) CheckBoxList(CheckBoxListUtil.cs) 窗口管理类(ChildWinManagement.cs) ChildWinManagement 由马丁·米勒http://msdn.microsoft.com/en-us/library/ms996492.aspx提供一个简单的方法打印工作的一个RichTextBox一个帮手(ExRichTextBoxPrintHelper.cs) 显示,隐藏或关闭动画形式。(FormAnimator.cs) 对窗体
WHC.OrderWater.Commons 伍华聪 公共类源码 类库文档 本资料共包含以下附件: WHC.OrderWater.Commons.rar 公共类文档.docx ----------------------------------------------------------- ----------Database-------------- 1.DataTable帮类(DataTableHelper.cs) 2.Access数据库文件操作类(JetAccessUtil.cs) 5.查询条件组合辅类(SearchCondition.cs) 6.查询信息实体类(Search Info.cs) 8.Sql命令操作函数(可用于安装程序的时候数据库脚本执行)(SqlScriptHelper.cs) ----------Device-------------- 声音播放辅类(AudioHelper.cs) 摄像头操作类,包括开启、关闭、抓图、设置等功能(Camera.cs) 提供用于操作【剪切板】的方法(ClipboardHelper.cs) 获取电脑信息(Computer.cs) 提供用户硬件唯一信息的辅类(FingerprintHelper.cs) 读取指定盘符的硬盘序列号(HardwareInfoHelper.cs) 提供访问键盘当前状态的属性(KeyboardHelper.cs) 全局键盘钩子。这可以用来在全球范围内捕捉键盘输入。(KeyboardHook.cs) 模拟鼠标点击(MouseHelper.cs) 全局鼠标钩子。这可以用来在全球范围内捕获鼠标输入。(MouseHook.cs) MP3文件播放操作类(MP3Helper.cs) 关联文件(ExtensionAttachUtil.cs) 注册文件关联的辅类(FileAssociationsHelper.cs) 打开、保存文件对话框操作类(FileDialogHelper.cs) 常用的文件操作类FileUtil(FileUtil.cs) INI文件操作类(INIFileUtil.cs) 独立存储操作类(IsolatedStorageHelper.cs) 序列号操作类(Serializer.cs) 获取一个对象,它提供用于访问经常引用的目录的属性。(SpecialDirectories.cs) 简单的Word操作对象(WordCombineUtil.cs) 这个类提供了一些实用的方法来转换XML和对象。(XmlConvertor.cs) XML操作类(XmlHelper.cs) ----------Format-------------- 参数验证的通用验证程序。(ArgumentValidation.cs) 这个类提供了实用方法的字节数组和图像之间的转换。(ByteImageConvertor.cs) byte字节数组操作类(BytesTools.cs) 处理数据类型转换,数制转换、编码转换相关的类(ConvertHelper.cs) CRC校验辅类(CRCUtils.cs) 枚举操作公共类(EnumHelper.cs) 身份证操作类(IDCardHelper.cs) 检测字符编码的类(IdentifyEncoding.cs) RGB颜色操作类(MyColors.cs) 日期操作类(MyDateTime.cs) 转换人民币大小金额辅类(RMBUtil.cs) 常用的字符串常量(StringConstants.cs) 简要说明TextHelper。(StringUtil.cs) 获取中文字首字拼写,随机发生器,按指定概率随机执行操作(Util.cs) 各种输入格式验证辅类(ValidateUtil.cs) ----------Network-------------- Cookie操作类(CookieManger.cs) FTP操作类(FTPHelper.cs) HTML操作类(HttpHelper.cs) 网页抓取帮(HttpWebRequestHelper.cs) Net(NetworkUtil.cs) IE代理设置辅类(ProxyHelper.cs) ----------Winform-------------- 跨线程的控件安全访问方式(CallCtrlWithThreadSafety.cs) CheckBoxList(CheckBoxListUtil.cs) 窗口管理类(ChildWinManagement.cs) ChildWinManagement
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值