Search code examples
c#openfiledialog

How to set path of the file to its root in OpenFileDialogBox C#


Ok let me explain it again My problem is I want to display the image. But i want this without the opendialogfile I tried this:

        pictureBox1.Image = Image.FromFile("C:\\Users\\Abdullah\\Documents\\Visual Studio 2013\\Projects\\Maker\\Maker\\add.png");// it works

But i dont want to do this because it will cause errors at deployment time. What i want to do is:

pictureBox1.Image= Image.FromFile("add.png");// because this picture is already in the project folder

In this case it show me error that file not found Now Hope so I explained it :)


Solution

  • Assuming that you are hard coding the path to your image and the image really exists in that path, then you should remember to escape the backslash when using a constant string like that one.

    Try with

     pictureBox1.ImageLocation = @"C:\Users\Abdullah\Documents\Visual Studio 2013
                                   \Projects\Maker\Maker\Resources\add.png";
    

    or

     pictureBox1.ImageLocation = "C:\\Users\\Abdullah\\Documents\\Visual Studio 2013
                                    \\Projects\\Maker\\Maker\\Resources\\add.png";
    

    (Warning strings splitted in two lines for readability. It should be on one single line)

    See How do I write a backslash?

    EDIT: Based on your comment below, then it seems that the Image folder always exists in your project (also when it will be deployed to a customer machine) then you could write something like this

     pictureBox1.ImageLocation = Path.Combine(Application.StartupPath, "Images", "add.png");
    

    or

    string imageFile = Path.Combine(Application.StartupPath, "Images", "add.png");
    pictureBox1.Image= Image.FromFile(imageFile);
    

    But looking back to your example: Is it Images or Resources?