3
votes

I'm using .NET Core 3 preview 6 and Visual Studio 2019 16.2 for creating WinForms application.

In .NET Framework I used type-safe mechanism to load resources, something like that:

this.pictureBox1.BackgroundImage = global::MyNamespace.Properties.Resources.Image1;
this.textBox1.Text = global::MyNamespace.Properties.Resources.Script1;

But in .NET Core 3 I must write special helper class with several methods:

public static class EmbeddedResource
{
    public static Image GetImage(String resourceName)
    {
        try
        {
            using (var stream = typeof(EmbeddedResource).GetTypeInfo().Assembly.GetManifestResourceStream(resourceName))
                return Image.FromStream(stream);
        }

        catch(Exception exception)
        {
            throw new Exception($"Failed to read Embedded Resource {resourceName}");
        }
    }

    public static String GetString(String resourceName)
    {
        try
        {
            using (var stream = typeof(EmbeddedResource).GetTypeInfo().Assembly.GetManifestResourceStream(resourceName))
            using (var reader = new StreamReader(stream, Encoding.UTF8))
                return reader.ReadToEnd();
        }

        catch(Exception exception)
        {
            throw new Exception($"Failed to read Embedded Resource {resourceName}");
        }
    }
}

And use it like that:

this.pictureBox1.BackgroundImage = EmbeddedResource.GetImage("MyNamespace.Image1.jpg");
this.textBox1.Text = EmbeddedResource.GetString("MyNamespace.Script1.sql");

Is there a better way (e. g. strictly-typed and resourceName error-safe) to do that?

Thank you in advance.

1

1 Answers

2
votes

Visual Studio 2019 16.2 Has design-time support for Resx file for Windows Forms .NET Core Projects. It is the same feature that is supported in previous versions of Visual Studio for Windows Forms classic .NET projects.

It means you can:

  1. Add New Item → Choose Resources File and set a name like Resources.Resx and press Add. The file will be opened in design mode. (Later to open it in design mode, just double click on it.)

  2. Add an image to the designer by dragging an image file and dropping it into the designer. You can also add the image by click on Add Resource tool strip drop down button and choosing Add Existing File ....

Then the image will be accessible using a property which has the same name as the image. For example I created a Properties folder and created Resources.Resx under that folder, then added MyImage.jpg to the resource file, the I could use it this way:

this.BackgroundImage = Properties.Resources.MyImage;

Note - Create default Resource file for the project in Properties folder

  1. Right click on Project → Choose Properties
  2. In project properties window, choose Resources (left side, bottom of the list).
  3. At the center, you will see a link This project does not contain a default resources file. Click here to create one. Click on the link and it will create a Resources.Resx file under Properties folder for your project.