WPF C#如何检查URI文件是否存在

时间:2018-10-11 08:30:10

标签: c# wpf

我正在使用这样的代码。 当lan为null时,我收到异常 TypeInitializationException。

有没有办法检查URI是否为null?

ResourceDictionary dict = new ResourceDictionary();

dict.Source = new Uri("/Emdep.Geos.UI.Common;component/Resources/Language." + lan + ".xaml", UriKind.RelativeOrAbsolute);

    An unhandled exception of type 'System.TypeInitializationException' occurred in Emdep.Geos.UI.Common.dll## Heading ##

1 个答案:

答案 0 :(得分:0)

您应该做的是捕捉找不到FileNotFoundException时发生的ResourceDictionary

ResourceDictionary dict = new ResourceDictionary();
try
{
    dict.Source = new Uri("/Emdep.Geos.UI.Common;component/Resources/Language." + lan + ".xaml", UriKind.RelativeOrAbsolute);
}
catch (FileNotFoundException)
{
    //the resource dictionary could not be located/loaded...
}