locked
Accessing KnownFolders when called from another class RRS feed

  • Question

  • I'm investigating Hybrid JavaScript and C++ Sample from online samples for WinRT. Shortly, it's a JS front-end, which calls another C++ WinRT component.

    I wanted to create C# functionality, which would be the same as in C++ component in the sample. I created and C# winmd component and ported the C++ functionality.

     

    Basically, all interop is working fine, but I cannot open an image. When I access any KnownFolders or TemporaryFolder I get an access denied exception.

    Exception message:

    Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
    
    

    Exception stack trace:

       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
       at MyComponent.MyWorkingClass.<LoadFromImageFile>d__0.MoveNext()
    
    

     

    It happens for any access to any folder, for example:

    1 The snippet, taken from File Access Sample (not working in my code, working in File Access Sample):

                    StorageFolder storageFolderN = KnownFolders.DocumentsLibrary;
                    StorageFile sampleFileN = await storageFolderN.CreateFileAsync("sample.txt");
    
    
    2 My own code (not working too):
                    var dirrToCheck = await Windows.Storage.ApplicationData.Current.TemporaryFolder.GetFoldersAsync();
                    var c = dirrToCheck.Count;
                    var dirdir = dirrToCheck.FirstOrDefault();
    
    

    3 Even InstalledLocation throws the UnauthorizedAccessException:
                    StorageFolder storageFolder11 = Package.Current.InstalledLocationAsStorageFolder;
                    var some33 = await storageFolder11.GetFilesAsync();
                    int cc = some33.Count;
    

    All required capabilities are set in package.appxmanifest in JS project, which calls the C# winmd component.  C# File Access Sample, which is
    just one Application project, is still working fine. 
    What may be the problem? Should I create another package.appxmanifest for my C# component?

     


    • Edited by Anton K2 Tuesday, December 13, 2011 1:46 PM
    Tuesday, December 13, 2011 1:36 PM

Answers

  • The capabilities are on the package itself.  Assuming the C# winmd component is included in the same package as the JS project they should all be the same.

    The application cannot see all files in the Documents Library: if it doesn't handle .txt files then scenario #1 is expected behavior.  Does the same code to read from the TemporaryFolder and InstalledLocation work if called from a C# app with the same package settings?

    --Rob

    • Marked as answer by Anton K2 Friday, December 16, 2011 11:19 AM
    Wednesday, December 14, 2011 1:20 AM
    Moderator

All replies

  • The capabilities are on the package itself.  Assuming the C# winmd component is included in the same package as the JS project they should all be the same.

    The application cannot see all files in the Documents Library: if it doesn't handle .txt files then scenario #1 is expected behavior.  Does the same code to read from the TemporaryFolder and InstalledLocation work if called from a C# app with the same package settings?

    --Rob

    • Marked as answer by Anton K2 Friday, December 16, 2011 11:19 AM
    Wednesday, December 14, 2011 1:20 AM
    Moderator
  •  

    From the previous topic:

    Your code works for me.  Are you getting an exception or is everything running but nothing happens?  If you are getting an exception, what is it?

    My guess is that you're getting Access denied at the CreateFileAsync call.  Even with the Document Library Access capability the app can only access file types that it can handle.  Is your test app declared to handle .txt files?

    The capabilities are on the package itself.  Assuming the C# winmd component is included in the same package as the JS project they should all be the same.

    The application cannot see all files in the Documents Library: if it doesn't handle .txt files then scenario #1 is expected behavior.  Does the same code to read from the TemporaryFolder and InstalledLocation work if called from a C# app with the same package settings?

     

    Rob, thanks. I added .txt extension to the list of extensions and it started working.

    Now the package.appxmanifest contains the following sections:

     

     

      <Capabilities>
        <Capability Name="defaultWindowsCredentials" />
        <Capability Name="removableStorage" />
        <Capability Name="picturesLibrary" />
        <Capability Name="documentsLibrary" />
        <DeviceCapability Name="location" />
      </Capabilities>
    
    

    and 
     
      <Applications>
        <Application Id="App" StartPage="default.html">
        ...
         <Extensions>
            <Extension Category="windows.fileTypeAssociation" StartPage="default.html">
              <FileTypeAssociation Name="images">
                <SupportedFileTypes>
                  <FileType>.jpg</FileType>
                  <FileType>.jpeg</FileType>
                  <FileType>.png</FileType>
                  <FileType>.txt</FileType>
                </SupportedFileTypes>
              </FileTypeAssociation>
            </Extension>
          </Extensions>
        </Application>
      </Applications>
    
    

    Another thing, which was really new for me is that in async method, debugger doesn't work linearly and in fact I got the exception in  another place, then I realized.

     

    Are TemporaryFolder and InstalledLocation always accessible without extension specifications and capability indication?


    • Edited by Anton K2 Wednesday, December 14, 2011 11:58 AM
    Wednesday, December 14, 2011 6:32 AM