Checking for a resource file

I have an image file in the project folder in visual studio, and it is configured to create an action resource, so it is included in my exe file. I can link to this file in xaml without problems, for example <.Image Source = "images / myimage.png"> and it works.

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

+6
source share
3 answers

If you do not want it to be associated with the output folder additionally - you do not need to do anything. It is built into your exe, no need to check. It will always be true.


Ok, I understand, because you are dynamically creating the name of the embedded resource that you want to check.

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

They mostly check Assembly.GetExecutingAssembly().GetManifestResourceNames()

You can use this as a starting point. But keep in mind that the name of the resource is not images / myimage.png, but is built from your namespace, such as YourApp.images.myimage.png. You might want to take a look at the contents of the embedded resourceNames array from this answer.

+6
source

Have the Copy to Output property set to Always? and make sure you use the correct path. The path of your executable assembly can be found using the 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); } 

Greetings.

+2
source

Xamarin.Forms

From the working code, it is checked whether the auto-generated file name exists in the 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; 
0
source

Source: https://habr.com/ru/post/969458/


All Articles