serialization - What are the best practices for file handling in c#? -


i have couple questions on how file handling properly. use binary serialization , deserialization in program. on top of export , import text files. listed example below of serialization code. use openfile dialog select folders/files.

this binary serialization method:

if (string.isnullorwhitespace(filename)) {     throw new argumentoutofrangeexception("filename"); }  filestream filestream = new filestream(filename, filemode.openorcreate);  try {     binaryformatter binaryformatter = new binaryformatter();     binaryformatter.serialize(filestream, animals); } {     filestream.close(); } 

and these exceptions catch:

try {     administration.load(filename); } catch (notsupportedexception nonsupportedexception) {     messagebox.show(nonsupportedexception.message); } catch (unauthorizedaccessexception unauthorizedaccesexception) {     messagebox.show(unauthorizedaccesexception.message); { catch (securityexception securityexception) {     messagebox.show(securityexception.message); } catch (directorynotfoundexception directorynotfoundexception) {     messagebox.show(directorynotfoundexception.message); } catch (ioexception ioexception) {     messagebox.show(ioexception.message); } 

i catch same exceptions deserialization. difference there these 3 lines:

if (file.exists(filename)) { } filestream filestream = new filestream(filename, filemode.open); animals = formatter.deserialize(filestream) list<animal>; 

what should if wrong data? example: half of file has right data , other half doesn't.

how should write unit tests serialization? many exceptions securityexception hard test.

what exceptions should catch? looked @ msdn, not sure if should straight catch of exceptions listed. deliberately don't catch argumentoutofrange exception example, because don't want catch programming mistakes.

i have same questions reading , writing text files. there difference in testing/exception catching? difference use streamwriter writing text files. , use file.readalllines read text file select.

thank you.

your question not fit stack overflow requirements largely depend on things haven't told us, , don't know yet. have ask customer want, figure out simplest thing possibly work, send customer , have them try out, incorporate fixes next iteration. not think can design out in advance , first attempt perfect.

using binary serialization data files mistake. format poorly documented , subject change. can't use other platforms, or incompatible frameworks in same platform. has poor performance hierarchical data structures. if needs simple, start text json or xml files. if have complex data or need enforce data consistency should use sql.

code mistake:

catch (notsupportedexception nonsupportedexception) {     messagebox.show(nonsupportedexception.message); } 

catch fixing errors , messagebox.show not fix errors; hides errors continues on if nothing went wrong. did go wrong; need figure out , fix it. if can't fix not catch error; let enclosing methods handle it.


Comments