Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

C# UWP Sample: Open File with GlobalFile Access BraodFileSystemAccess

19.06.2018 (👁15497)

Example Application UWP with free file access via Capability broadFileSystemAccess  

 

In May 2018, Microsoft finally expanded the development platform UWP Universal Windows Platform.

Starting with the 1803 version, the BroadFileSystemAccess settings can be used to access the files of the current user.

That is, the application can easily access files in the C path or data path without additional user interaction.

Previously, a manual file dialog or folder dialog had to be opened for approval.

 

Example application:

Copy a file to free directory and path specification

 

MainPage.xaml

In this example, the default page simply contains a button and any two text boxes with the path of a file

MainPage.xaml

<Page

    x:Class="test_file_open.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:test_file_open"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d"

    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

 

    <Grid>

        <Button x:Name="btnCopy" Content="Copy" HorizontalAlignment="Left" Height="99" Margin="94,140,0,0"

                VerticalAlignment="Top" Width="257" Click="btnCopy_Click"/>

        <TextBox x:Name="tbxFolder" HorizontalAlignment="Left" Height="84" Margin="88,276,0,0" Text="C:\_Daten\_temp\UWP_unknown" VerticalAlignment="Top" Width="603" FontSize="22"/>

        <TextBox x:Name="tbxFile" HorizontalAlignment="Left" Height="84" Margin="91,380,0,0" Text="Testfile.log" VerticalAlignment="Top" Width="603" FontSize="22"/>

 

    </Grid>

</Page>

 

 

 

MainPage.Xaml.cx

In C # code, the path is read from the text fields. From this the StorageFolder is created and from this the file is opened and copied as StorageFile.

Attention: for this purpose the setting with Capability Name = "broadFileSystemAccess is necessary. Because in this UWP app, the file is simply opened without additional user interaction.

So no manual file sharing dialog will be necessary!

        private async void  btnCopy_Click(object sender, RoutedEventArgs e)

        {

            StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(tbxFolder.Text);

            StorageFile file = await folder.GetFileAsync(tbxFile.Text);

            StorageFile copyFile = await file.CopyAsync(folder, "Copied_File.txt", NameCollisionOption.ReplaceExisting);

        }

 

package.appxmanifest

In the package.appxmanifest file you have to complete the Package.IgnoreableNamespaces and add the capabilities

 

<Package ..

         xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"

         IgnorableNamespaces="uap mp rescap">

..

<Capabilities>

    ..

    <rescap:Capability Name="broadFileSystemAccess" />

  </Capabilities>

 

 

 

<?xml version="1.0" encoding="utf-8"?>

<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"

         xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"

         xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"

         xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"

         IgnorableNamespaces="uap mp rescap">

  <Identity Name="c76c1dbf-c893-4f40-8f82-0934dfd31490" Publisher="CN=Raimund" Version="1.0.0.0" />

  <mp:PhoneIdentity PhoneProductId="c76c1dbf-c893-4f40-8f82-0934dfd31490" PhonePublisherId="00000000-0000-0000-0000-000000000000" />

  <Properties>

    <DisplayName>test_file_open</DisplayName>

    <PublisherDisplayName>Raimund</PublisherDisplayName>

    <Logo>Assets\StoreLogo.png</Logo>

  </Properties>

  <Dependencies>

    <TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.0.0" MaxVersionTested="10.0.0.0" />

  </Dependencies>

  <Resources>

    <Resource Language="x-generate" />

  </Resources>

  <Applications>

    <Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="test_file_open.App">

      <uap:VisualElements DisplayName="test_file_open" Square150x150Logo="Assets\Square150x150Logo.png" Square44x44Logo="Assets\Square44x44Logo.png" Description="test_file_open" BackgroundColor="transparent">

        <uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png">

        </uap:DefaultTile>

        <uap:SplashScreen Image="Assets\SplashScreen.png" />

      </uap:VisualElements>

    </Application>

  </Applications>

 

  <Capabilities>

    <Capability Name="internetClient" />

    <uap:Capability Name="picturesLibrary" />

    <rescap:Capability Name="broadFileSystemAccess" />

  </Capabilities>

</Package>

 

Project minimum version

Necessary for this is the version 1803 in the UWP settings

Project-> Properties-> Application

targeting:

Target: Universal Windows

Target Version: Windows 10, version 1803 (10.0; Build 17134)