C
C#17mo ago
rotor_

❔ Loading static data without Visual Studio

I've got a number of static files (json, csv etc) that I need to make visible to my solution. On Visual Studio I'd add them as resources and the IDE would take care of everything for me. It doesn't look like that's an option with VSC, though. What's the idiomatic approach that I should use when developing using Visual Studio Code?
7 Replies
ero
ero17mo ago
add them manually to your csproj
<ItemGroup>
<Resource Include="../res/some-file.json" />
</ItemGroup>
<ItemGroup>
<Resource Include="../res/some-file.json" />
</ItemGroup>
rotor_
rotor_17mo ago
Yeah I saw that in the documentation but I wasn't overjoyed by that solution because it means I'm going to have to write codegen for my csproj After spending a while staring at this, I'm guessing this grabs the resource (which as already been encoded as a resx file using resgen) and then makes the resource accessible as a "class" under a generated namespace (which presumably intellisense sees?) within C#. And I'd have to duplicate the StronglyTypedClassName and EmbeddedResource elements for each resource that I want to load? Or would I simply be able to insert them all into that Update attribute on line 8? Must you load .resx files or can you load raw ones, too? json txt whatever As I understand a resx file just repackages one of those That's a shame. Means I'll have to write a different script to convert everything into a resx file every time my resources change Ah ok I saw hints of that being possible but I wasn't confident that it was Grand. So I can generate the resource resx file and everything else can stay out of it. That's very reasonable Out of interest, could you please explain what this expression actually does? $([System.String]::Concat('%(ManifestResourceName)', '').Remove($([System.String]::Concat('%(ManifestResourceName)', '') Ah hold up the expression .LastIndexOf('.') is inside the arguments for Remove Now I follow But what's with concatenating an empty string? That smells like a workaround for something behaving weirdly catsip strange Does the trick, though Thanks for the gist. I'll see if I can get it working with this solution and omnisharp and all that Yes it seems you're stripping everything after the last dot (inclusive) To crop out the class name so you can set the namespace
MarkPflug
MarkPflug17mo ago
You could try this package that I've built, might do what you need. You can add a .resj (like resx but json), and it will embed resources and generate code accessors just like a resxThe code-gen is msbuild based, so it should work in VS Code, but you might not get intellisense from omnisharp. https://github.com/MarkPflug/Sylvan.BuildTools.Resources
GitHub
GitHub - MarkPflug/Sylvan.BuildTools.Resources: Build-time resource...
Build-time resource file support in C# projects. Contribute to MarkPflug/Sylvan.BuildTools.Resources development by creating an account on GitHub.
MarkPflug
MarkPflug17mo ago
I'm aware. Alas, you can't add embedded resources through roslyn source generators, so it isn't possible to reproduce this. Also, source generators are ideal when you need to interrogate the source code of the project, which my code gen doesn't need to do.
MarkPflug
MarkPflug17mo ago
GitHub
Source Generators: Allow embedding of files other than source · Iss...
We&#39;ve seen from several customers the want to embed other files into the output assembly, particularly EmbeddedResources (see #49923) We should explore an API that allows generators to add ...
rotor_
rotor_17mo ago
This is a very useful resource thank you! Although I've found an issue which I've isolated here https://github.com/MarkPflug/Sylvan.BuildTools.Resources/issues/19 It's quite easy to work around, though
GitHub
Build failure: static resource folder edgecase · Issue #19 · MarkPf...
Build fails with the following project layout: my-project.csproj ... &lt;ItemGroup&gt; &lt;StaticResourceFolder Include=&quot;data/Example&quot;/&gt; &lt;/ItemGroup&...
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.