c# - MD5 each file in a folder and also MD5 the folder -
if please ask assistance problem have, i'd open folder display each file , hash , @ end of displayed files i'd hash display of total folder structure. code below isn't correct adds path md5 file md5.
the code below displays each file in listbox , under hash hash code hash folder repeated each file.
private void btnfolder_click(object sender, eventargs e) { dialogresult result = folderbrowserdialog1.showdialog(); if (result == dialogresult.ok) { _path = folderbrowserdialog1.selectedpath; txtfolder.text = _path; // assuming want include nested folders var files = directory.getfiles(_path, "*.*", searchoption.topdirectoryonly) .orderby(p => p).tolist(); foreach (string items in files) { md5 md5 = md5.create(); (int = 0; < files.count; i++) { string file = files[i]; // hash path string relativepath = file.substring(_path.length + 1); byte[] pathbytes = encoding.utf8.getbytes(relativepath.tolower()); md5.transformblock(pathbytes, 0, pathbytes.length, pathbytes, 0); // hash contents byte[] contentbytes = file.readallbytes(file); if (i == files.count - 1) md5.transformfinalblock(contentbytes, 0, contentbytes.length); else md5.transformblock(contentbytes, 0, contentbytes.length, contentbytes, 0); } lstbox.items.add(items); lstbox.items.add(bitconverter.tostring(md5.hash).replace("-", "").tolower()); } } else { return; } }
thanks in advance assistance.
below code output desired output , requirements.
please read "note" sections in code more information.
you should not run in ui thread since lock down until files have been processed. please @ refactoring method can called in thread.
private void btnfolder_click(object sender, eventargs e) { dialogresult result = folderbrowserdialog1.showdialog(); if (result == dialogresult.ok) { _path = folderbrowserdialog1.selectedpath; txtinput.text = _path; // assuming want include nested folders var files = directory.getfiles(_path, "*.*", searchoption.topdirectoryonly) .orderby(p => p).tolist(); md5 totalmd5 = md5.create(); int bytestoreadatonce = 2048; // note: can changed bigger or smaller. foreach (string singlefile in files) { md5 singlemd5 = md5.create(); // hash contents // note: nice small files, memory eater big files //byte[] contentbytes = file.readallbytes(singlefile); //singlemd5.transformfinalblock(contentbytes, 0, contentbytes.length); using (filestream inputfile = file.openread(singlefile)) { byte[] content = new byte[bytestoreadatonce]; int bytesread = 0; // read file in chunks, allowing minimal memory usage. while ((bytesread = inputfile.read(content, 0, bytestoreadatonce)) > 0) { totalmd5.transformblock(content, 0, bytesread, content, 0); singlemd5.transformblock(content, 0, bytesread, content, 0); } // close singlemd5 block 0 length singlemd5.transformfinalblock(content, 0, 0); // output per file lstbox.items.add(string.format("file: {0}", singlefile)); lstbox.items.add(string.format("md5 : {0}", bitconverter.tostring(singlemd5.hash).replace("-", "").toupper())); } } // close totalmd5 empty byte[] , 0 length (basically nothing close block) totalmd5.transformfinalblock(new byte[0], 0, 0); // output total lstbox.items.insert(0, environment.newline); lstbox.items.insert(0, string.format("total md5 : {0}", bitconverter.tostring(totalmd5.hash).replace("-", "").toupper())); lstbox.items.insert(0, string.format("root path : {0}", _path)); } else { return; } }
with change read each file in chunks, accidentally let code run on folder 287k files in it, totalling ~41gb size.
memory usage didn't go above 7mb during entire processing of directory.
Comments
Post a Comment