I am trying to copy (an array of file extensions) to a folder (and to this folder which I copy to make a 100MB limit), if the limit is exceeded the files in the folder, stop copying! Here is the code:
public static void GetFilesList() { long DS = 1000000; string[] extens = { ".txt", ".doc", ".cs", ".ico", ".Dll", ".Html", ".Htm", ".Xml", ".Php", ".png", ".jpg", ".gif" }; if (DirSize(new DirectoryInfo(Easy.GooVer), DS) > DS) { foreach (string fileName in Directory.GetFiles(Easy.GooVer, "*.*", SearchOption.AllDirectories)) { string ext = Path.GetExtension(fileName); if (Array.IndexOf(extens, ext) >= 0) { try{ File.Copy(fileName, Path.Combine(Easy.str1, Path.GetFileName(fileName)), true);}catch { } } } } } public static long DirSize(DirectoryInfo d, long aLimit = 0) { long Size = 0; FileInfo[] fis = d.GetFiles(); foreach (FileInfo fi in fis) { Size += fi.Length; if (aLimit > 0 && Size > aLimit) return Size; } DirectoryInfo[] dis = d.GetDirectories(); foreach (DirectoryInfo di in dis) { Size += DirSize(di, aLimit); if (aLimit > 0 && Size > aLimit) return Size; } return (Size); }
if (DirSize(new DirectoryInfo(Easy.GooVer), DS) > DS)? Are you sure about>? - VladDcatch {}in case the file could not be copied (although it would be better to check if the copied file exists at all, and if so, check its size). - Surfin Birdwhile(true), but apparently it does not channel, I don’t know where to go) - GooliveR