c# - Determine corrupt file of "Data Error (Cyclic redundancy check) on IOException" -



c# - Determine corrupt file of "Data Error (Cyclic redundancy check) on IOException" -

given code snippet:

public ienumerable<ifileinfo> getfiles(string searchpattern) { homecoming this.directoryinfo .getfiles(searchpattern) .select(fileinfo => new fileinfowrapper(fileinfo.fullname)); }

suppose physical disk have bad sectors. code called wrapped try/catch clause.

try { var files = this.getfiles("*"); // ... } grab (ioexception ex) { // phone call writes new entry on windows eventlog // - text written ex.message plus ex.stacktrace value in case. new logentry((int)globaleventlogid.fileioerror, ex) { source = assembly.getexecutingassembly().getname().name, entrytype = logentrytype.error, }.write(); }

how can determine what file corrupt if code raises ioexception text: data error (cyclic rendundancy check)?

i modify getfiles() utilize yield return , wrapping new fileinfowrapper() bit try/catch , re-throw custom exception. this:

foreach(var fileinfo in directoryinfo.getfiles(searchpattern)) { seek { yield homecoming new fileinfowrapper(fileinfo.fullname); } catch(exception ex) { throw new myexception(fileinfo.fullname, ex); } }

if want stick linq, utilize microsoft's ix-main bundle has catch extension, think makes pretty ugly code:

this.directoryinfo .getfiles(searchpattern) .selectmany(fileinfo => enumerableex.return(fileinfo) .select(fi => new fileinfowrapper(fi.fullname)) .catch<fileinfowrapper, exception>(ex => { throw new myexception(fileinfo.fullname, ex); }));

c# ioexception

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -