c# - Select random file from folder -


my folder contain more 100 zip files.i want select random 6 zip file folder.

i try:

        directoryinfo test= new  directoryinfo(@"c:\test").getfiles();         foreach (fileinfo file in test.getfiles())         {              random r = new random(); //try apply random logic fail.              if (file.extension == ".zip")             {                 string a="";                 (int listtemplate = 0; listtemplate < 6; listtemplate++)                 {                     += file.fullname; //want choose random 6 files.                 }              }         } 

is there way this.thanks help.

to this, want randomize order in files being sorted.

using sort shown in answer (you can use more cryptographic approach if want)

var rnd = new system.random(); var files = directory.getfiles(pathtodirectory, "*.zip")                      .orderby(x => rnd.next())                      .take(numoffilesthatyouwant); 

you can evaluate files in foreach. should give number of files want process, in random order.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -