2011-12-19 4 views
0

좋은 날,C# 모듈러스 계수

30MB가 넘는 파일을 다운로드하는 곳에서 응용 프로그램을 작성하고 있습니다. 현재 다운로드 된 바이트 수를 추적합니다.

내 질문은 :

가 나는 1M, 2M, 3M 과거 등 이동시기를 결정합니다.

내 논리는 다음과 같습니다

int totalFileSave = 0; 
... 
... 
int bytesRead = responseStream.Read(buffer, 0, 4096); 
totalFileSave += bytesRead; 

while (bytesRead > 0) { 
    // How do I test when I hit 1M, 2M, 3M and so forth... 
    bytesRead = responseStream.Read(buffer, 0, 4096); 
    totalFileSave += bytesRead; 
} 
+2

제목에서 당신이 스스로 해결하는 방법 아이디어가 보인다. 너는 무엇을 시도 했는가? – Russell

+0

@ Russell, 처음 시도했을 때 : if totalFileSave = 1049201 then totalFileSave % 1000000 = 49201 이는 내가 원한 것이 아닙니다. – coson

답변

0

어떻게 이런 일에 대해 :

private const int megaByte = 1024 * 1024; 
private int current = 0; 

while (bytesRead > 0) 
{ 
    bytesRead = responseStream.Read(buffer, 0, 4096); 
    totalFileSave += bytesRead; 

    int total = bytesRead/megaByte; 

    if (total > current) 
    { 
     current = total; 
     // you went up 1 M and are now at or greater than 'current'M 
    } 
} 
1
private const int MEGABYTE = 1024 * 1024; 

if ((bytesRead % MEGABYTE) == 0) 
{ 
    // Do something... 
} 
+1

bytesRead는 언제 4096을 초과합니까? – sq33G