2

在我的 Windows Phone 应用程序中,我有 .mp4 文件作为内容。如何检查我的应用程序中是否真的有它?

更新 例如在我的情况下我有来源:/res/9a8be1550f818223edcfc5ab6d765cec.mp4

if (File.Exists(localPath))
{
    myMediaElement.Source = new Uri(localPath, UriKind.Relative);
}

如果我以这种方式检查,则会出现异常:

4

2 回答 2

3
System.Windows.Resources.StreamResourceInfo sr;  
sr = Application.GetResourceStream(new Uri("images/MenuIcon.png", UriKind.Relative)); // path should NOT begin with a slash  
if (sr == null)  
{  
    // doesn't exist  
}  
else  
{  
    // exists  
} 
于 2012-07-16T11:51:07.663 回答
0

用户“revolutionkpi”的回答可能导致 System.IO.IOException,最好添加 try-catch 块:

System.Windows.Resources.StreamResourceInfo sr; 
try {
    sr = Application.GetResourceStream(new Uri("/images/MenuIcon.png",UriKind.Relative));
    if (sr == null)  {  
        // doesn't exist  
    }
    else{  
        // exists  
    } 
}
catch (Exception e) {
    // doesn't exist
}

Edit1:更漂亮的解决方案

var uri = new Uri("/images/MenuIcon.png", UriKind.Relative);
ImageSource image = null;

try
{
    if (Application.GetResourceStream(uri) != null)
    {
        image = new BitmapImage(uri); 
    } 
}
catch { }

if (image == null) {
    // doesn't exist  
}
于 2016-11-04T08:56:19.007 回答