Search code examples
c#wpfvisual-studio-2015windows-explorersolution-explorer

Adding image files in Windows Explorer vs. Solution Explorer


I have a piece of code that loops through the files (and folders) in the Images folder and display the thumbnails in a UniformGrid.

When I drag or add image files (and folders) to Visual Studio Solution Explorer then hit F5 to Start Debugging, all images are displayed.

But when I add the images to Windows Explorer and start debugging, the images are not displayed. I tried refreshing the Solution Explorer, Clean Solution, Rebuild Solution, and even restart Visual Studio, but neither do the images shown nor the image files listed in the Solution Explorer's Image folder. If I count the number of images added to Window Explorer using Directory.GetFiles("../../Images/", "*.jpg", SearchOption.TopDirectoryOnly);, the length of the files array does reflect the correct number of images in the folder.

Why is that when I add the images in Windows Explorer, neither the Solution Explorer nor the program display the images? And why Directory.GetFiles(...).Length does count the correct number of images files?

I have tried both running Visual Studio as and not as administrator.


Solution

  • When you run the program, it's not looking in the images folder for your project, it's looking in the output images folder (most likely beneath the "bin" directory). When you add images via solution explorer, they get added to the project and are (again, most likely) copied into the images folder in the output directory when you debug or run your application.

    On the other hand, when you add them just in Windows Explorer, the project doesn't know about them, so it doesn't copy those files. You need to "show all files" on the project, and right-click on the new images and add them to your project. Only then will they get copied to the output directory and be accessible to your application when you're debugging it.