File 读取和写入大文件大小c
我正在写一个文件传输应用程序来发送和接收像1GB这样的大数据。。但我认为,当我从文件中读取数据并将其填充到字节数组中时,它会存储在RAM中,这会影响计算机的速度。。我应该喜欢:File 读取和写入大文件大小c,file,send,File,Send,我正在写一个文件传输应用程序来发送和接收像1GB这样的大数据。。但我认为,当我从文件中读取数据并将其填充到字节数组中时,它会存储在RAM中,这会影响计算机的速度。。我应该喜欢: (loop till end of the file) { read 128 MB from the file into byte array (loop till end of 128) { send 1 kb to server } byte array = null }
(loop till end of the file)
{
read 128 MB from the file into byte array
(loop till end of 128)
{
send 1 kb to server
}
byte array = null
}
如果是这样的话。。
哪一个更好做!!beginSend和beginReceive发送大文件,或直接循环发送文件
如果你教我一些代码,我会很高兴的
提前感谢:如果您[开始]一次发送超过约1MB的数据,Windows将开始出现异常行为。这在Windows版本、网络驱动程序、用户鞋子尺寸和moon phase之间有所不同。低于1MB您应该可以 所以,也
(loop till end of the file)
{
read 128 MB from the file into byte array
(loop till end of 128)
{
send 1 MB to server
}
byte array = null
}
或者,如果它真的是一个文件
SendFile(filename[,...])
即使是128mb也不是一个好办法。。最好读一个小的缓冲区。。然后直接送到另一边 看看吧 将文件名和文件大小发送到另一端后 这在服务器/客户端中应该很常见
FileStream fs;
NetworkStream network;
int packetSize = 1024*8;
发送方法
public void Send(string srcPath, string destPath)
{
byte data;
string dest = Path.Combine(destPath, Path.GetFileName(srcPath));
using (fs = new FileStream(srcPath, FileMode.Open, FileAccess.Read))
{
try
{
long fileSize = fs.Length;
long sum = 0;
int count = 0;
data = new byte[packetSize];
while (sum < fileSize)
{
count = fs.Read(data, 0, packetSize);
network.Write(data, 0, count);
sum += count;
}
network.Flush();
}
finally
{
fs.Dispose();
data = null;
}
}
}
接收方法:
public void Receive(string destPath, long fileSize)
{
byte data;
using (fs = new FileStream(destPath, FileMode.Create, FileAccess.Write))
{
try
{
int count = 0;
long sum = 0;
data = new byte[packetSize];
while (sum < fileSize)
{
count = network.Read(data, 0, packetSize);
fs.Write(data, 0, count);
sum += count;
}
}
finally
{
fs.Dispose();
data = null;
}
}
}
事实上我试图弄清楚SendFile是如何工作的,但我做不到,因为没有ReceiveFile。。所以我不知道如何从另一边接收文件。。它是通过循环发送到文件末尾还是使用什么缓冲区。。!!你知道吗?