需求:将文件拷贝到Hololens程序中并读取
测试结果:
1.本机(不设置任何权限):
1.AppContext.BaseDirectory=D:\SoftwareProjects\Hololens\Test\UWPForReadFile\UWPForReadFile\bin\x86\Debug\AppX\
2.可以读取BaseDirectory下的文件和目录列表,不能读取上一级目录的文件和目录列表。
3.可以读取BaseDirectory下的文件的内容。
4.可以通过在项目中添加文件的方式,将文件拷贝到程序所在目录。
a. 默认的 内容-不复制 就可以了。
b.无-不复制是不会拷贝的。
c.无-复制也是不会拷贝的。
5.不能创建目录。
6.不能创建文件,修改文件,删除文件。
2.设备中(不设置任何权限)
1.AppContext.BaseDirectory=C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\9428c709-abf7-4e8f-9857-9cddf79fc61eVS.Debug_x86.caiwanwei
9428c709-abf7-4e8f-9657-9cddf79fc61e是Package.appxmanifect中的打包中的包名。
caiwanwei是发布者显示名称
2.可以读取BaseDirectory下的文件和目录列表,可以读取C:\Data\Users\DefaultAccount\AppData\Local下的文件和目录列表,再往上就不能读取了。
3.可以读取到列表的,文件内容也能读取。
4.不能创建目录。
5.不能创建文件,修改文件,删除文件。
3. 文件读写
就是勾选了功能中的“可移动存储”也无法修改文件,前面的读写都是用System.IO中的类的,发现有要用hololens提供的读写类,StorageFolder,StorageFile。
另外有个ApplicationData,里面存储了一下文件位置:
LocalFolder:C:\Users\caiwanwei\AppData\Local\Packages\9428c709-abf7-4e8f-9657-9cddf79fc61e_w7gt0ng89zrh4\LocalState
RoamingFolder:C:\Users\caiwanwei\AppData\Local\Packages\9428c709-abf7-4e8f-9657-9cddf79fc61e_w7gt0ng89zrh4\RoamingState
TemporaryFolder:C:\Users\caiwanwei\AppData\Local\Packages\9428c709-abf7-4e8f-9657-9cddf79fc61e_w7gt0ng89zrh4\TempState
LocalCacheFolder:C:\Users\caiwanwei\AppData\Local\Packages\9428c709-abf7-4e8f-9657-9cddf79fc61e_w7gt0ng89zrh4\LocalCache
SharedLocalFolder:null
在hololen中的位置也差不多:
LocalFolder:C:\Data\Users\llhsw\AppData\Local\Packages\9428c709-abf7-4e8f-9657-9cddf79fc61e_w7gt0ng89zrh4\LocalState
RoamingFolder:C:\Data\Users\caiwanwei\AppData\Local\Packages\9428c709-abf7-4e8f-9657-9cddf79fc61e_w7gt0ng89zrh4\RoamingState
TemporaryFolder:C:\Data\Users\caiwanwei\AppData\Local\Packages\9428c709-abf7-4e8f-9657-9cddf79fc61e_w7gt0ng89zrh4\TempState
LocalCacheFolder:C:\Data\Users\caiwanwei\AppData\Local\Packages\9428c709-abf7-4e8f-9657-9cddf79fc61e_w7gt0ng89zrh4\LocalCache
多了一个Data文件夹SharedLocalFolder:null
用StorageFolder,StorageFile重新写了一下读写函数。
1.在电脑上
1.有“可移动存储”的功能的情况下,文件和文件夹都可以增删改查了。
2.在没有“可移动存储”的功能的情况下,文件和文件夹也都可以增删改查了。不对啊,奇怪???
最开始测试的时候发现一个现象:操作上可以创建文件夹,此时获取列表中有新文件夹,但是重新进入该文件夹,新增的文件夹不见了。后来也没有了,奇怪???
3.“自己的目录”上一级的目录返回的Storage是null。
4.用Storage读取原来的文件显示在TextBox后,变成都在右边显示了。用程序新增的文件,写入内容则不会这样。打断点内存中的字符串是正确的。这现象好奇怪????
2.在设备中
1.调到上级目录可以查看到C:\Data\Users\llhsw\AppData\Local下面的列表,往上就跳不了了。
2.可以在自己的5个Folder文件夹中修改文件和文件夹,但是不能再BaseDirectory那个文件夹中修改,无论有没有“可移动存储”的功能。
4. 文件/文件夹选择(FileOpenPicker,FolderPicker)
1.在电脑上,能弹出选择文件夹或者选择文件的界面
1.选中没有权限的目录是无法进去的。
2.没有权限的文件是无法读取的。
2.在设备上,FolderPicker无反应,FileOpenPicker会弹出一个界面说需要安装OneDriver程序,点击取消了。
5.添加到项目中的文件怎么修改?
既然在BaseDirectory中无法修改,复制到*Folder中修改就好了。复制文件夹没有实现,不过需要的话,将复制文件和创建文件夹封装一下或者去找一下有没有现成的API。
-----------------------------------------------------------------------------------------------------------------------------------------------
部署发现在VS中设计的1280*720的界面在hololens中只能显示一小部分。左右会缩放,主要是上下显示部分,把下面的截掉看不到了。
电脑中:
设备中:
测试一下发现设备中的分辨率是853,480。然后注意到实际上VS已经告诉我了。
那以后设置hololens上的uwp程序,大小就按853,480来设计了。
源代码
前台:
Dir1
File1
后台:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Storage;
using System.Threading.Tasks;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
// https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x804 上介绍了“空白页”项模板
namespace UWPForReadFile
{
///
/// 可用于自身或导航至 Frame 内部的空白页。
///
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void BtnTestDirPath_Click(object sender, RoutedEventArgs e)
{
TbResult.Text = string.Format("BaseDirectory:{0}\nLocalFolder:{1}\nRoamingFolder:{2}\nTemporaryFolder:{3}\nLocalCacheFolder:{4}\nSharedLocalFolder:{5}", AppContext.BaseDirectory, ApplicationData.Current.LocalFolder.GetPath(), ApplicationData.Current.RoamingFolder.GetPath(), ApplicationData.Current.TemporaryFolder.GetPath(), ApplicationData.Current.LocalCacheFolder.GetPath(), ApplicationData.Current.SharedLocalFolder.GetPath());
SetPath(AppContext.BaseDirectory);
}
private void SetPath(string path)
{
TbPath.Text = path;
GoToPath();
}
private void BtnCreateFile_Click(object sender, RoutedEventArgs e)
{
//try
//{
// string filePath = AppContext.BaseDirectory + "File1.txt";
// string content = TbResult.Text;
// File.WriteAllText(filePath, content);
//}
//catch (Exception ex)
//{
// TbException.Text = ex.ToString();
//}
//CreateFileDemo();
CreateStorageFile(TbPath.Text, TbNewFile.Text);
}
private async void CreateStorageFile(string dirPath,string newFileName)
{
try
{
// Get the app's temporary folder.
StorageFolder tempFolder = await StorageFolder.GetFolderFromPathAsync(dirPath);
StorageFile newFile = await tempFolder.CreateFileAsync(newFileName);
TbResult.Text = "创建文件成功";
GoToPath();
}
catch (Exception ex)
{
TbException.Text = ex.ToString();
}
}
private void BtnReadFile_Click(object sender, RoutedEventArgs e)
{
LoadSelectedFile();
}
private async void LoadStorageFile(string path)
{
StorageFile sf = await StorageFile.GetFileFromPathAsync(path);
using (IRandomAccessStream readStream = await sf.OpenAsync(FileAccessMode.Read))
{
using (DataReader dataReader = new DataReader(readStream))
{
UInt64 size = readStream.Size;
if (size <= UInt32.MaxValue)
{
try
{
UInt32 numBytesLoaded = await dataReader.LoadAsync((UInt32)size);
string fileContent = dataReader.ReadString(numBytesLoaded);
TbResult.Text = fileContent;
}
catch (Exception ex)
{
TbException.Text = ex.ToString();
}
}
}
}
}
private void LoadFile(string path)
{
try
{
string content = File.ReadAllText(path);
TbResult.Text = content;
}
catch (Exception ex)
{
TbException.Text = ex.ToString();
}
}
private void BtnSaveFile_Click(object sender, RoutedEventArgs e)
{
//SaveSelectedFile();
SaveSelectedStorageFile();
}
private async void SaveSelectedStorageFile()
{
try
{
StorageFile file = LbFileList.SelectedItem as StorageFile;
if (file == null) return;
string writingData = TbResult.Text;
using (StorageStreamTransaction transaction = await file.OpenTransactedWriteAsync())
{
using (DataWriter dataWriter = new DataWriter(transaction.Stream))
{
dataWriter.WriteString(writingData);
transaction.Stream.Size = await dataWriter.StoreAsync();
await transaction.CommitAsync();
}
}
}
catch (Exception ex)
{
TbException.Text = ex.ToString();
}
}
//private void SaveSelectedFile()
//{
// try
// {
// FileInfo file = LbFileList.SelectedItem as FileInfo;
// if (file == null) return;
// string filePath = file.FullName;
// string content = TbResult.Text;
// File.WriteAllText(filePath, content);
// //StorageFile storageFile=new StorageFile()
// }
// catch (Exception ex)
// {
// TbException.Text = ex.ToString();
// }
//}
private void BtnClear_Click(object sender, RoutedEventArgs e)
{
TbResult.Text = "";
TbException.Text = "";
}
private void BtnGetDirs_Click(object sender, RoutedEventArgs e)
{
GetDirsEx(TbPath.Text);
}
private async void GetFolders(string path)
{
try
{
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(path);
IReadOnlyList folders = await folder.GetFoldersAsync();
LbDirList.ItemsSource = folders;
LbDirList.DisplayMemberPath = "Name";
}
catch (Exception ex)
{
TbException.Text = ex.ToString();
}
}
private void GetDirsEx(string path)
{
GetFolders(path);
//GetDirs(path);
}
//private void GetDirs(string path)
//{
// try
// {
// DirectoryInfo dirInfo = new DirectoryInfo(path);
// DirectoryInfo[] dirs = dirInfo.GetDirectories();
// string txt = "";
// foreach (DirectoryInfo dir in dirs)
// {
// txt += dir.FullName + "\n";
// }
// //TbResult.Text = txt;
// LbDirList.ItemsSource = dirs;
// }
// catch (Exception ex)
// {
// TbException.Text = ex.ToString();
// }
//}
private void BtnGetFiles_Click(object sender, RoutedEventArgs e)
{
GetFilesEx(TbPath.Text);
}
private async void GetStorageFiles(string path)
{
try
{
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(path);
IReadOnlyList folders = await folder.GetFilesAsync();
LbFileList.ItemsSource = folders;
LbFileList.DisplayMemberPath = "Name";
}
catch (Exception ex)
{
TbException.Text = ex.ToString();
}
}
private void GetFilesEx(string path)
{
GetStorageFiles(path);
//GetFiles(path);
}
//private void GetFiles(string path)
//{
// try
// {
// DirectoryInfo dirInfo = new DirectoryInfo(path);
// FileInfo[] files = dirInfo.GetFiles();
// string txt = "";
// foreach (FileInfo file in files)
// {
// txt += file.FullName + "\n";
// }
// //TbResult.Text = txt;
// LbFileList.ItemsSource = files;
// }
// catch (Exception ex)
// {
// TbException.Text = ex.ToString();
// }
//}
private void BtnGoToParent_Click(object sender, RoutedEventArgs e)
{
//GotoParentDir();
GotoParentFolder();
}
private void BtnGoToParent2_Click(object sender, RoutedEventArgs e)
{
GotoParentDir();
}
private async void GotoParentFolder()
{
try
{
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(TbPath.Text);
StorageFolder pFolder = await folder.GetParentAsync();
if (pFolder != null)
{
TbPath.Text = pFolder.Path;
TbResult.Text = "";
}
else
{
TbResult.Text = "父目录为空";
}
GoToPath();
}
catch (Exception ex)
{
TbException.Text = ex.ToString();
}
}
private void GotoParentDir()
{
try
{
DirectoryInfo dirInfo = new DirectoryInfo(TbPath.Text);
TbPath.Text = dirInfo.Parent.FullName;
}
catch (Exception ex)
{
TbException.Text = ex.ToString();
}
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
SetPath(AppContext.BaseDirectory);
}
private void GoToPath_Click(object sender, RoutedEventArgs e)
{
TbResult.Text = "";
GoToPath();
}
private void GoToPath()
{
GoToPath(TbPath.Text);
}
private void GoToPath(string path)
{
if (string.IsNullOrEmpty(path)) return;
if (path == "null") return;
GetDirsEx(path);
GetFilesEx(path);
}
private void BtnCreateDir_Click(object sender, RoutedEventArgs e)
{
//try
//{
// string newDirPath = TbPath.Text + TbNewDir.Text;
// Directory.CreateDirectory(newDirPath);
// GetDirsEx(TbPath.Text);
//}
//catch (Exception ex)
//{
// TbException.Text = ex.ToString();
//}
CreateFolder();
}
private async void CreateFolder()
{
try
{
//string newDirPath = TbPath.Text + TbNewDir.Text;
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(TbPath.Text);
//Directory.CreateDirectory(newDirPath);
await folder.CreateFolderAsync(TbNewDir.Text);
GetDirsEx(TbPath.Text);
}
catch (Exception ex)
{
TbException.Text = ex.ToString();
}
}
private void LbDirList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//DirectoryInfo dirInfo = LbDirList.SelectedItem as DirectoryInfo;
//if (dirInfo != null)
//{
// TbPath.Text = dirInfo.FullName;
//}
StorageFolder folder = LbDirList.SelectedItem as StorageFolder;
if (folder != null)
{
TbPath.Text = folder.Path;
}
}
private void LbFileList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
LoadSelectedFile();
}
private void LoadSelectedFile()
{
//FileInfo file = LbFileList.SelectedItem as FileInfo;
//if (file != null)
//{
// LoadFile(file.FullName);
//}
StorageFile file = LbFileList.SelectedItem as StorageFile;
if (file != null)
{
LoadStorageFile(file.Path);
//LoadFile(file.Path);
}
}
private void BtnGetSize_Click(object sender, RoutedEventArgs e)
{
TbWidth.Text = this.ActualWidth.ToString();
TbHeight.Text = this.ActualHeight.ToString();
}
private void Page1_SizeChanged(object sender, SizeChangedEventArgs e)
{
TbWidth.Text = this.ActualWidth.ToString();
TbHeight.Text = this.ActualHeight.ToString();
}
private void BtnDeleteFile_Click(object sender, RoutedEventArgs e)
{
//RemoveSelectFile();
RemoveSelectStorageFile();
}
private async void RemoveSelectStorageFile()
{
try
{
StorageFile storageFile = LbFileList.SelectedItem as StorageFile;
if (storageFile != null)
{
await storageFile.DeleteAsync();
}
GoToPath();
}
catch (Exception ex)
{
TbException.Text = ex.ToString();
}
}
//private void RemoveSelectFile()
//{
// try
// {
// FileInfo file = LbFileList.SelectedItem as FileInfo;
// if (file != null)
// {
// file.Delete();
// }
// GoToPath();
// }
// catch (Exception ex)
// {
// TbException.Text = ex.ToString();
// }
//}
private void TbException_TextChanged(object sender, TextChangedEventArgs e)
{
if (string.IsNullOrEmpty(TbException.Text))
{
TbException.BorderBrush = new SolidColorBrush(Windows.UI.Colors.Black);
TbException.BorderThickness = new Thickness(0);
}
else
{
TbException.BorderBrush = new SolidColorBrush(Windows.UI.Colors.Red);
TbException.BorderThickness = new Thickness(1);
}
}
private void BtnLocalFolder_Click(object sender, RoutedEventArgs e)
{
SetPath(ApplicationData.Current.LocalFolder.GetPath());
}
private void BtnRoamingFolder_Click(object sender, RoutedEventArgs e)
{
SetPath(ApplicationData.Current.RoamingFolder.GetPath());
}
private void BtnTemporaryFolder_Click(object sender, RoutedEventArgs e)
{
SetPath(ApplicationData.Current.TemporaryFolder.GetPath());
}
private void BtnLocalCacheFolder_Click(object sender, RoutedEventArgs e)
{
SetPath(ApplicationData.Current.LocalCacheFolder.GetPath());
}
private void BtnSharedLocalFolder_Click(object sender, RoutedEventArgs e)
{
SetPath(ApplicationData.Current.SharedLocalFolder.GetPath());
}
private void BtnSelectDir_Click(object sender, RoutedEventArgs e)
{
SelectFolder();
}
private async void SelectFolder()
{
try
{
Windows.Storage.Pickers.FolderPicker folderPicker = new Windows.Storage.Pickers.FolderPicker();
folderPicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
folderPicker.FileTypeFilter.Add(".txt");
StorageFolder folder = await folderPicker.PickSingleFolderAsync();
if (folder != null)
{
//notify("选择" + folder.Name);
//folder_demonstration = folder;
TbPath.Text = folder.Path;
}
}
catch (Exception ex)
{
TbException.Text = ex.ToString();
}
}
private async void SelectFile()
{
try
{
Windows.Storage.Pickers.FileOpenPicker picker = new FileOpenPicker();
picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
picker.FileTypeFilter.Add(".txt");
picker.FileTypeFilter.Add(".xml");
StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
//notify("选择" + folder.Name);
//folder_demonstration = folder;
LoadFile(file.Path);
}
}
catch (Exception ex)
{
TbException.Text = ex.ToString();
}
}
private void BtnSelectFile_Click(object sender, RoutedEventArgs e)
{
SelectFile();
}
private void BtnDeleteDir_Click(object sender, RoutedEventArgs e)
{
}
private async void BtnCopyToLocalFolder_Click(object sender, RoutedEventArgs e)
{
try
{
StorageFile storageFile = LbFileList.SelectedItem as StorageFile;
if (storageFile != null)
{
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
// Specify a new name for the copied file.
string renamedFileName = storageFile.Name;
// Copy the file to the destination folder and rename it.
// Replace the existing file if the file already exists.
StorageFile copiedFile = await storageFile.CopyAsync(localFolder, renamedFileName, NameCollisionOption.ReplaceExisting);
}
TbResult.Text = "创建复制成功";
}
catch (Exception ex)
{
TbException.Text = ex.ToString();
}
//CreateFileDemo();
}
private async void CreateFileDemo()
{
try
{
// Get the app's temporary folder.
StorageFolder tempFolder = ApplicationData.Current.TemporaryFolder;
// Create a sample file in the temporary folder.
string newFileName = "test.txt";
StorageFile newFile = await tempFolder.CreateFileAsync(newFileName);
// Get the app's local folder to use as the destination folder.
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
// Specify a new name for the copied file.
string renamedFileName = "renamed_test.txt";
// Copy the file to the destination folder and rename it.
// Replace the existing file if the file already exists.
StorageFile copiedFile = await newFile.CopyAsync(localFolder, renamedFileName, NameCollisionOption.ReplaceExisting);
TbResult.Text = "创建文件成功";
}
catch (Exception ex)
{
TbException.Text = ex.ToString();
}
}
private void BtnCopyDirToLocalFolder_Click(object sender, RoutedEventArgs e)
{
try
{
StorageFolder folder = LbDirList.SelectedItem as StorageFolder;
if (folder != null)
{
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
// Specify a new name for the copied file.
string renamedName = folder.Name;
// Copy the file to the destination folder and rename it.
// Replace the existing file if the file already exists.
StorageFile copiedFile = await folder.cop
}
TbResult.Text = "创建复制成功";
}
catch (Exception ex)
{
TbException.Text = ex.ToString();
}
}
}
}
最后效果: