Accessing a File Using a Path in UWP

DocTMDocTM Member

Hi guys,

I'm working with Xamarin Forms UWP (C#), and I'm using the Microsoft Cognitive Services Speech library.
One of the method, KeywordRecognitionModel.FromFile(string fileName), requires a string path to a file. I keep getting an error that the file is failed to open, which is probably because of some file permission issues.
That's the error:
An exception of type 'System.ApplicationException' occurred in System.Private.CoreLib.dll Exception with an error code: 0x8 (SPXERR_FILE_OPEN_FAILED)

Unfortunately, there's no other way to pass the file (aka as a stream or StorageFil etc), so I have to use some sort of path.
I already tried placing the file in different locations in the solution itself (the common project, the UWP project etc), as well in the directories that I know the UWP can access with given permission (Documents, Music folder etc).

There are very few samples that use this method, but non of them use a path to the file. I found 2 examples, one in Java which uses a stream (not applicable to c#), and the other c# project, doens't seem to set up an actual path (as if the project is a theoretical example rather than a working one).

At this point I'm out of ideas, maybe one of you guys come up with something I have't thought about.

Thank you!

Best Answer

  • DocTMDocTM
    Accepted Answer

    @TedRogers said:
    @DocTM I think you are going to have to put the file in your "sandbox".

    Can you put the file here? ApplicationData.Current.LocalFolder

    Ted

    We ended up doing something like that.
    We added the file to the project, changed its Build Action to 'Content', changed copy to output to 'Always Copy'. And in the code we treated it similarly to what you said:

    StorageFile sFile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(fileName);
    String path = sFile.Path;
    KeywordRecognitionModel Model = KeywordRecognitionModel.FromFile(path);
    

    @batmaci said:
    why you post your question under visual studio? you should post in xamarin forms. this forum part is visual studio related issues

    I didn't realized, my bad.

Answers

  • TedRogersTedRogers USMember ✭✭✭✭

    @DocTM I think you are going to have to put the file in your "sandbox".

    Can you put the file here? ApplicationData.Current.LocalFolder

    Ted

  • batmacibatmaci DEMember ✭✭✭✭✭

    why you post your question under visual studio? you should post in xamarin forms. this forum part is visual studio related issues

  • DocTMDocTM Member
    Accepted Answer

    @TedRogers said:
    @DocTM I think you are going to have to put the file in your "sandbox".

    Can you put the file here? ApplicationData.Current.LocalFolder

    Ted

    We ended up doing something like that.
    We added the file to the project, changed its Build Action to 'Content', changed copy to output to 'Always Copy'. And in the code we treated it similarly to what you said:

    StorageFile sFile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(fileName);
    String path = sFile.Path;
    KeywordRecognitionModel Model = KeywordRecognitionModel.FromFile(path);
    

    @batmaci said:
    why you post your question under visual studio? you should post in xamarin forms. this forum part is visual studio related issues

    I didn't realized, my bad.

Sign In or Register to comment.