0

i crated a Picturebox in windows form.This code below is working properly

PictureBox.ImageLocation=@"C:\Users\MyPc\Desktop\Project\Image.jpg

but When i run my code in another pc the image doesn't shown cause of its directory.I have to change the pc name.How can I do this in a more general way without chancing the name?

MrPala
  • 11
  • 1
    Use a relative path – DCAggie Dec 21 '21 at 22:24
  • https://stackoverflow.com/a/295694/3279876 – Sami Dec 21 '21 at 22:24
  • Does this answer your question? [Get path to execution directory of Windows Forms application](https://stackoverflow.com/questions/295687/get-path-to-execution-directory-of-windows-forms-application) – Sami Dec 21 '21 at 22:26
  • [relative path](https://networkencyclopedia.com/relative-path/#:~:text=Traditional%20DOS%20Path%20%20%20%20Path%20,directory%20t%20...%20%202%20more%20rows%20) – Steve Dec 21 '21 at 22:51
  • _.I have to change the pc name._ Meaning what? – TaW Dec 22 '21 at 14:35

1 Answers1

0

You sould use the following method to determine the user's desktop folder:

Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

and maybe the best way for having a correct path is also using System.IO.Path.Combine() method:

PictureBox.ImageLocation = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Project", "Image.jpg"));