Agasi Mkhitaryan
Agasi Mkhitaryan

Reputation: 180

How to use data/file in Datasources/folder to make program portable

Currently i have a program that uses a file (picture) from my PC. I found out i can load my picture into the program DataSources but this won't help me solving my problem. When I replace the program or transfer it to another PC and run, it crashes because of the second line:

InitializeComponent();
        Animation = new Bitmap(@"C:\Users\User\Documents\Visual Studio 2015\Projects\CourseWork\CourseWork\Properties\DataSources\Selection-Sort-Animation (1).gif");

I wonder how to set the path (@"") properly or do something else so my program can find and load a picture on any machine.

Upvotes: 1

Views: 55

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125197

You can put the image in a Resources.resx file.

  • It supports Localization
  • It is simple to maintain and use
  • It has a good design-time support and you can use resource items later in designer; for example set the back ground image of the form to a resource item using designer.

When you create a Windows Forms Application, if you look in solution explorer, you will see a Resources.resx file exists in the Properties folder. you can use this file (or add another .resx file) to embed resources in your application to do so:

Using Resource Designer:

  1. Open Resoures.resx designer.
  2. Click on drop down of Add resource Button in toolbar of designer.
  3. Click on Add Existing File ...
  4. Browse and select the file you want and click Open.
  5. The file will be added in Resources.resx with a name.
  6. Also you can see the file in Resources folder of your project. These files will not copy to output and are here only for you to have simple access to change them in future.

Accessing Resources:

You can access each resource simply with its name, for example:

this.PictureBox1.Image= Properties.Resources.Loading;

Upvotes: 1

Related Questions