正文

[asp.net]框架类笔记2006-09-06 15:34:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/bakers/18338.html

分享到:

.NET框架类   [关键字]System.IO,  System.Drawing ,  System.Web.Security., System.Web.Mail, System.Net 一.System.IO命名空间    该空间提供很多对文件,以及数据操作的类. 类 说明 Directory 提供的方法为静态方法,用于创建、移动和读取目录。 DirectoryInfo 提供的方法必须创建该类的实例,才能使用。用于创建、移动和读取目录。 File 提供创建、复制、删除、移动和打开文件的静态方法。 FileInfo 提供创建、复制、删除、移动和打开文件的实例方法。 FileStream 以文件为主的 Stream,既支持同步读写操作,也支持异步读写操作。 StreamReader 实现一个TextReader,以一种特定的编码从字节流中读取字符。 StreamWriter 实现一个 TextWriter,以一种特定的编码向流中写入字符。 TextReader 可读取连续字符的对象。 TextWriter 可写入连续字符的对象。该类为抽象类。 实例1.创建一个有文本信息的文件. <% @ Page Language="C#" %> <% @ Import Namespace="System.IO" %> <Script Runat="Server"> public void Page_Load(Object src,EventArgs e){        //建立StreamWrite        StreamWriter rw = File.CreateText(Server.MapPath(".")+"\\myText.txt");        rw.WriteLine("热爱祖国");    //使用WriteLine写入内容        rw.WriteLine("热爱人民");        rw.Flush();     //将缓冲区的内容写入文件        rw.Close();     //关闭rw对象        //打开文本文件        StreamReader sr = File.OpenText(Server.MapPath(".")+"\\myText.txt");        StringBuilder output = new StringBuilder();        string rl;        while((rl=sr.ReadLine())!=null)     {               output.Append(rl+"<br>");        }        lblFile.Text = output.ToString();        sr.Close(); } </script> <form runat="server"> <b>创建文本文件成功,写入成功,文件的内容为:</b> <p><asp:Label id="lblFile" runat="server" /> </p></form>   实例2.追加文本. <% @ Page Language="C#" %> <% @ Import Namespace="System.IO" %> <Script Runat="Server"> public void Page_Load(Object src,EventArgs e){   StreamWriter sw = File.AppendText(Server.MapPath(".")+"\\myText.txt");        sw.WriteLine("热爱祖国");    //使用WriteLine写入内容        sw.WriteLine("热爱人民");        sw.Flush();    //将缓冲区的内容写入文件        sw.Close();    //关闭sw对象          //打开文本文件        StreamReader sr = File.OpenText(Server.MapPath(".")+"\\myText.txt");        StringBuilder output = new StringBuilder();        string rl;        while((rl=sr.ReadLine())!=null)     {               output.Append(rl+"<br>");        }        lblFile.Text = output.ToString();        sr.Close(); } </script> <form runat="server"> <b>读取文本文件成功</b> <p><asp:Label id="lblFile" runat="server" /> </p></form> 实例3.拷贝文件  “File.Copy(OrignFile,NewFile,true)”,参数true的意思是如果目标文件存在就覆盖。 <% @ Page Language="C#" %> <% @ Import Namespace="System.IO" %> <Script Runat="Server"> public void Page_Load(Object src,EventArgs e){        string OrignFile,NewFile;        OrignFile = Server.MapPath(".")+"\\myText.txt";        NewFile = Server.MapPath(".")+"\\myTextCopy.txt";        //拷贝文件        try   {               File.Copy(OrignFile,NewFile,true);               if(File.Exists(NewFile))         {               FileInfo fi = new FileInfo(NewFile);               DateTime Ctime = fi.CreationTime;               lblEToFile.Text = NewFile + "<br>创建时间:" + Ctime.ToString() + "<br>";               }               else         {               lblEToFile.Text = NewFile + "不存在<br>";}        }        catch(Exception ee)       {               lblError.Text = "不能拷贝文件";        } } </script> <b>拷贝文件操作完毕</b><br> <form runat="server"><p> </p><asp:Label id="lblEToFile" runat="server" /><p> </p><asp:Label id="lblError" runat="server" /> </form> 实例4.删除文件 <% @ Page Language="C#" %> <% @ Import Namespace="System.IO" %> <Script Runat="Server"> public void Page_Load(Object src,EventArgs e){        //首先判断文件是否存在        string delFile = Server.MapPath(".")+"\\myTextCopy.txt";               try{               //删除文件                      File.Delete(delFile);                      Label lblOk = new Label();                      lblOk.Text = "删除文件"+delFile+"成功";                      plShow.Controls.Add(lblOk);               }               catch(Exception ee){                  //捕捉异常                      Label lblFileExists = new Label();                      lblFileExists.Text = "不能删除文件"+delFile+"<br>";                      plShow.Controls.Add(lblFileExists);               } } </script> <form runat="server"> <b>删除文件操作的结果为</b><p> <asp:Panel id="plShow" runat="server" Font-Name="Arial"/> </form>     实例5.移动文件 <% @ Page Language="C#" %> <% @ Import Namespace="System.IO" %> <Script Runat="Server"> public void Page_Load(Object src,EventArgs e){        string OrignFile,NewFile;        OrignFile = Server.MapPath(".")+"\\myText.txt";        NewFile = Server.MapPath(".")+"\\myTextCopy.txt";        try   {     //移动文件               File.Move(OrignFile,NewFile);         lblError.Text = "移动文件成功";        }        catch(Exception ee)       {               lblError.Text = "不能移动文件";        } } </script> <body><b>移动文件操作结果为:</b><br> <form runat="server"> <asp:Label id="lblError" runat="server" /> </form>    对文件夹操作利用system.IO.Directory和System.IO.DirectoryInfo类.Directory类提供了用于创建,删除,移动和浏览目录和子目录的各种方法,这些方法都是静态的.   类是Directory类,DirectoryInfo也提供了用于创建,删除,移动和浏览子目录的各种方法,提供的方法都是非静态方法,不能按照类名DirectoryInfo进行访问,而必学通过DirectoryInfo的实例进行访问.创建的文件夹操作包括创建和删除. 实例6. 操作文件夹  <% @ Page Language="C#" %> <% @ Import Namespace="System.IO" %> <Script Runat="Server"> public void Page_Load(Object src,EventArgs e){                     // 创建目录c:\first                  DirectoryInfo d=Directory.CreateDirectory("c:\\first");                  // d1指向c:\first\first1                  DirectoryInfo d1=d.CreateSubdirectory("first1");                  // d2指向c:\first\first1\first1_1                  DirectoryInfo d2=d1.CreateSubdirectory("first1_1");                  // 将当前目录设为c:\first                  Directory.SetCurrentDirectory("c:\\first");                  // 创建目录c:\first\first2                  Directory.CreateDirectory("first2");                  // 创建目录c:\first\first2\first2_1                  Directory.CreateDirectory("first2\\first2_1"); } </script> 二.使用System.Drawing命名空间   System.Drawing命名空间提供了对GDI+基本图形功能的访问. 在System.Drawing.Drawing2D, System.Drawing.Imaging 以及System.Drawing.Text命名空间中提供了相关的绘图功能. GDI+介绍 GDI(Graphics Device Interface., 图形设备接口)提供了对各种图形绘制的支持. GDI+是对GDI的改进,也是.NET框架结构的重要组成部分. 和GDI一样,提供对二维图形图像和文字排版处理的支持. 使用System.Drawing 画图 “<%@ Import namespace="System.Drawing"%>” 需要动态输出JPEG图像,在页面声明时需加. “<%@ Page Language="C#" ContentType="image/jpeg" %>” 实例1.使用System.Drawing画图 <%@ Page Language="C#" ContentType="image/jpeg" %> <%@ Import namespace="System.Drawing"%> <script runat="server">   void Page_Load(object sender,EventArgs e)   {   Bitmap image=new Bitmap(350,200);   Graphics g=Graphics.FromImage(image);   g.Clear(Color.White);   Rectangle outline=new Rectangle(10,5,300,100);   g.DrawEllipse(new Pen(Color.Black,8.0f),outline);   g.FillPie(new SolidBrush(Color.Red),outline,-20f,120f);   g.FillPie(new SolidBrush(Color.Yellow),outline,100f,120f);   g.FillPie(new SolidBrush(Color.Blue),outline,220f,100f);   g.FillPie(new SolidBrush(Color.Green),outline,320f,40f);   image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);   }   </script> 三. System.web.Security 实例1. 字符串加密 <% @ Page Language="C#" %> <%@ Import Namespace="System.Web.Security" %> <script language="C#" runat="server"> void Page_Load(object sender,EventArgs e)   {        string s1="We are happy!";        Response.Write("字符串We are happy!经过sha1加密后为:<br>");        string s2 = System.Web.Security.FormsAuthentication        .HashPasswordForStoringInConfigFile( s1 , "sha1");        Response.Write(s2);        Response.Write("<br>");        string s3="shizhiguo";        Response.Write("字符串We are happy!经过md5加密后为:<br>");        string s4 = System.Web.Security.FormsAuthentication        .HashPasswordForStoringInConfigFile( s1 , "md5");        Response.Write(s4);   } </script>   四.System.Web.Mail 实例1. 发送需要服务断验证的邮件 <%@page language="C#" %> <%@Import Namespace="System.Web.Mail" %> <Script language="C#" runat="server"> private void Page_Load(object sender, System.EventArgs e){  MailMessage mail = new MailMessage();  mail.To = "shizhiguo@tom.com";//收件人地址  mail.From = "shizhiguo@tom.com";//发件人地址  mail.Subject = "邮件标题";  mail.Body = "邮件内容";   //设置需要服务器端验证  mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");   //验证的用户名 mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername","用户名");   //验证的密码  mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "密码");   //发送邮件的SMTP服务器地址  SmtpMail.SmtpServer = "SMTP服务器地址";  SmtpMail.Send( mail );  Response.Write("发送成功!"); } </Script> 注: 使用组件本质上是利用Socket直接和SMTP服务器进行交互,可以在ASP.NET中使用. 比如Jmail组件等. 在.NET中,目前最常用的电子邮件组件是C#写的openSmtp组件. 实例2. 使用组件发送电子邮件 <%@page language="C#" %> <%@ Import Namespace="OpenSmtp.Mail" %> <script runat="server"> private void Page_Load(object sender, System.EventArgs e){ try   {        MailMessage msg = new MailMessage("发送人地址", "接收人地址");     msg.Charset = "GB2312";        msg.Subject = "smtp组件发送邮件";        msg.Body = "邮件内容";        Smtp smtp = new Smtp("SMTP服务器地址", //SMTP地址                                           "用户名",     //验证的用户名                                           "密码");    //密码        smtp.SendMail(msg);         Response.Write("邮件发送成功!");        }        catch(Exception ex)       {               System.Console.WriteLine("Error occured: " + ex.Message + "r\n" + ex);        }   } </script>  五. 使用System.Net命名空间.  实例1. 利用机器名查找站点的IP地址 <%@Page Language="C#"%> <%@Import NameSpace="System.Net"%> <script runat=server>  protected void mybuttonClick(Object Src, EventArgs E){  IPHostEntry hostInfo = Dns.GetHostByName(txtDomain.Text);  showmsg.Text=hostInfo.AddressList[0].ToString(); } </script> <asp:Label runat=server id=showmsg/>  <form runat=server> 请输入域名:<asp:TextBox runat=server id=txtDomain/>  <asp:Button runat=server id=mybutton        Text="确定" onClick=mybuttonClick/> </form> 实例2. 利用IP地址查找该站点的机器名. <%@Page Language="C#"%> <%@Import NameSpace="System.Net"%> <script language="C#" runat=server>  protected void mybuttonClick(Object Src, EventArgs E){  IPHostEntry hostInfo = Dns.GetHostByAddress(txtIP.Text);  showmsg.Text=hostInfo.HostName; } </script> <asp:Label runat=server id=showmsg/>  <form runat=server>  要查找的IP地址:<asp:TextBox runat=server id=txtIP/>  <asp:Button runat=server id=mybutton Text="查找" onClick=mybuttonClick />   总结. 整体了解.NET框架类的组成和一些基本类的使用. 初步掌握和使用GDI+图形接口类绘制简单图形. 掌握System.Web.Security进行字符加密. 掌握System.web.Mail类进行电子邮件发送. 熟悉System.Net命名空间对网络进行操作.

阅读(5463) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册