zoukankan      html  css  js  c++  java
  • C#.net磁盘管理以及文件操作

    原文发布时间为:2008-08-08 —— 来源于本人的百度文章 [由搬家工具导入]

       需要引入的命名空间:

    using System.IO;
    using System.Text;

    private DirectoryInfo di;
    private FileInfo fi;

    文件与文件夹 FileSystemInfo

    文件 FileInfo

    文件夹 DirectoryInfo

    创建文件

    FileInfo fi=new FileInfo(path);
               if (!fi.Exists)
                   fi.Create();

    创建文件夹
    DirectoryInfo    di = new DirectoryInfo(path);
                if (!di.Exists)
                    di.Create();

    删除文件夹 di.Delete();

    删除文件 fi.Delete();

    移动文件夹 di.MoveTo(newpath);

    移动文件 fi.MoveTo(newfpath);

    重命名文件夹 di.MoveTo(newfpath); 【原路径相同,只是文件名字不同】

    重命名文件 fi.MoveTo(newfpath);【原路径相同,只是文件名字不同】

    复制文件: fi.CopyTo(newfpath);

    复制文件夹:

    protected void dirCopy(string oldpath,string newpath)
        {
             di = new DirectoryInfo(oldpath);
            foreach(FileSystemInfo fsi in di.GetFileSystemInfos())
            {
                if(fsi is FileInfo)
                {
                    fi = (FileInfo)fsi;
                    if(!Directory.Exists(newpath))
                    {
                       DirectoryInfo newDir= Directory.CreateDirectory(newpath);
                       fi.CopyTo(newDir.FullName+");
                    }            
                    else
                   {
                       fi.CopyTo(newpath+");
                   }
                }
                else
                {
                    DirectoryInfo child_di=(DirectoryInfo)fsi;
                    string olddir=child_di.FullName;
                    string dirname=child_di.FullName.Substring(child_di.FullName.LastIndexOf(");
                    string newchildpath=Path.Combine(newpath,dirname);
                    if(!Directory.Exists(olddir))
                        Directory.CreateDirectory(olddir);
                    dirCopy(olddir,newchildpath);
                }

            }
        }

    文件的编辑:

    FileStream fs = new FileStream(fpath,FileMode.Create, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs, Encoding.Default);
            sw.WriteLine(information);
            sw.Close();
            fs.Close();

    文件的读取:

    File.ReadAllText(fpath,Encoding.Default);

    文件及文件夹的一些属性获取:

    di = new DirectoryInfo(fpath);
                foreach (FileSystemInfo fsi in di.GetFileSystemInfos())
                {
                    if (fsi is FileInfo)
                    {
                        FileInfo fi = (FileInfo)fsi;
                        fname = fi.Name;
                        if (fi.Extension.Length < 1)
                            fexp = "";
                        else
                            fexp = fi.Extension.Remove(0, 1);
                        fsize = fi.Length.ToString();
                        ftime = fi.LastWriteTime.ToString();
                    }
                    else
                    {
                        di = (DirectoryInfo)fsi;
                        fname = di.Name;
                        fexp = "文件夹";
                        fsize = "";
                        ftime = di.LastWriteTime.ToString();
                    }
    ……

    }

  • 相关阅读:
    mysql查询版本
    mysql导出bug备注
    linux查找文件
    linux 编译式安装nginx
    linux文件校验
    linux 编译式安装apache
    网络层
    数据链路层
    物理层
    servlet过滤器
  • 原文地址:https://www.cnblogs.com/handboy/p/7143818.html
Copyright © 2011-2022 走看看