1

I am trying to write a routine to check if a file exists in a application package. After reading a lot on the subject it's obvious that MS forgot to put a FileExists function in the API (deliberate or not) but here is where I am at so far...

    public async Task<bool> CheckFile(string filePath)
    {
        bool found = false;
        try
        {
            Windows.ApplicationModel.Package package = Windows.ApplicationModel.Package.Current;
            Windows.Storage.StorageFolder installedLocation = package.InstalledLocation;
            StorageFile file = await installedLocation.GetFileAsync("Assets\\" + filePath); 
            found = true;
        }
        catch (System.IO.FileNotFoundException ex)
        {
            found = false;
        }
        return found;
    }

and then called from:

    private ImageSource _image = null;
    private String _imagePath = null;
    public ImageSource Image
    {
        get
        {
            if (this._image == null && this._imagePath != null)
            {
                Task<bool> fileExists = CheckFile(this._imagePath);
                bool filefound = fileExists.Result;

                string realPath = string.Empty;
                if (filefound)
                {
                    Windows.ApplicationModel.Package package = Windows.ApplicationModel.Package.Current;
                    Windows.Storage.StorageFolder installedLocation = package.InstalledLocation;
                    realPath = installedLocation + "Assets\\" + this._imagePath;
                }
                else
                {
                    realPath = "http://<<my url>>/images/" + this._imagePath;
                }
                this._image = new BitmapImage(new Uri(realPath));
            }
            return this._image;
        }

        set
        {
            this._imagePath = null;
            this.SetProperty(ref this._image, value);
        }
    }

SO basically it's a check to see if an image exists locally and if not then go get it from my website.

It all seems to work fine for the first image but then when it gets to "return this._image;" for the second image everything just freezes...

I'm just not sure what's going on here really..

Any help?

Cheers Dean

Dean Harry
  • 277
  • 1
  • 3
  • 13

1 Answers1

1

Checking for a file and then trying to open that file is a race condition. That is, the file can be removed between the check for existence and the open. So, you shouldn't do it that way. You're right to catch the exception from GetFileAsync from your GetFile, but you should catch a FileNotFoundException and then you know the file did not exist.

Your code for CheckFile does something funny, however. You have an internal try-catch block that will swallow up all exceptions, show a message box and then set found = true no matter what happened in the try block. I don't think that's what you intend. Also, the surrounding try-catch block is not necessary as it will only be hit if creating a new MessageDialog or ShowAsync throw an exception -- that is when you set found to false -- which is not what I think you want.

JP Alioto
  • 44,864
  • 6
  • 88
  • 112
  • Thanks JP, I see your point about CheckFile and have fixed that up except there is no FileNotFoundException in the store apps api that I can see. It still gets stuck though at the same place, when it tries to return the second image it just freezes up... – Dean Harry Aug 22 '12 at 05:32
  • FileNotFoundException is in System.IO, available for Metro style apps. Why would you append DataCommon._baseUri to the realPath which looks like the Uri you actually want. What's in DataCommon._baseUri? Also, do you have internet privilege declared? – JP Alioto Aug 22 '12 at 05:49
  • ah right, yes it is :) sorry, still getting my head around a few things... good point on the DataCommon, it's not needed and removed. Yes, internet privileges are declared. The internet part works fine and if I comment out all the local file stuff the images are returned from my web site as they should be. The problem is still with the local images, it still gets 'stuck' at the same place... – Dean Harry Aug 22 '12 at 06:17
  • @DeanHarry Can you update your question with the code changes you've made? – JP Alioto Aug 22 '12 at 15:53
  • @DeanHarry Looking better. Try implementing the ImageOpened and ImageFailed events to see if you can determine if your image is coming down properly from your website. http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.media.imaging.bitmapimage.aspx – JP Alioto Aug 23 '12 at 04:25
  • JP - The problem is not with the web site images, they work perfectly. They problem is with the local images, when it tries to load the second image it just locks up, I can't even Alt-F4 to end the app, it's completely frozen. – Dean Harry Aug 23 '12 at 07:20
  • The problem I believe is in the CheckFile function.. if I comment out the check and force filefound = true it loads all the images from local storage without an issue, so it's not a loading problem... – Dean Harry Aug 23 '12 at 07:44
  • Hi there, have u figured how it does work? @Dean Harry – A.S. Jan 29 '14 at 14:11