【测速】C#文件与文件夹操作(创建、移动、删除、复制)

更新时间:2018-10-20    来源:php常用代码    手机版     字体:

【www.bbyears.com--php常用代码】

//1.---------文件夹创建、移动、删除---------
//创建文件夹

 代码如下 Directory.CreateDirectory(Server.MapPath("a"));
Directory.CreateDirectory(Server.MapPath("b"));
Directory.CreateDirectory(Server.MapPath("c"));
//移动b到a
Directory.Move(Server.MapPath("b"), Server.MapPath("a\b"));
//删除c
Directory.Delete(Server.MapPath("c"));

//2.---------文件创建、复制、移动、删除---------

 代码如下 //创建文件
//使用File.Create创建再复制/移动/删除时会提示:文件正由另一进程使用,因此该进程无法访问该文件
//改用 FileStream 获取 File.Create 返回的 System.IO.FileStream 再进行关闭就无此问题
FileStream fs;
fs = File.Create(Server.MapPath("a.txt"));
fs.Close();
fs = File.Create(Server.MapPath("b.txt"));
fs.Close();
fs = File.Create(Server.MapPath("c.txt"));
fs.Close();
//复制文件
File.Copy(Server.MapPath("a.txt"), Server.MapPath("a\a.txt"));
//移动文件
File.Move(Server.MapPath("b.txt"), Server.MapPath("a\b.txt"));
File.Move(Server.MapPath("c.txt"), Server.MapPath("a\c.txt"));
//删除文件
File.Delete(Server.MapPath("a.txt"));


C#追加文件

 代码如下

 StreamWriter sw = File.AppendText(
                Server.MapPath(".") + "\myText.txt");
            sw.WriteLine("追逐理想");
            sw.WriteLine("kzlll");
            sw.WriteLine(".NET笔记");
            sw.Flush(); sw.Close();

