Reputation: 365
I am struggling to find an easy way to load my test data in C#.
In Java, I load a resource using the following code:
...
public static InputStream loadResource(String resource) throws LoadException {
InputStream is = TestUtils.class.getResourceAsStream(resource);
if (is == null) {
throw new LoadException("Error loading '" + resource + "'");
}
return is;
}
...
public static void main(String[] args) {
InputStream is = TestUtils.loadResource("/resourcelocation");
}
I tried to use C# resource file, but I found awkward to load and manipulate it. Is there a simpler way to load resources in C#?
Upvotes: 2
Views: 1901
Reputation: 1503799
Yes - use Assembly.GetManifestResourceStream
, e.g.
typeof(TestClass).Assembly
.GetManifestResourceStream("test.namespace.Filename.txt")
Just make sure the files are tagged as "Embedded Resource" in the properties, so they get built into the assembly correctly.
Upvotes: 9