Pregunta

I'm trying to save a string of user inputs to a file in a windows runtime app. However I'm getting the error System.UnauthorizedAccessException. How do I gain access to this Library?

    static private async Task WriteDataToFileAsync(string fileName, string content)
    {
        byte[] data = Encoding.Unicode.GetBytes(content);

        var folder = KnownFolders.DocumentsLibrary;

        var file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);


        using (var s = await file.OpenStreamForWriteAsync())
        {
            await s.WriteAsync(data, 0, data.Length);
        }
    }
¿Fue útil?

Solución

"Documents library" capability in Visual Studio 2013 has been removed since it is only available for Windows Store Company accounts. Without this capability you will get "Access is denied".

For more information, read here: http://lunarfrog.com/blog/2013/07/05/documents-library-capability-winrt/

Otros consejos

Like this, notice under 'Extensions' I specify the file type I want to access (.txt) and then under 'Capabilities' I have added 'documentsLibrary'.

Then to actually write or access a file from there, something like this.

 var file = await KnownFolders.DocumentsLibrary.CreateFileAsync("myFile.txt", CreationCollisionOption.ReplaceExisting);
                        await FileIO.WriteTextAsync(file, data);

<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://schemas.microsoft.com/appx/2010/manifest" xmlns:m2="http://schemas.microsoft.com/appx/2013/manifest">
  <Identity Name="testapp" Publisher="CN=test.test" Version="1.5.0.3" />
  <Properties>
    <DisplayName>test.MetroApp</DisplayName>
    <PublisherDisplayName>test.test</PublisherDisplayName>
    <Logo>Assets\StoreLogo.png</Logo>
  </Properties>
  <Prerequisites>
    <OSMinVersion>6.3.0</OSMinVersion>
    <OSMaxVersionTested>6.3.0</OSMaxVersionTested>
  </Prerequisites>
  <Resources>
    <Resource Language="x-generate" />
  </Resources>
  <Applications>
    <Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="test.MetroApp.App">
      <m2:VisualElements DisplayName="test.MetroApp" Square150x150Logo="Assets\test.png" Square30x30Logo="Assets\SmallLogo.png" Description="test.MetroApp" ForegroundText="light" BackgroundColor="#464646">
        <m2:SplashScreen Image="Assets\test.scale-620.png" BackgroundColor="#464646" />
        <m2:InitialRotationPreference>
          <m2:Rotation Preference="landscape" />
        </m2:InitialRotationPreference>
      </m2:VisualElements>
      <Extensions>
        <Extension Category="windows.fileTypeAssociation">
          <FileTypeAssociation Name="1">
            <DisplayName>AccessTXT</DisplayName>
            <SupportedFileTypes>
              <FileType>.txt</FileType>
            </SupportedFileTypes>
          </FileTypeAssociation>
        </Extension>
      </Extensions>
    </Application>
  </Applications>
  <Capabilities>
    <Capability Name="internetClient" />
    <Capability Name="privateNetworkClientServer" />
    <Capability Name="musicLibrary" />
    <Capability Name="documentsLibrary" />
  </Capabilities>
</Package>

According to the docs "your app must use the File Type Association declaration in the app manifest file to explicitly declare what file types (extensions) will be accessed or created in the Documents library".

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top