如何在构造函数中创建新实例FileStream而不指定其他参数,而不是源FileStream中的参数。 我该怎么做?
FileStream fs= new FileStream([any parametrs]);
FileStream copy1= new FileStream(parametrs from fs); // First variant
FileStream copy2= new FileStream(fs); // Or second variant
例如,我创建文件流
FileStream fs = new FileStream(_fileName, FileMode.Open, FileAccess.Read, FileShare.Read, 4 * 1024, true)
现在我想要很多独立的副本,但fs
不会保存从构造函数收到的任何参数。
像这样解决
class FileStreamExt : FileStream
{
private string _fileName;
private FileMode _mode;
public FileStreamExt Clone()
{
return new FileStreamExt(_fileName, _mode);
}
public FileStreamExt(string filename, FileMode mode)
: base(filename, mode)
{
_fileName = filename;
_mode = mode;
}
}
FileStreamExt fs = FileStreamExt(_fileName,FileAccess.Read);
FileStreamExt copy = fs.Clone();
What's about Stream.CopyTo();
FileStream fs= new FileStream([any parametrs]);
var copy1 = new MemoryStream();
fs.CopyTo(copy1)
它应该工作