c# - How do i check for all the files sizes in a directory while in a loop? -


s = environment.getenvironmentvariable("userprofile") + "\\pictures";             string[] photosfiles = directory.getfiles(t, "*.*", searchoption.alldirectories);             (int = 0; < s.length; i++)             {                  file.copy(photosfiles[i], tempphotos + "\\" + path.getfilename(photosfiles[i]), true);              } 

this copy files 1 directory another. want check time inside loop destination directory size. example first copying 1 file check file size if less 50mb continue.

next itertion in loop after copying second file check 2 files in destination directory size if both files size less 50mb continue. , on untill reaching 50mb stop loop.

you count size of directory before start copying files across , add size of each file copy or recalculate size of directory after each file copy. think latter option more accurate less efficient depending on size of files you're copying (if they're small you'll end counting them many times).

to size of directory use:

public static long dirsize(directoryinfo d)  {         long size = 0;         // add file sizes.     fileinfo[] fis = d.getfiles();     foreach (fileinfo fi in fis)      {               size += fi.length;         }     // add subdirectory sizes.     directoryinfo[] dis = d.getdirectories();     foreach (directoryinfo di in dis)      {         size += dirsize(di);        }     return(size);   } 

function used example here: http://msdn.microsoft.com/en-us/library/system.io.directory(v=vs.100).aspx

so code like:

for (int = 0; < photosfiles.length; i++) {     fileinfo fi(photosfiles[i]);      directoryinfo d = new directoryinfo(tempphotos);     long dirsize = dirsize(d);      //if copying file take directory on 50mb don't     if ((dirsize + fi.length) <= 52428800)         fi.copyto(tempphotos + "\\" + fi.name)     else         break; } 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -