zoukankan      html  css  js  c++  java
  • 创建文件夹 文件File操作方法

    protected void Page_Load(object sender, EventArgs e)
    {
    string strPath = Server.MapPath("upload");
    List<FileInfo> fList = GetAllFilesInDirectory(strPath);

    foreach (var item in fList)
    {
    Response.Write(item.FullName + "<br>");
    }

    // DirectoryInfo df=new DirectoryInfo()
    string userPath = "test";
    if (Directory.Exists(Server.MapPath("upload/" + userPath)))
    {
    Response.Write("true");
    DirectoryInfo userFile = new DirectoryInfo(Server.MapPath("upload/" + userPath));
    long fileLong = GetDictSize(Server.MapPath("upload"));

    //float f = float.MinValue;
    //float.TryParse((fileLong * 1000 / (1024 * 1024)).ToString(), out f);
    //Response.Write(f.ToString());

    float f = (float)fileLong;
    Response.Write((f / (1024 * 1024)));

    }
    else
    {
    Response.Write("false");
    }

    }


    private long GetDictSize(string path)
    {
    if (!System.IO.Directory.Exists(path))
    return 0;
    string[] fs = System.IO.Directory.GetFiles(path, "*.*", System.IO.SearchOption.AllDirectories);
    //获取该文件夹中所有的文件名
    long ll = 0;
    foreach (string f in fs)
    {
    dynamic fa = System.IO.File.GetAttributes(f);
    System.IO.FileInfo fi = new System.IO.FileInfo(f);
    ll += fi.Length;
    }
    return ll;
    }

    /// <summary>
    /// 返回指定目录下的所有文件信息
    /// </summary>
    /// <param name="strDirectory"></param>
    /// <returns></returns>
    public List<FileInfo> GetAllFilesInDirectory(string strDirectory)
    {
    List<FileInfo> listFiles = new List<FileInfo>(); //保存所有的文件信息
    DirectoryInfo directory = new DirectoryInfo(strDirectory);
    DirectoryInfo[] directoryArray = directory.GetDirectories();
    FileInfo[] fileInfoArray = directory.GetFiles();
    if (fileInfoArray.Length > 0) listFiles.AddRange(fileInfoArray);
    foreach (DirectoryInfo _directoryInfo in directoryArray)
    {
    DirectoryInfo directoryA = new DirectoryInfo(_directoryInfo.FullName);
    DirectoryInfo[] directoryArrayA = directoryA.GetDirectories();
    FileInfo[] fileInfoArrayA = directoryA.GetFiles();
    if (fileInfoArrayA.Length > 0) listFiles.AddRange(fileInfoArrayA);
    GetAllFilesInDirectory(_directoryInfo.FullName);//递归遍历
    }
    return listFiles;
    }

    http://www.58os.com

  • 相关阅读:
    Luogu P4316 绿豆蛙的归宿 题解报告
    Luogu P1850 换教室(NOIP 2016) 题解报告
    Rainbow的信号 题解报告
    $CH5105 Cookies$ 线性$DP+$贪心
    算法竞赛 $0×50$ 动态规划 (+一本通
    $CH5104 I-country$ 线性$DP$
    洛谷$2014$ 选课 背包类树形$DP$
    $SP703 Mobile Service DP$
    $POJ1015 Jury Compromise Dp$/背包
    $POJ1742 Coins$ 多重背包+贪心
  • 原文地址:https://www.cnblogs.com/guoxiongfei/p/2760587.html
Copyright © 2011-2022 走看看