我寫了一個「更新程序」以保持我的開發團隊其餘部分的最新.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只是不返回所以這個條款的同時不填充任何字節。
對我有何建議?
與您的問題無關,但可以刪除多餘的Close()調用 - 這就是using語句的用途。您也可以將所有使用情況堆疊在一起,並在一個縮進級別阻止。 – GazTheDestroyer
謝謝!我這樣做是爲了讓我的代碼更加「概覽」。 – Nop0x