从WPF中的包URI打开文件

我希望从应用程序包中打开一个.csv文件来进行一些unit testing。 所以我真正喜欢的是模拟File.ReadAllText(string path) ,而不是X.ReadAllText(Uri uri) 。 我还没有找到这个。

有没有人知道是否可以从包中的文件读取文本/字节(不介意哪些)而不首先将此文件编译到磁盘?

哦,顺便说File.ReadAllText(@"pack://application:,,,/SpreadSheetEngine/Tests/Example.csv")File.ReadAllText(@"pack://application:,,,/SpreadSheetEngine/Tests/Example.csv")对我来说不起作用..我已经在做var app = new Application()确保我在unit testing期间启动了一个包的技巧。

只需从Application.GetResourcePart()返回值获取流,并使用StreamReader使用ReadToEnd成员读取所有文本。

我只是想做同样的事情,最后使用Application.GetResourceStream。 下面是一个代码示例。 我使用了从流中创建字节数组的ReadFully方法。

 string imagePath = "pack://application:,,,/ReferencedAssembly;Component/Assets/Images/image.png"; StreamResourceInfo imageInfo = System.Windows.Application.GetResourceStream(new Uri(imagePath)); byte[] imageBytes = ReadFully(imageInfo.Stream);