2011-11-18 71 views
0

我寫了一個「更新程序」以保持我的開發團隊其餘部分的最新.exe。它曾經工作得很好,但突然停止工作。用於文件傳輸的C#遠程流不會讀取

我已經注意到了這個問題:我的遠程流沒有開始讀取。

 Uri patch = new Uri("http://********/*********/" + GetVersion().ToString() + ".exe"); 
     Int64 patchsize = PatchSize(patch); 
     var CurrentPath = String.Format("{0}\\", Environment.CurrentDirectory); 
     Int64 IntSizeTotal = 0; 
     Int64 IntRunning = 0; 
     string strNextPatch = (version + ".exe"); 

     using (System.Net.WebClient client = new System.Net.WebClient()) 
     { 
      using (System.IO.Stream streamRemote = client.OpenRead(patch)) 
      { 
       using (System.IO.Stream streamLocal = new FileStream(CurrentPath + strNextPatch, FileMode.Create, FileAccess.Write, FileShare.None)) 
       { 
        int intByteSize = 0; 

        byte[] byteBuffer = new Byte[IntSizeTotal]; 

        while ((intByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0) 
        { 
         streamLocal.Write(byteBuffer, 0, intByteSize); 

         IntRunning += intByteSize; 

         double dIndex = (double)(IntRunning); 
         double dTotal = (double)byteBuffer.Length; 
         double dProgressPercentage = (dIndex/dTotal); 
         int intProgressPercentage = (int)(dProgressPercentage * 100); 

         worker.ReportProgress(intProgressPercentage); 
        } 
        streamLocal.Close(); 
       } 
       streamRemote.Close(); 

GetVersion()只返回當前服務器版本的.exe的當前版本號。 問題就出在這裏:

while ((intByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0) 

我streamRemote只是不返回所以這個條款的同時不填充任何字節。

對我有何建議?

+0

與您的問題無關,但可以刪除多餘的Close()調用 - 這就是using語句的用途。您也可以將所有使用情況堆疊在一起,並在一個縮進級別阻止。 – GazTheDestroyer

+0

謝謝!我這樣做是爲了讓我的代碼更加「概覽」。 – Nop0x

回答

0

我相信問題出在服務器上。我會執行一些檢查:

  • Web服務器的配置有什麼變化,阻止您下載可執行文件?
  • 你是通過代理連接嗎?
  • 您可以手動獲取相同的URL(在您的應用程序的相同用戶憑據下)嗎?
+0

恩,謝謝你的幫助,但是我已經有了問題。 byte [] byteBuffer = new Byte [IntSizeTotal]; 必須是 byte [] byteBuffer = new Byte [patchsize]; 現在它的工作:) – Nop0x

+1

好吧,認爲這是一個外部的問題,因爲你說「它曾經工作正常,但突然它停止工作」:) – Strillo