byte - Character Limit in filepath C# -
i have following piece of code uploads file , checks validity. first issue:
if (radupload1.uploadedfiles.count == 0) { session[appconstants.error_message] = errorslist.geterrormessage( errorslist.err_p_date_file_valid); } else { if (radupload1.uploadedfiles.count > 0) { foreach (uploadedfile validfile in radupload1.uploadedfiles) { fileinfo fi = new fileinfo(validfile.filename); stream fs = validfile.inputstream; idbcontextualrecord pfile = statuscontext.createandaddrecordforinsert(partstoredfile.t_name); pfile[partstoredfile.c_partid] = _part[part.c_id]; string targetfolder = appsession.current.configparameters[appconstants.upload_file_path] + "\\partrec\\" + _part[part.c_id] + "\\" + pfile[partstoredfile.c_id]; long bytesonthestream = 0l; try { directoryinfo dir = new directoryinfo(targetfolder); if (dir.exists == false) dir.create(); string fullfilename = path.combine(targetfolder, fi.name); stream targetstream = file.openwrite(fullfilename); byte[] buffer = new byte[appconstants.buff_size]; int bytesread; // while read method returns bytes // keep writing them output stream while ((bytesread = fs.read(buffer, 0, appconstants.buff_size)) > 0) { targetstream.write(buffer, 0, bytesread); bytesonthestream += bytesread; } fs.close(); targetstream.close(); } catch (exception ex) { throw ex; }
what want check if number of characters in filepath name exceeds 260 display me message of error.
this second issue after modification made:
if (radupload1.uploadedfiles.count <= 0) { session[appconstants.error_message] = errorslist.geterrormessage( errorslist.err_p_date_file_valid); } else { if (radupload1.uploadedfiles.count > 0 ) { foreach (uploadedfile validfile in radupload1.uploadedfiles) { pomdoc = (idbcontextualrecord)session[appconstants.pom_document_new]; fileinfo fi = new fileinfo(validfile.filename); stream fs = validfile.inputstream; idbcontextualrecord pomfile = pomcontext.createandaddrecordforinsert(pomfile.t_name); pomfile[pomfile.c_pomdocumentid] = pomdoc[pomdocument.c_id]; string targetfolder = appsession.current.configparameters[appconstants.upload_file_path] + "\\pom\\" + pomdoc[pomdocument.c_id] + "\\" + pomfile[pomfile.c_id]; long bytesonthestream = 0l; try { directoryinfo dir = new directoryinfo(targetfolder); if (dir.exists == false) dir.create(); string fullfilename = path.combine(targetfolder, fi.name); if (fullfilename.length > 260) { throw new exception(string.format("the filename long!",fullfilename)); } stream targetstream = file.openwrite(fullfilename); byte[] buffer = new byte[appconstants.buff_size]; int bytesread; // while read method returns bytes // keep writing them output stream while ((bytesread = fs.read(buffer, 0, appconstants.buff_size)) > 0) { targetstream.write(buffer, 0, bytesread); bytesonthestream += bytesread; } fs.close(); targetstream.close(); } catch (exception ex) { throw ; }
you have compare fullfilename.lenght
260 , raise exception if needed:
if (radupload1.uploadedfiles.count <= 0) // changed condition remove check within else block { session[appconstants.error_message] = errorslist.geterrormessage( errorslist.err_p_date_file_valid); } else { foreach (uploadedfile validfile in radupload1.uploadedfiles) { fileinfo fi = new fileinfo(validfile.filename); stream fs = validfile.inputstream; idbcontextualrecord pfile = statuscontext.createandaddrecordforinsert(partstoredfile.t_name); pfile[partstoredfile.c_partid] = _part[part.c_id]; string targetfolder = appsession.current.configparameters[appconstants.upload_file_path] + "\\partrec\\" + _part[part.c_id] + "\\" + pfile[partstoredfile.c_id]; long bytesonthestream = 0l; try { directoryinfo dir = new directoryinfo(targetfolder); if (dir.exists == false) dir.create(); string fullfilename = path.combine(targetfolder, fi.name); if(fullfilename.length > 260) { throw new exception(string.format("the filename {0} long.", fullfilename)); // or whatever want } stream targetstream = file.openwrite(fullfilename); byte[] buffer = new byte[appconstants.buff_size]; int bytesread; // while read method returns bytes // keep writing them output stream while ((bytesread = fs.read(buffer, 0, appconstants.buff_size)) > 0) { targetstream.write(buffer, 0, bytesread); bytesonthestream += bytesread; } fs.close(); targetstream.close(); } catch (exception ex) { throw; }
also, don't want throw ex;
rather throw;
or reset stacktrace, see is there difference between "throw" , "throw ex"?
Comments
Post a Comment