如题,在写程序时,找不到这个方法NetworkStream ns = client.GetStream();
using (FileStream fs = File.Open(fileName, FileMode.Open))
{
fs.CopyTo(ns);
}错误 1 “System.IO.FileStream”不包含“CopyTo”的定义,并且找不到可接受类型为“System.IO.FileStream”的第一个参数的扩展方法“CopyTo”(是否缺少 using 指令或程序集引用?) D:\开发工具\Projects\TCP+UDP\SyncChatClient\ClientForm.cs 134 20 Client但是我打开个新的项目就有这个方法。有using System.IO;
using (FileStream fs = File.Open(fileName, FileMode.Open))
{
fs.CopyTo(ns);
}错误 1 “System.IO.FileStream”不包含“CopyTo”的定义,并且找不到可接受类型为“System.IO.FileStream”的第一个参数的扩展方法“CopyTo”(是否缺少 using 指令或程序集引用?) D:\开发工具\Projects\TCP+UDP\SyncChatClient\ClientForm.cs 134 20 Client但是我打开个新的项目就有这个方法。有using System.IO;
你出问题程序的目标框架可能被设置为2.0或3.5了。
你出问题程序的目标框架可能被设置为2.0或3.5了。
真好,我在3.5里面自己写了一个CopyTo方法,方法和它的基本一样,唯一不同的是,它多了很多判断(似乎多余),同时自己实现还可以控制速度,它的就不可以。
附上4.0里面实现的源码:public void CopyTo(Stream destination)
{
if (destination == null)
{
throw new ArgumentNullException("destination");
}
if (!this.CanRead && !this.CanWrite)
{
throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_StreamClosed"));
}
if (!destination.CanRead && !destination.CanWrite)
{
throw new ObjectDisposedException("destination", Environment.GetResourceString("ObjectDisposed_StreamClosed"));
}
if (!this.CanRead)
{
throw new NotSupportedException(Environment.GetResourceString("NotSupported_UnreadableStream"));
}
if (!destination.CanWrite)
{
throw new NotSupportedException(Environment.GetResourceString("NotSupported_UnwritableStream"));
}
this.InternalCopyTo(destination, 4096);
}
private void InternalCopyTo(Stream destination, int bufferSize)
{
byte[] array = new byte[bufferSize];
int count;
while ((count = this.Read(array, 0, array.Length)) != 0)
{
destination.Write(array, 0, count);
}
}