前面说了如何编写自定义类库,以及如何调用,以及在程序中的使用。
现在给出一个自定义的类库的案例,并对其进行分析和学习。
一些频繁使用的功能,如数据库访问,字符替换,邮件发送,文件读取等这些代码较多,重新写比较繁琐。如果利用代码插入方式也可以方便的使这些代码重复使用,但是下面介绍一种更加有效的方法-编写类库。哈哈,我来了。。
该程序文件名webtools.cs,编译后文件是WebTools.dll,位于站点的bin目录下。
该案例用到的主要技术是ASP.NET中组件的应用技术,包括组件的编写、编译、调用。
代码如下:
namespace WebTools
{
using System;
using System.IO;
using System.Net;
using System.Data;
using System.Data.SqlClient;
using System.Web.Mail;
public class Tools
{
public SqlConnection connStr=new SqlConnection("server=172.0.0.1;database=misgd;User ID=test;Password=888888;");
//站点参数
public string ChangeStr(string str,int num)
{
string add_str="";
if(str.Length>num)add_str="..";
string addstr=str+" ";//加点空格
return addstr.Substring(0,num).Trim().Replace(" "," ").Replace("/n","<br/>")+add_str;
}//文本字符替换
public string CheckStr(string strname,string str,int minnum,int maxnum)
{
string estr="";
byte[] bstr=System.Text.Encoding.Default.GetBytes(str);
if((bstr.Length>maxnum)||(bstr.Length<minnum)) estr+="字符数目不符,应该在"+minnum+"-"+maxnum+"之间!";
Char[] spestr={'?','=','&','+','"','/'',';','//','<','>','|'};
if(str.IndexOfAny(spestr)>-1)
estr+="含有非法字符:问号 等号 加号 分号 单引号 双引号 // () <>|&等!";
if(estr!="")
{
return "“"+strname+"”: "+estr+"<br/>";
}
else
{
return estr;
}
}//客户输入检查
public string PswdFormat(string str,string format)
{
string returnstr="";
if(format=="SHA1")returnstr=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str,"SHA1");
if(format=="MD5")returnstr=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str,"MD5");
return returnstr;
}//ASP.NET的加密方式
public SqlDataReader SqlReader(string sql,SqlConnection connstr)
{
SqlDataReader sqldr=null;
SqlCommand cmd = new SqlCommand(sql,connstr);
if(cmd.Connection.State.ToString()=="Closed")cmd.Connection.Open();
try
{
sqldr=cmd.ExecuteReader();
}
catch(Exception e)
{
if (e!=null)sqldr=null;
}
return sqldr;
}//数据库读取连接
public string SqlCmd(string sql,SqlConnection connstr)
{
string errorstr =null;
SqlCommand sqlcmd=new SqlCommand(sql,connstr);
if(sqlcmd.Connection.State.ToString()=="Open")sqlcmd.Connection.Close();
sqlcmd.Connection.Open();
try
{
sqlcmd.ExecuteNonQuery();
}
catch(Exception e)
{
if(e!=null)errorstr=e.ToString();
}
sqlcmd.Connection.Close();
return errorstr;
}//数据库操作连接
public void Wfile(string filename,string filecont)
{
FileStream fs=new FileStream(filename,FileMode.OpenOrCreate,FileAccess.Write);
StreamWriter wfile =new StreamWriter(fs);
wfile.BaseStream.Seek(0,SeekOrigin.End);
wfile.WriteLine(filecont);
wfile.Flush();
wfile.Close();
}//写入记录文件
public string Rfile(string filename,int startline,int endline)
{
string ReadFile="";
FileStream fs=new FileStream(filename,FileMode.OpenOrCreate,FileAccess.Read);
StreamReader rfile=new StreamReader(fs);
rfile.BaseStream.Seek(0,SeekOrigin.Begin);
for(int i=0;i<=endline;i++)
{
if((i>=startline)&&(rfile.Peek()>-1))
{
ReadFile+=rfile.ReadLine()+"/n";
}
else
{
if(rfile.Peek()>-1){rfile.ReadLine();}
else{break;}
}
}
return ReadFile;
}//读取文件
public void SendMail(string fromad,string sendad,string msubject,string mbody)
{
MailMessage mm=new MailMessage();
mm.From=fromad;
mm.To=sendad;
//mm.Bcc="";//密送地址
//mm.Cc="";//抄送地址
mm.Subject=msubject;//邮件主题
mm.Body=mbody;//邮件内容
mm.BodyFormat=MailFormat.Html;//正文格式
mm.Priority=MailPriority.High;//优先级
//mm.Attachments.Add(new MailAttachment("c://lixi.txt"));//附件
SmtpMail.Send(mm);
}//邮件发送
public string ShowMsgBox(string Msg,string Action)
{
string AddAction="";
if(Action!=null)
{
switch(Action)
{
case "back":
AddAction="window.history.back(1);";
break;
case "close":
AddAction="top.close();";
break;
default:
AddAction="location.href='"+Action+"';";
break;
}
}
return "<script language='javascript'>alert('"+Msg+"');"+AddAction+"<"+"/script>";
}//提示信息
public string PutToHtml(string HttpFile,string SaveFile)
{
string ReturnMsg="";
if((HttpFile!="")&&(SaveFile!=""))
{
WebClient Wclient= new WebClient();
try
{
Wclient.DownloadFile(HttpFile,SaveFile);
ReturnMsg="1";
}
catch(WebException ex)
{
if(ex!=null)ReturnMsg="0";
}
}
else
{
ReturnMsg="-1";
}
if(ReturnMsg=="")ReturnMsg="-1";
return ReturnMsg;
}//HTML下载
}
}
//connStr:数据库连接参数
//ChangStr(string str,int num):字符转换函数,显示空格,回车等。
//CheckStr(string strname,string str,int minnum,int maxnum):字符检查函数。
//SqlReader(string sql,SqlConnection connstr):数据库读取函数。
//PswdFormat(string str,string format):密码加密函数。
//SqlCmd(string sql,SqlConnection connstr):数据库命令执行函数。
//Wfile(string filename,string filecont):写文本文件函数。
//Rfile(string filename,int startline,int endline):读文本文件函数
//SendMail(string fromad,string sendad,string msubject,string mbody);邮件发送程序。
//ShowMsgBox(string Msg,string Action):信息提示函数。
//PutToHtml(string HttpFile,string SaveFile):可以将制定地址的HTML代码保存为HTML页面。
//反编译,微软的。。。。
ildasm /adv assembly.dll
添加/adv参数后,菜单中会多出几项,适用于.NET Framework 1.0 and 1.1。2.0下好像所有的菜单都直接显示出来了。
帮助文件和ildasm /?中都没有给出adv参数的描述。