UWP more powerful ability to obtain documents

Original: UWP more powerful ability to obtain documents

By default, the generic Windows platform (UWP) application can access a specific file system location. Applications can also access other location or by declaring the function via the file picker.

When you create a new application, by default, you can access the following file system location:

1. The application installation directory

Windows.Storage.StorageFolder installedLocation = Windows.ApplicationModel.Package.Current.InstalledLocation;

Or files directly specify the required

using Windows.Storage;            
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///file.txt"));

 

2. The location of the application data

 Use ApplicationData property to retrieve the application data folder.

using Windows.Storage;
StorageFolder localFolder = ApplicationData.Current.LocalFolder;

If you want to roam or temporary file access application folder, you can use RoamingFolder or TemporaryFolder property.

 

3. The user's "Downloads" folder

 You can create a file in the user's folder files downloaded

using Windows.Storage;
StorageFile newFile = await DownloadsFolder.CreateFileAsync("file.txt");

You can create subfolders in the user's "Downloads" folder

using Windows.Storage;
StorageFolder newFolder = await DownloadsFolder.CreateFolderAsync("New Folder");

 

4. Other folders

File DocumentsLibrary

Note: associate file types (application-specific files in the association statement can be accessed at this location type) must be added to the application list.

Case your application to use this feature:
- a valid ID OneDrive URL or resources to promote cross-platform access to specific OneDrive offline content
- offline file will open automatically saved to the user's OneDrive
KnownFolders.DocumentsLibrary
music MusicLibrary
See music, pictures and video library files and folders .
KnownFolders.MusicLibrary
image PicturesLibrary
See music, pictures and video library files and folders .
KnownFolders.PicturesLibrary
video VideosLibrary
See music, pictures and video library files and folders .
KnownFolders.VideosLibrary
Movable device RemovableDevices

must associate a file type (application-specific files in the association statement can be accessed at this location type) added to the application list.

See also access the SD card .
KnownFolders.Re

 

 

5. The ability to obtain a more powerful file

5. The ability to obtain a more powerful file

5. The ability to obtain a more powerful file

This function is almost subversive history before UWP, you can get all the documents except the C drive.

However, this privilege with the user application requires the user to manually confirm, you can.

 

Before the implementation of your powers, you need to guide users to your App permissions: privacy - a file system.

Using the code directly:

await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings:ms-settings:privacy-broadfilesystemaccess"));

 

This opens up such a screen.

 

 

 

Then what is your Package.appxmanifest configuration file. Open with a text viewer

Add the following code

Your file should exist inside the IgnorableNamespace, you just need to rescap can be added to the list.

复制代码
<Package
  ...
  xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
  IgnorableNamespaces="uap mp uap5 rescap">
...
<Capabilities>
    <rescap:Capability Name="broadFileSystemAccess" />
</Capabilities>
复制代码

 

然后开始写代码测试:

 

复制代码
        string dir = "D:\\";
        string fileName = "sample.txt";

        private async void Read_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
        {
            StorageFile file = await StorageFile.GetFileFromPathAsync(Path.Combine(dir, fileName));
            TextBox_Content.Text = await FileIO.ReadTextAsync(file);
        }

        private async void Write_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
        {
            StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(dir);
            StorageFile file = await folder.CreateFileAsync(fileName, CreationCollisionOption.OpenIfExists);
            await FileIO.WriteTextAsync(file, TextBox_Content.Text);
        }
复制代码

 

 

 

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12094852.html
UWP