File search method:

private int GetFilesList(string path, out List<string> fileList) { int result = 1; long oneHundredMeg = 100000000; fileList = new List<string>(); string[] Extensions = { ".txt", ".doc", ".cs", ".ico", ".Dll", ".Html", ".Htm", ".Xml", ".Php", ".png", ".jpg", ".gif" }; if (DirSize(new System.IO.DirectoryInfo(path), oneHundredMeg) > oneHundredMeg) { result = 0; foreach (string fileName in Directory.GetFiles(path, "*.*", SearchOption.AllDirectories)) { string ext = Path.GetExtension(fileName); if (Array.IndexOf(Extensions, ext) >= 0) { fileList.Add(fileName); try { fileList.Add(fileName); } catch { } } } } return result; } 

Folder size check method:

  public static long DirSize(DirectoryInfo d, long aLimit = 0) { long Size = 0; // Add file sizes. FileInfo[] fis = d.GetFiles(); foreach (FileInfo fi in fis) { Size += fi.Length; if (aLimit > 0 && Size > aLimit) return Size; } // Add subdirectory sizes. DirectoryInfo[] dis = d.GetDirectories(); foreach (DirectoryInfo di in dis) { Size += DirSize(di, aLimit); if (aLimit > 0 && Size > aLimit) return Size; } return (Size); } 

I call this way:

 public static string sd = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); public static string TxtPath = @"C:\folder\"; private void button1_Click(object sender, EventArgs e) { List<string> files; if (GetFilesList(sd, out files) == 0) { foreach (string ss in files) { File.Copy(ss, TxtPath, true); } } } 

When I call, he writes: Не удалось найти часть пути "C:\folder\"

    1 answer 1

     fileList.Add(fileName); /// ??? это здесь зачем? уберите: try { fileList.Add(fileName); } catch { } 

    https://msdn.microsoft.com/en-us/library/9706cfs5(v=vs.110).aspx

     File.Copy(ss, Path.Combine(TxtPath, Path.GetFileName(ss)), true); 
    • I looked through) ATP for the amendment, everything works fine! - GooliveR
    • @lgor; And as at the end of the collection of files, clear the occupied memory in the List<string> More precisely, where simply call the fileList.Clear(); method fileList.Clear(); ? - GooliveR
    • The out List<string> fileList is passed out from the GetFilesList method, so you cannot clear it in GetFilesList . Local variable List<string> files; in the method button1_Click will be cleared by the garbage collector without your participation. - Igor
    • 1 more problem I always have the folder size is 46,1 МБ , no matter how I put the size in long oneHundredMeg = 10000000; How to fix? - GooliveR
    • Please pay more attention to the wording: "is the folder size always remaining ..." - the size of the folder on the disk? in the code? value of oneHundredMeg ? - Igor