【ASP.NET 基础】ASP.NET 文件操作

以txt文件为例学习了ASP.NET文件的基本操作,下面是代码,应该很清楚了:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//引入System.IO
using System.IO;
namespace FileTry
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //文本文件并不是指扩展名为 .txt 的文件,只要是以 ASCII 存储的文件均可,比如:.aspx、.htm、.css、.ini 等等。
            RenameEx();
        }

        /// <summary>
        /// 读示例
        /// </summary>
        private void ReadEx()
        {
            //源文件,必须存在哦
            string path1 = Server.MapPath("goo1.txt");//返回与Web服务器上的指定虚拟路径相对应的物理文件路径
            if (File.Exists(path1))//确定指定文件是否存在,存在返回true,不存在返回false
            {
            //File.OpenText 目标不存在时,异常。
            using (StreamReader reader = //实现一个System.IO.StreamReader,使其以一种特定的编码从字节流中读取字符
                File.OpenText(path1)) //打开现有UTF-8编码文件以进行读取
            {
                string str = reader.ReadLine();//从当前流中读取一行字符并将数据作为字符串返回
                reader.Close();//关闭System.IO.StreamReader对象和基础流,并释放与读取器关联的所有系统资源
            }
            }
        }

        /// <summary>
        /// 写示例
        /// </summary>
        private void WriteEx() {
            //源文件,必须存在哦
            string path1 = Server.MapPath("goo1.txt");
            if (File.Exists(path1))
            {
            //File.CreateText 目标存在时,覆盖。相当于删除了原文本的内容
            using (StreamWriter writer =  File.CreateText(path1)) //创建或打开一个文件用于写入UTF-8编码的文本 
            {
                writer.WriteLine("写一行内容"); //将后跟行结束符的字符串写入文本流
                writer.Close();
            }
            }
        }

        /// <summary>
        /// 追加示例
        /// </summary>
        private void ElseEx()
        {
            //源文件,必须存在哦
            string path1 = Server.MapPath("goo1.txt");
            if (File.Exists(path1))
            {
                using (StreamWriter writer =
                    File.AppendText(path1))//创建一个System.IO.StreamReader,它将UTF-8编码文本追到现有文件,不会删除原文本内容
                {
                    writer.WriteLine("追加一行内容");
                    writer.Close();
                }
            }
        }

        /// <summary>
        /// 删示例(不限定文件类型)
        /// </summary>
        private void DeleteEx()
        {
            //源文件,必须存在哦
            string path1 = Server.MapPath("goo1.txt");
            if(File.Exists(path1))
            {
            //File.Delete 目标不存在时,跳过。
             File.Delete(path1);
            }
        }

        /// <summary>
        /// 复制文件
        /// </summary>
        private void CopyEx()
        {
            //源文件,必须存在哦
            string path1 = Server.MapPath("goo.txt");
            //目标文件
            string path2 = Server.MapPath("goo1.txt");
            //File.Copy(Server.MapPath("goo.txt"), Server.MapPath("goo1.txt"));
            //将现有文件复制到新文件。允许覆盖同名文件。
            /**第一个参数表示源文件路径,
             * 第二个参数表示目标文件路径,
             * 第三个参数表示目标文件存在时,是否覆盖。默认为false。
             * 如果该值为 false,当目标文件存在时,会产生异常,而不是跳过复制。--异常为该文件已存在。
           */
            if (File.Exists(path1) && !File.Exists(path2))
            {
                File.Copy(path1,path2, true);
            }
            

        }

        /// <summary>
        /// 移动文件
        /// </summary>
        private void MoveEx()
        {
            //源文件,必须存在哦
            string path1 = Server.MapPath("goo.txt");
            //目标文件
            string path2 = Server.MapPath("document\\goo.txt");
            //将指定文件移到新位置,并提供指定新文件名的选项
            //如果目标文件存在,则会产生异常,而不是跳过移动。
            //第一个参数表示源文件路径, 第二个参数表示目标文件路径,路径上的文件夹要确实存在,不然会报错
            if (File.Exists(path1)&&!File.Exists(path2))
            {
                File.Move(path1,path2);
            }
        }

        /// <summary>
        /// 重命名文件
        /// </summary>
        private void RenameEx()
        {
            //源文件,必须存在哦
            string path1 = Server.MapPath(@"document\goo.txt");
            //目标文件
            string path2 = Server.MapPath(@"document\goo1.txt");
            //重命名文件的方法和移动文件的方法完全相同,只要目标文件和源文件位于同一文件夹下且文件名不相同。
            if (File.Exists(path1)&&!File.Exists(path2))
            {
                File.Move(path1,path2);
            }
        }
    }
}

 

 

posted @ 2013-04-27 11:03  叶超Luka  阅读(691)  评论(0编辑  收藏  举报