创建没有文件c#的文件流
是否可以在没有实际文件的情况下创建文件流?
我会试着解释一下: 我知道如何从真实文件创建流: FileStream s = new FileStream("FilePath",FileMode.Open,FileAccess.Read); 但我可以创建一个伪文件的fileStream吗? 含义: 编辑. 我正在使用具有该代码的API示例: FileStream s = new FileStream("FilePath",FileAccess.Read); try { SolFS.SolFSStream stream = new SolFS.SolFSStream(Storage,FullName,true,false,"pswd",SolFS.SolFSEncryption.ecAES256_SHA256,0); try { byte[] buffer = new byte[1024*1024]; long ToRead = 0; while (s.Position < s.Length) { if (s.Length - s.Position < 1024*1024) ToRead = s.Length - s.Position; else ToRead = 1024 * 1024; s.Read(buffer,(int) ToRead); stream.Write(buffer,(int) ToRead); } 所以它基本上是在某处写入fileStream“s”. 解决方法
显然,您希望拥有一个
FileStream (明确使用其特定于FileStream的属性,如
Name ),它不指向文件.
据我所知,这是不可能基于FileStream的实现. 但是,创建具有所需属性的包装类将是一个简单的解决方案: >您可以在包装器中存储所需的所有属性. 这是一个例子: public class StreamContainer { public StreamContainer(string name,Stream contents) { if (name == null) { throw new ArgumentNullException("name"); } if (contents == null) { throw new ArgumentNullException("contents"); } this.name = name; this.contents = contents; } private readonly string name; public string Name { get { return name; } } private readonly Stream contents; public Stream Contents { get { return contents; } } } 当然,您可以为各种流类型添加一些礼貌的创建方法(作为上述类中的静态方法): public static StreamContainer CreateForFile(string path) { return new StreamContainer(path,new FileStream(path,FileAccess.Read)); } public static StreamContainer CreateWithoutFile(string name) { return new StreamContainer(name,new MemoryStream()); } 在您的应用程序中,无论您想要使用这样的命名流,都要传递StreamContainer,而不是直接期望Stream. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |