wpf 检查资源文件是否存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23720619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:44:41  来源:igfitidea点击:

Checking if resource file exists

c#wpf

提问by user3595338

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.

我在 Visual Studio 的项目文件夹中有一个图像文件,它被设置为构建操作“资源”,因此它包含在我的 exe 文件中。

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

例如,我可以在 xaml 中链接到这个文件,没问题,<Image Source="images/myimage.png">而且它可以工作。

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?

但是,如果我尝试使用 File.exists("images/myimage.png") 检查文件是否存在,它总是返回 false。我在这里做错了什么?

采纳答案by ZoolWay

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.

如果您不想将它另外捆绑到输出文件夹中 - 您不必做任何事情。它内置于您的 exe 中,无需检查。永远是真的。



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

请参阅此处:WPF - 检查资源是否存在,无需结构化异常处理

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

他们基本上检查 Assembly.GetExecutingAssembly().GetManifestResourceNames()

You can use that as a starting point. But note that the resource name is not images/myimage.pngbut 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.

您可以将其用作起点。但请注意,资源名称不是images/myimage.png而是从您的命名空间构建的,例如YourApp.images.myimage.png。您可能想从该答案中查看构建的 resourceNames 数组的内容。

回答by Toan Nguyen

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.

干杯。

回答by Nick Kovalsky

Xamarin.Forms

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)

从工作代码中,检查共享项目的嵌入资源中是否存在自动生成的文件名(如此处所述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;