Search code examples
c#imagefilesolution-explorer

How to use solution explorer files in c# application


I've developed a small application in c#. In this application i've added a text file named Data.txt and folder with about 20 images numbered from 1 - 20 in the solution explorer so that these are hidden from user and embedded in application. I've set these files properties to "None" and CopyToOutput "false" (also tried Property to "Content").

The problem is that when i debug my program on my Windows 8.1 laptop pc which contains my project and files, it works well but when i try to run the release files (also tried debug files) on my Win 7 Home Basic desktop pc, it stops working (means it doesn't load those files). Here is my code :

// Code to change images in picture box after small interval of time
private void timer1_Tick(object sender, EventArgs e)
    {
        try
        {
            if (Angle > 20)
            {
                Angle = 1;
            }

            picBackground.BackgroundImage.Dispose();
            picBackground.BackgroundImage = new Bitmap("../../" + Angle + ".png");
            Angle += 5;

        }
        catch
        { }
    }

// Here is constructor of the class
public RateFiles()
{
    try
    {
        string[] data = File.ReadAllLines("../../Data.txt");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message.ToString(), "Error");
    }

    // Object loads the strings
    obj.LoadData(data);
}

The Picture Box Background is required to change after 1 second but it is not working and File.ReadAllLines("../../Data.txt") is giving error "Could not find file 'C:\Users\Dell\Data.txt'".

How can I resolve this problem?


Solution

  • The problem is that you are trying to both access the files as if you were distributing the files outside of the assembly, and setting them not to copy which excludes them from the files to be distributed.

    To do what you want, you need to set the files up as resources, and then access them as such in your application. Try the following link to read up on how to create and access resources in C# with VS.

    https://msdn.microsoft.com/en-us/library/7k989cfy%28v=vs.90%29.aspx