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

maven fortify plugin : Unable to load build session with ID XXXXX .. See log file for more details -

c# - Primavera WebServices does not return any data -

android - Display emoji panel with genymotion - keyboard/touch input? -