Opening a file from a package URI in WPF

I am looking to open a CSV file from an application package to do some unit testing. So I really would like some analog File.ReadAllText(string path) , which instead is X.ReadAllText(Uri uri) . I could not find it yet.

Does anyone know if it is possible to read text / bytes (do not pay attention to it) from a file in a package without compiling this file to disk?

Oh and btw, File.ReadAllText(@"pack://application:,,,/SpreadSheetEngine/Tests/Example.csv") didn't work for me .. and I'm already doing the trick var app = new Application() to make sure that I have a package running during my unit test.

+4
source share
2 answers

Just get the stream from the return value of Application.GetResourcePart () and use StreamReader to read all the text using the ReadToEnd member.

+5
source

I just wanted to do the same and ended up using Application.GetResourceStream. The following is sample code. I used the ReadFully method from Creating a byte array from a stream .

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

Source: https://habr.com/ru/post/1305347/


All Articles