//3.---------遍历文件夹中的文件和子文件夹并显示其属性---------

 代码如下 if(Directory.Exists(Server.MapPath("a")))
{
    //所有子文件夹
    foreach(string item in Directory.GetDirectories(Server.MapPath("a")))
    {
        Response.Write("文件夹:" + item + "
");
        DirectoryInfo directoryinfo = new DirectoryInfo(item);
        Response.Write("名称:" + directoryinfo.Name + "
");
        Response.Write("路径:" + directoryinfo.FullName + "
");
        Response.Write("创建时间:" + directoryinfo.CreationTime + "
");
        Response.Write("上次访问时间:" + directoryinfo.LastAccessTime + "
");
        Response.Write("上次修改时间:" + directoryinfo.LastWriteTime + "
");
        Response.Write("父文件夹:" + directoryinfo.Parent + "
");
        Response.Write("所在根目录:" + directoryinfo.Root + "
");
        Response.Write("
");
    }
    //所有子文件
    foreach (string item in Directory.GetFiles(Server.MapPath("a")))
    {
        Response.Write("文件:" + item + "
");
        FileInfo fileinfo = new FileInfo(item);
        Response.Write("名称:" + fileinfo.Name + "
");
        Response.Write("扩展名:" + fileinfo.Extension +"
");
        Response.Write("路径:" + fileinfo.FullName +"
");
        Response.Write("大小:" + fileinfo.Length +"
");
        Response.Write("创建时间:" + fileinfo.CreationTime +"
");
        Response.Write("上次访问时间:" + fileinfo.LastAccessTime +"
");
        Response.Write("上次修改时间:" + fileinfo.LastWriteTime +"
");
        Response.Write("所在文件夹:" + fileinfo.DirectoryName +"
");
        Response.Write("文件属性:" + fileinfo.Attributes +"
");
        Response.Write("
");
    }
}

//4.---------文件读写---------

 代码如下

if (File.Exists(Server.MapPath("a\a.txt")))
{
    StreamWriter streamwrite = new StreamWriter(Server.MapPath("a\a.txt"));
    streamwrite.WriteLine("ABCD数据库");
    streamwrite.WriteLine("http://www.111cn.net/");
    streamwrite.Write("2012-05-15");
    streamwrite.Close();

    StreamReader streamreader = new StreamReader(Server.MapPath("a\a.txt"));
    Response.Write(streamreader.ReadLine());
    Response.Write(streamreader.ReadToEnd());
    streamreader.Close();
}

//文件夹删除

 代码如下

〈%@PageLanguage=C#%〉 
        〈%@Importnamespace="System.IO"%〉 
        〈Scriptrunat=server〉 
        public void DeleteFolder(string dir) 
        ...{ 
            //如果存在这个文件夹删除之
            if(Directory.Exists(dir))
            ...{ 
                foreach(string d in Directory.GetFileSystemEntries(dir))
                ...{ 
                    if(File.Exists(d))
                        File.Delete(d);//直接删除其中的文件
                    else DeleteFolder(d);//递归删除子文件夹 
                } 
                Directory.Delete(dir);
                //删除已空文件夹
                Response.Write(dir+"文件夹删除成功"); 
            }
            else //如果文件夹不存在则提示
                Response.Write(dir+"该文件夹不存在");
        } 
   
    protected void Page_Load(Object  sender,EventArgse)
    ...{ 
        stringDir="D:\gbook\11"; 
        DeleteFolder(Dir);//调用函数删除文件夹
    }
        //======================================== 
        //实现一个静态方法将指定文件夹下面的所有
        //内容copy到目标文件夹下面 
        //如果目标文件夹为只读属性就会报错。 
        //April18April2005InSTU 
        //======================================== 
        public static void CopyDir(string srcPath,  string aimPath) 
        ...{
            try  ...{ 
                //检查目标目录是否以目录分割字符
                //结束如果不是则添加之
                if(aimPath[aimPath.Length-1] != 
                    Path.DirectorySeparatorChar)
                    aimPath+=Path.DirectorySeparatorChar; 
                //判断目标目录是否存在如果不存在则新建之 
                if(!Directory.Exists(aimPath))
                    Directory.CreateDirectory(aimPath); 
                //得到源目录的文件列表,该里面是包含
                //文件以及目录路径的一个数组 
                //如果你指向copy目标文件下面的文件 
                //而不包含目录请使用下面的方法 
                //string[]fileList=  Directory.GetFiles(srcPath); 
                string[]fileList=
                    Directory.GetFileSystemEntries(srcPath); 
                //遍历所有的文件和目录 
                foreach(string file in fileList) 
                ...{  
                    //先当作目录处理如果存在这个
                    //目录就递归Copy该目录下面的文件 
                    if(Directory.Exists(file)) 
                        CopyDir(
                            file,
                            aimPath+Path.GetFileName(file)); 
                    //否则直接Copy文件
                    else
                        File.Copy(
                            file,
                            aimPath+Path.GetFileName(file),
                            true);
                } 
            } 
            catch(Exception e)
            ...{
                MessageBox.Show(e.ToString()); 
            } 
        } 
   
    //======================================== 
    //实现一个静态方法将指定文件夹下面的所有内容Detele 
    //测试的时候要小心操作,删除之后无法恢复。
    //April18April2005InSTU 
    //======================================== 
    public static void DeleteDir(string aimPath) 
    ...{ 
        try
        ...{
            //检查目标目录是否以目录分割
            //字符结束如果不是则添加之
            if(aimPath[aimPath.Length-1] !=
                Path.DirectorySeparatorChar)
                aimPath+=Path.DirectorySeparatorChar;
            //得到源目录的文件列表,该里面是
            //包含文件以及目录路径的一个数组
            //如果你指向Delete目标文件下面的
            //文件而不包含目录请使用下面的方法 
            //string[]fileList=  Directory.GetFiles(aimPath); 
            string[]fileList= 
                Directory.GetFileSystemEntries(aimPath); 
            //遍历所有的文件和目录
            foreach(string file in fileList) 
            ...{
                //先当作目录处理如果存在这个
                //目录就递归Delete该目录下面的文件
                if(Directory.Exists(file)) 
                ...{
                    DeleteDir(
                        aimPath+Path.GetFileName(file)); 
                } 
                //否则直接Delete文件
                else
                ...{
                    File.Delete(
                        aimPath+Path.GetFileName(file));
                } 
            } 
            //删除文件夹
            System.IO.Directory.Delete(aimPath,true); 
        } 
        catch(Exceptione) 
        ...{ 
            MessageBox.Show(e.ToString()); 
        }
    }  

好了关于C#创建文件夹与文件的一些具体实现的操作就先你介绍到这里,希望对你了解和学习C#创建文件夹的过程以及实现相关的权限设置有所帮助。

本文来源:http://www.bbyears.com/jiaocheng/45197.html

热门标签

更多>>

本类排行