none
Visaul Studio Resources RRS feed

  • Question

  • Hi, I am having a problem with the resources section within the visual studio IDE.
    The problem is that I am creating a folder, either within or outside the resource folder so I am able to organise files in that maner.
    Say I import a load of .wav file, and feel the need to put them into a folder called WavSounds, the connection breaks and I get a red exclamation symbol.

    How am I to prevent this or am I just going to have to leave them in my resource folder unorganised?

    PS

    Which will be a pain because I planning on having many different types of files in there.
    Sunday, May 18, 2014 1:54 AM

Answers

  • Hello,

    The following example is for images which can be adapted for different types of files, I am using images simply because I have no sound files.

    1. Create a folder under the project names Images
    2. Create a folder under Images named Cars
    3. Using Solution Explorer select the Cars folder
    4. Add images, when pressing the add button select the down arrow, add as link.
    5. Select properties for the image added, set Build Action = Embedded Resource then set Copy to Output Directory to Copy if Newer
    6. Place a ListBox on the form, add image names as shown below
    7. Add a PictureBox
    8. Add a Button, add the code shown below.

    Load images into ListBox

    private void Form1_Load(object sender, EventArgs e)
    {
        string[] filePaths = System.IO.Directory.GetFiles(
            System.IO.Path.Combine(
            AppDomain.CurrentDomain.BaseDirectory,"Images\\Cars") );
    
        foreach (string file in filePaths)
        {
            listBox1.Items.Add(System.IO.Path.GetFileName(file));
        }
        listBox1.SelectedIndex = 0;
    }

    Load selected image where name is taken from the ListBox

    private void button2_Click(object sender, EventArgs e)
    {
        Assembly theAssembly = this.GetType().Assembly;
        using (Stream stream = theAssembly.GetManifestResourceStream(theAssembly.GetName().Name + "." + "Images" + ".Cars." + listBox1.Text))
        {
            pictureBox1.Image = Image.FromStream(stream);
        }
    }



    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.



    Sunday, May 18, 2014 1:00 PM
    Moderator
  • Hello,

    You select the "All Files" option.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Monday, May 19, 2014 11:58 AM
    Moderator

All replies

  • Hello,

    The following example is for images which can be adapted for different types of files, I am using images simply because I have no sound files.

    1. Create a folder under the project names Images
    2. Create a folder under Images named Cars
    3. Using Solution Explorer select the Cars folder
    4. Add images, when pressing the add button select the down arrow, add as link.
    5. Select properties for the image added, set Build Action = Embedded Resource then set Copy to Output Directory to Copy if Newer
    6. Place a ListBox on the form, add image names as shown below
    7. Add a PictureBox
    8. Add a Button, add the code shown below.

    Load images into ListBox

    private void Form1_Load(object sender, EventArgs e)
    {
        string[] filePaths = System.IO.Directory.GetFiles(
            System.IO.Path.Combine(
            AppDomain.CurrentDomain.BaseDirectory,"Images\\Cars") );
    
        foreach (string file in filePaths)
        {
            listBox1.Items.Add(System.IO.Path.GetFileName(file));
        }
        listBox1.SelectedIndex = 0;
    }

    Load selected image where name is taken from the ListBox

    private void button2_Click(object sender, EventArgs e)
    {
        Assembly theAssembly = this.GetType().Assembly;
        using (Stream stream = theAssembly.GetManifestResourceStream(theAssembly.GetName().Name + "." + "Images" + ".Cars." + listBox1.Text))
        {
            pictureBox1.Image = Image.FromStream(stream);
        }
    }



    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.



    Sunday, May 18, 2014 1:00 PM
    Moderator
  • Hi, thanks for the tutorial but I can't seem to do this with sound files. Theres add bitmap but no sound.
    Monday, May 19, 2014 1:32 AM
  • Hello,

    You select the "All Files" option.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Monday, May 19, 2014 11:58 AM
    Moderator