我有這個c#程序,它是一個客戶端從服務器接收文件。有時它可以無縫工作。有時在fileName = Encoding.ASCII.GetString(dataByte, 4, fileNameLen);
中有例外。c從一個系統到另一個系統的文件傳輸#
ArgumentOutOfRange Exception
Index and count must refer to a location within the buffer.
Parameter name: bytes
如果fileNameLen
值是8
或12
然後它工作正常。否則它將是1330795077
。這是爲什麼?任何人都可以解釋我爲什麼這樣嗎?請。這是我的代碼。
string fileName = string.Empty;
int thisRead = 0;
int blockSize = 1024;
Byte[] dataByte = new Byte[blockSize];
lock (this)
{
string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)+"\\";
ns.Read(dataByte, thisRead, blockSize);
int fileNameLen = BitConverter.ToInt32(dataByte, 0);
fileName = Encoding.ASCII.GetString(dataByte, 4, fileNameLen);
Stream fileStream = File.OpenWrite(folderPath + fileName);
fileStream.Write(dataByte, 4 + fileNameLen, (1024 - (4 + fileNameLen)));
while (true)
{
thisRead = ns.Read(dataByte, 0, blockSize);
fileStream.Write(dataByte, 0, thisRead);
if (thisRead == 0)
break;
}
fileStream.Close();
}