




已阅读5页,还剩19页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第5章 NET框架类 5 内容提要 本章介绍常用的 NET框架类 使用System IO命名空间进行文件系统处理使用System Drawing命名空间绘制图形 利用System Web Security实现加密等等 System IO命名空间操作文件系统 System IO命名空间提供了许多用于数据文件和数据流进行读写操作的类 包括的类如表5 1所示 对文件进行操作 案例名称 创建一个有内容的文本文件程序名称 5 01 aspxpublicvoidPage Load Objectsrc EventArgse 建立StreamWriteStreamWriterrw File CreateText Server MapPath myText txt rw WriteLine 热爱祖国 使用WriteLine写入内容rw WriteLine 热爱人民 rw Flush 将缓冲区的内容写入文件rw Close 关闭rw对象 打开文本文件StreamReadersr File OpenText Server MapPath myText txt StringBuilderoutput newStringBuilder stringrl while rl sr ReadLine null output Append rl lblFile Text output ToString sr Close 创建文本文件成功 写入成功 文件的内容为 追加文本文件 案例名称 追加文本文件程序名称 5 02 aspxpublicvoidPage Load Objectsrc EventArgse StreamWritersw File AppendText Server MapPath myText txt sw WriteLine 热爱祖国 使用WriteLine写入内容sw WriteLine 热爱人民 sw Flush 将缓冲区的内容写入文件sw Close 关闭sw对象 打开文本文件StreamReadersr File OpenText Server MapPath myText txt StringBuilderoutput newStringBuilder stringrl while rl sr ReadLine null output Append rl lblFile Text output ToString sr Close 读取文本文件成功 文本文件的拷贝 拷贝文件利用File对象的Copy方法该方法使用格式为 File Copy OrignFile NewFile true 参数true的意思是如果目标文件存在就覆盖 文本文件的拷贝 案例名称 文本文件的拷贝程序名称 5 03 aspxpublicvoidPage Load Objectsrc EventArgse stringOrignFile NewFile OrignFile Server MapPath myText txt NewFile Server MapPath myTextCopy txt 拷贝文件try File Copy OrignFile NewFile true if File Exists NewFile FileInfofi newFileInfo NewFile DateTimeCtime fi CreationTime lblEToFile Text NewFile 创建时间 Ctime ToString else lblEToFile Text NewFile 不存在 catch Exceptionee lblError Text 不能拷贝文件 拷贝文件操作完毕 删除文件 案例名称 删除文件程序名称 5 04 aspxpublicvoidPage Load Objectsrc EventArgse 首先判断文件是否存在stringdelFile Server MapPath myTextCopy txt try 删除文件File Delete delFile LabellblOk newLabel lblOk Text 删除文件 delFile 成功 plShow Controls Add lblOk catch Exceptionee 捕捉异常LabellblFileExists newLabel lblFileExists Text 不能删除文件 delFile plShow Controls Add lblFileExists 删除文件操作的结果为 移动文件 案例名称 移动文件程序名称 5 05 aspxpublicvoidPage Load Objectsrc EventArgse stringOrignFile NewFile OrignFile Server MapPath myText txt NewFile Server MapPath myTextCopy txt try 移动文件File Move OrignFile NewFile lblError Text 移动文件成功 catch Exceptionee lblError Text 不能移动文件 移动文件操作结果为 对文件夹进行操作 对文件夹操作利用System IO Directory和System IO DirectoryInfo类 Directory类提供了用于创建 删除 移动和浏览目录和子目录的各种方法 这些方法都是静态的 类似Directory类 DirectoryInfo也提供了用于创建 删除 移动和浏览目录及子目录的各种方法 提供的方法都是非静态方法 不能按照类名DirectoryInfo进行访问 而必须通过DirectoryInfo的实例进行访问 创建的文件夹操作包括创建和删除 如程序5 06 aspx所示 操作文件夹 案例名称 操作文件夹程序名称 5 06 aspxpublicvoidPage Load Objectsrc EventArgse 创建目录c firstDirectoryInfod Directory CreateDirectory c first d1指向c first first1DirectoryInfod1 d CreateSubdirectory first1 d2指向c first first1 first1 1DirectoryInfod2 d1 CreateSubdirectory first1 1 将当前目录设为c firstDirectory SetCurrentDirectory c first 创建目录c first first2Directory CreateDirectory first2 创建目录c first first2 first2 1Directory CreateDirectory first2 first2 1 使用System Drawing命名空间 System Drawing命名空间提供了对GDI 基本图形功能的访问 在System Drawing Drawing2D System Drawing Imaging以及System Drawing Text命名空间中提供了相关的绘图功能 GDI 介绍 GDI GraphicsDeviceInterface 图形设备接口 提供了对各种图形绘制的支持 GDI 是对GDI的改进 也是 NET框架结构的重要组成部分 和GDI一样 提供对二维图形图像和文字排版处理的支持 使用System Drawing画图 在页面中引入命名空间System Drawing的语法是 要动态的输出jpeg图像 在页面声明的时候 需要声明为 使用System Drawing画图 案例名称 使用System Drawing画图程序名称 5 08 aspxvoidPage Load objectsender EventArgse Bitmapimage newBitmap 350 200 Graphicsg Graphics FromImage image g Clear Color White Rectangleoutline newRectangle 10 5 300 100 g DrawEllipse newPen Color Black 8 0f outline g FillPie newSolidBrush Color Red outline 20f 120f g FillPie newSolidBrush Color Yellow outline 100f 120f g FillPie newSolidBrush Color Blue outline 220f 100f g FillPie newSolidBrush Color Green outline 320f 40f image Save Response OutputStream System Drawing Imaging ImageFormat Jpeg ASP NET中的字符加密 案例名称 ASP NET中字符串的加密程序名称 5 09 aspxvoidPage Load objectsender EventArgse strings1 Wearehappy Response Write 字符串Wearehappy 经过sha1加密后为 strings2 System Web Security FormsAuthentication HashPasswordForStoringInConfigFile s1 sha1 Response Write s2 Response Write strings3 shizhiguo Response Write 字符串Wearehappy 经过md5加密后为 strings4 System Web Security FormsAuthentication HashPasswordForStoringInConfigFile s3 md5 Response Write s4 发送需要服务器端验证的Email 案例名称 发送电子邮件程序名称 5 10 aspxprivatevoidPage Load objectsender System EventArgse MailMessagemail newMailMessage mail To shizhiguo 收件人地址mail From shizhiguo 发件人地址mail Subject 邮件标题 mail Body 邮件内容 设置需要服务器端验证mail Fields Add 使用 NET外部组件发送Email 使用组件本质上是利用Socket直接和SMTP服务器进行交互 可以在ASP中使用的电子邮件组件 也可以在ASP NET中使用 比如Jmail组件等 在 NET中 目前最常用的电子邮件组件是用C 写的openSmtp组件 使用组件发送电子邮件 案例名称 使用组件发送电子邮件程序名称 5 11 aspxprivatevoidPage Load objectsender System EventArgse try MailMessagemsg newMailMessage 发送人地址 接收人地址 msg Charset GB2312 msg Subject smtp组件发送邮件 msg Body 邮件内容 Smtpsmtp newSmtp SMTP服务器地址 SMTP地址 用户名 验证的用户名 密码 密码smtp SendMail msg Response Write 邮件发送成功 catch Exceptionex System Console WriteLine Erroroccured ex Message r n ex 使用System Net命名空间 案例名称 利用机器名查找该站点的IP地址程序名称 5 12 aspxprotectedvoidmybuttonClick ObjectSrc EventArgsE IPHostEntryhostInfo Dns GetHostByName txtDomain Text showmsg Text hostInfo AddressList 0 ToString 请输入域名 利用IP地址查找该站点的机器名 案例名称 利用IP地址查找该站点的机器名程序名称 5 13 aspxprotectedvoidmybuttonClick ObjectSrc EventArgsE IPHostEntryhostInfo Dns GetHostByAddress txtIP Text showmsg Text hostInfo HostName 要查找的IP地址 小结 本章对框架的部分类做了介绍 着重介绍了使用Syst
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 药品耗材监测管理制度
- 药品销售差错管理制度
- 药店医保基本管理制度
- 药店数据安全管理制度
- 菌种保藏中心管理制度
- 设备人员日常管理制度
- 设备制作车间管理制度
- 设备实施安全管理制度
- 设备日常基础管理制度
- 设备租赁资产管理制度
- 变电站一键顺控改造技术规范(试行)
- 光储充一体化充电站设计方案
- 胸腔积液护理查房
- 《公路桥涵施工技术规范》JTGT3650-2020
- 湖南省张家界市永定区2023-2024学年三年级下学期期末考试数学试题
- 2024年湖北省中考历史真题
- 2024小学六年级人教版道德与法治升学毕业小升初试卷及答案(时政+上下册考点)04
- 期末考试试卷(试题)-2023-2024学年三年级下册数学北师大版
- 人教版2024年数学小升初模拟试卷(含答案解析)
- 架空送电线路导线及避雷线液压施工工艺规程
- 森林管护工考试考试题库(强化练习)
评论
0/150
提交评论