c# - How can i get a file name size in bytes and then reportprogress? -


in top of form1 have string array , string file name:

string[] stringprogressreport = new string[4]; string filename = ""; 

then in openfiledialog i'm getting filename.

then have 2 events:

private void videosinsertrequest_responsereceived(video obj) {     stringprogressreport[0] = obj.status.uploadstatus;     backgroundworker1.reportprogress(0, 0); } 

and

private void videosinsertrequest_progresschanged(iuploadprogress obj) {     stringprogressreport[1] = obj.status.tostring();     backgroundworker1.reportprogress(0, 1);     double mbsent = ((double)obj.bytessent) / (1 << 20);     stringprogressreport[2] = mbsent.tostring();     backgroundworker1.reportprogress(0, 2);     double percentcomplete = ((double)obj.bytessent) / totalbytes * 100;     stringprogressreport[3] = percentcomplete.tostring();     backgroundworker1.reportprogress(0, 3); } 

totalbytes should file size in bytes if i'm not wrong calculation of percentcomplete.

then, in end, have backgroundworker1_progresschanged event:

private void backgroundworker1_progresschanged(object sender, progresschangedeventargs e) {     int eventindex = (int)e.userstate;      if (eventindex == 0)     {         toolstripstatuslabel1.text = stringprogressreport[0];     }     else     {         toolstripstatuslabel2.text = stringprogressreport[1];     } } 

when reported progress 2 items stringprogressreport in 2 events working fine.

but now, should if in backgroundworker1_progresschanged event when reporting 4 items ?

and how calculate percentcomplete , getting file size in bytes ?

getting file size in bytes. think got it. i'm doing:

using (filestream filestream = file.openread(filename)) {     totalbytes = filestream.length; } 

totalbytes type long , global. , i'm doing in openfiledialog right after selecting file.

if part fine, need solve second problem.

i tried in backgroundworker_progresschanged add line in else:

toolstripprogressbar1.value = int32.parse(stringprogressreport[3]); 

but i'm getting following exception on line:

formatexception: input string not in correct format

system.formatexception occurred   hresult=-2146233033   message=input string not in correct format.   source=mscorlib   stacktrace:        @ system.number.stringtonumber(string str, numberstyles options, numberbuffer& number, numberformatinfo info, boolean parsedecimal)        @ system.number.parseint32(string s, numberstyles style, numberformatinfo info)        @ system.int32.parse(string s)        @ youtube_manager.form1.backgroundworker1_progresschanged(object sender, progresschangedeventargs e) in d:\c-sharp\youtube-manager\youtube-manager\youtube-manager\form1.cs:line 313   innerexception:  

well, way reporting bit unusual least. if try follow pattern, backgroundworker1_progresschanged want change this:

private void backgroundworker1_progresschanged(object sender, progresschangedeventargs e) {     int eventindex = (int)e.userstate;      if (eventindex == 0) // upload status.     {         toolstripstatuslabel1.text = stringprogressreport[0];     }     else if (eventindex == 1) // obj.status     {         toolstripstatuslabel2.text = stringprogressreport[1];     }     else if (eventindex == 2) // mb sent far     {         // ??? want put ??? = stringprogressreport[2];     }     else if (eventindex == 3) // percent complete     {         toolstripprogressbar1.value = int32.parse(stringprogressreport[3]);     }     else     {         throw new exception("invalid event index: " + eventindex);     } } 

and then, address second question: reason why toolstripprogressbar1.value = int32.parse(stringprogressreport[3]); throws formatexception because trying format decimal. since want use integer, suggest fixing these 2 lines:

double percentcomplete = ((double)obj.bytessent) / totalbytes * 100; stringprogressreport[3] = percentcomplete.tostring(); 

... changing them instead make sure have integer:

int percentcomplete = (int)math.round(((double)obj.bytessent) / totalbytes * 100); stringprogressreport[3] = percentcomplete.tostring(); 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -