8

I have an image file in my project's folder in visual studio and it is set to build action "resource" so it is included in my exe file.

I can link to this file in xaml no problem, for example <Image Source="images/myimage.png"> and it works.

But if I try to check the existence of the file, with File.exists("images/myimage.png") it always returns false. What am i doing wrong here?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
user3595338
  • 737
  • 3
  • 11
  • 26

3 Answers3

10

If you do not want to have it bundled to the output folder additionally - you do not have to do anything. It is build into your exe, not need to check. Would always be true.


Okay, I understand because you dynamically build the name of your embedded resource you want to check it.

See here: WPF - check resource exists without structured exception handling

They basically check against Assembly.GetExecutingAssembly().GetManifestResourceNames()

You can use that as a starting point. But note that the resource name is not images/myimage.png but constructed from your namespace like YourApp.images.myimage.png. You might like to take a look at the contents of the built resourceNames array from that answer.

Community
  • 1
  • 1
ZoolWay
  • 5,411
  • 6
  • 42
  • 76
  • Actually that is not the case here, I link the file with a variable so the file path looks like this: "images/" + filename + ".png", it will often happen that the corresponding file will not exist and I need to check for this so I can supply "images/default.png" path instead. – user3595338 May 18 '14 at 09:58
1

Xamarin.Forms

From a working code, checks if auto-generated filename exists in embedded resources in the shared project (as described here https://developer.xamarin.com/guides/xamarin-forms/user-interface/images/#Embedded_Images)

var assembly = typeof(App).GetTypeInfo().Assembly;
var AssemblyName = assembly.GetName().Name;
var generatedFilename = AssemblyName+".Images.flags.flag_" + item.CountryCode?.ToLower() + @".png";

bool found = false;
foreach (var res in assembly.GetManifestResourceNames())
{
    if (res == generatedFilename)
    {
        found = true;
        break;
    }
}

if (found) 
    UseGeneratedFilename();
else 
    UseSomeOtherPlaceholderImage;
Dennis van Opstal
  • 1,294
  • 1
  • 22
  • 44
Nick Kovalsky
  • 5,378
  • 2
  • 23
  • 50
  • Yeah, this might work for embedded resources, but won't do the trick for images inside the drawables in android nor the Resources folder in iOS. – Juansero29 Apr 10 '20 at 07:22
0

Have you set the "Copy to the Output" property to "Always"? And make sure that you use the correct path. The path of your executing assembly can be detected by using following code:

private string GetExecutingAssemblyPath()
{
    string codeBase = Assembly.GetExecutingAssembly().CodeBase;
    UriBuilder uri = new UriBuilder(codeBase);
    string path = Uri.UnescapeDataString(uri.Path);
    return Path.GetDirectoryName(path);
}

Cheers.

Dennis van Opstal
  • 1,294
  • 1
  • 22
  • 44
Toan Nguyen
  • 11,263
  • 5
  • 43
  • 59
  • 2
    Have you set "Copy to the Output" property to "Always"? No, I don't want to have the file separated in the directory, I want to have it included directly in my exe file and check if it exists in there. – user3595338 May 18 '14 at 09:53