C#基础问题希望得到您的帮助5
namespace 文件流复制
{
class Program
{
static void Main(string[] args)
{
CopyFile("e:\\内容.txt","e:\\内容太.txt");
Console.WriteLine("复制完成");
Console.Read();
}
static void CopyFile(string srcName, string decName)
{
using (FileStream fs = File.OpenRead(srcName))
{
using (FileStream decStream = File.OpenWrite(decName))
{
//int bufferSize = 1024;
//byte[] buffer = new byte[bufferSize];
CopyStream(fs, decStream, 1024);
} //int bufferSize=1024;
//byte[] buffer = new byte[bufferSize];
}
}
static void CopyStream(Stream srcStream, Stream decStream,int bufferSize)
{
byte[] buffer = new byte[bufferSize];
int n;
while ((n = srcStream.Read(buffer, 0, bufferSize)) > 0)
{
decStream.Write(buffer, 0, n);
srcStream.Position = decStream.Position;
}
}
}
}
问题是:FileStream decStream = File.OpenWrite(decName)
这行代码说明 decName 不存在,是先创建文件以后再写入
还是先在主函数里创建文件
CopyFile("e:\\内容.txt","e:\\内容太.txt");
------解决方案--------------------
你这个 CopyFile是 COPY文件。
当然得先有文件e:\\内容.txt
他去复制一个e:\\内容太.txt的文件