首页 技术 正文
技术 2022年11月10日
0 收藏 455 点赞 2,763 浏览 3643 个字

在操作文件之前,先相应用的应用功能声明进行设定。用户通过C#(非UI)对win8.1上的文件进行訪问,仅仅能局限于图片,音乐,视频和文档四个目录。

而通过文件选取器则能訪问到整个系统的文件。

(一)应用功能声明

对于win8应用商店应用,打开Package.appxmanifest文件。点击“功能”选项卡,勾选“音乐库”,“图片库”和“视频库”,这样你就能够通过代码对这里面的文件和目录进行操作:

音乐

musicLibrary 许可范围可提供对用户音乐的编程訪问能力,让应用无需用户交互就可以枚举和訪问库中的全部文件。此许可范围通经常使用在须要訪问整个音乐库的自己主动唱片点唱机应用中。

file picker 提供了一种强大的 UI 机制,让用户能够打开要通过某个应用处理的文件。

仅当应用须要进行编程訪问,而使用 file picker 无法实现编程訪问时。才应声明 musicLibrary 许可范围。

图片

picturesLibrary 许可范围可提供对用户图片的编程訪问能力,让应用无需用户交互就可以枚举和訪问库中的全部文件。

此许可范围通经常使用在须要訪问整个图片库的照片播放应用中。

file picker 提供了一种强大的 UI 机制,让用户能够打开要通过某个应用处理的文件。 仅当应用须要进行编程訪问,而使用 file picker 无法实现编程訪问时,才应声明 picturesLibrary 许可范围。

视频

videosLibrary 许可范围可提供对用户视频的编程訪问能力,让应用无需用户交互就可以枚举和訪问库中的全部文件。此许可范围通经常使用在须要訪问整个视频库的电影播放应用中。

file picker 提供了一种强大的 UI 机制。让用户能够打开要通过某个应用处理的文件。 仅当应用须要进行编程訪问,而使用 file picker 无法实现编程訪问时。才应声明 videosLibrary 许可范围。

对于“文档"目录的訪问,要依据VS提供的出错信息进行自己主动设置,之后要设置文件关联,依照提示做就可以。

(二)文件选取器

UI形式。可訪问整个系统上的文件。

使用文件选取器通过让用户选取文件和目录来訪问文件和目录。你能够使用 FileOpenPicker 类获取对文件的訪问,使用 FolderPicker 获取对目录的訪问。

通过文件选取器,你的应用能够在用户的整个系统上获得对文件和目录的訪问。当你调用文件选取器时。用户能够浏览其系统并选择文件(或目录)以訪问和保存。

在用户选取文件或目录之后,你的应用将这些选取作为 StorageFile 和 StorageFolder
对象进行接收。接着你的应用能够通过使用这些对象在选取的文件和目录上操作。

if (rootPage.EnsureUnsnapped())
{
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
openPicker.FileTypeFilter.Add(".jpg");
openPicker.FileTypeFilter.Add(".jpeg");
openPicker.FileTypeFilter.Add(".png"); StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
// Application now has read/write access to the picked file
OutputTextBlock.Text = "Picked photo: " + file.Name;
}
else
{
OutputTextBlock.Text = "Operation cancelled.";
}
}

(三)通过编程进行文件操作

类KnownFolders 提供对当中包括用户内容的常见位置的訪问。 这包括用户的本地库(如照片、文档、音乐或视频)中的内容、可移动设备、家庭组以及媒体server设备。对于訪问磁盘文件来说。这仅仅局限于图片,音乐,文档和视频四个目录。

演示样例1:将server上的一个文件下载到“图片”目录下的一个子文件(需动态创建)中

 string dync_IPv4 = "211.87.237.23";
string port = "8081";
string UrlJsonPath = "json/Images.txt";
//string UrlJsonPath = "Image/694021692/1214936171.png";
string uri = "http://" + dync_IPv4 + ":" + port + "/" + UrlJsonPath; System.Diagnostics.Debug.WriteLine(uri); string filename = "ImagesUri.txt";
//string filename = "DAXIA.png"; var rass = RandomAccessStreamReference.CreateFromUri(new Uri(uri));
IRandomAccessStream inputStream = await rass.OpenReadAsync();
Stream input = WindowsRuntimeStreamExtensions.AsStreamForRead(inputStream.GetInputStreamAt(0)); try
{
//获取图片扩展名的Guid
StorageFolder folder = KnownFolders.PicturesLibrary;
//System.Diagnostics.Debug.WriteLine(folder.Path);
//Creates a new file in the current folder, and specifies what to do if a file with the same name already exists in the current folder.
StorageFolder childFolder = await folder.CreateFolderAsync("WhereWeGo");
StorageFile outputFile = await childFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
//StorageFile outputFile = await folder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
System.Diagnostics.Debug.WriteLine(outputFile.Path);
using (IRandomAccessStream outputStream = await outputFile.OpenAsync(FileAccessMode.ReadWrite))
{
Stream output = WindowsRuntimeStreamExtensions.AsStreamForWrite(outputStream.GetOutputStreamAt(0));
await input.CopyToAsync(output);
output.Dispose();
input.Dispose();
}
}
catch (Exception)
{
System.Diagnostics.Debug.WriteLine("adfasd");
}

演示样例2:接演示样例1。将下载的文件读出来

  // 在指定的文件夹下获取指定的文件
StorageFolder storageFolder = KnownFolders.PicturesLibrary;
StorageFolder folder = await storageFolder.GetFolderAsync("WhereWeGo");
StorageFile storageFile = await folder.GetFileAsync("ImagesUri.txt");
// StorageFile storageFile = await storageFolder.GetFileAsync("ImagesUri.txt"); if (storageFile != null)
{
// 获取指定的文件里的文本内容
string textContent = await FileIO.ReadTextAsync(storageFile, Windows.Storage.Streams.UnicodeEncoding.Utf8);
System.Diagnostics.Debug.WriteLine(textContent);
}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297