Hololens開發(1) UWP程式拷貝讀取檔案
需求:將檔案拷貝到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來設計了。
原始碼
前臺:
<Page
x:Class="UWPForReadFile.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UWPForReadFile"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Loaded="Page_Loaded" Name="Page1" SizeChanged="Page1_SizeChanged">
<Page.Resources>
<Style TargetType="Button">
<Setter Property="Margin" Value="2"></Setter>
</Style>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Name="TopBar">
<StackPanel Orientation="Horizontal">
<Button Content="BaseDirectory" Name="BtnTestDirPath" Click="BtnTestDirPath_Click"></Button>
<Button Content="GetDirs" Name="BtnGetDirs" Click="BtnGetDirs_Click"></Button>
<Button Content="GetFiles" Name="BtnGetFiles" Click="BtnGetFiles_Click"></Button>
<Button Content="ClearText" Name="BtnClear" Click="BtnClear_Click"></Button>
<TextBlock Name="TbWidth" VerticalAlignment="Center" Text="0" Margin="5"></TextBlock>
<TextBlock Name="TbHeight" VerticalAlignment="Center" Text="0" Margin="5"></TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Button Content="LocalFolder" Name="BtnLocalFolder" Click="BtnLocalFolder_Click"/>
<Button Content="RoamingFolder" Name="BtnRoamingFolder" Click="BtnRoamingFolder_Click"/>
<Button Content="TemporaryFolder" Name="BtnTemporaryFolder" Click="BtnTemporaryFolder_Click"/>
<Button Content="LocalCacheFolder" Name="BtnLocalCacheFolder" Click="BtnLocalCacheFolder_Click"/>
<Button Content="SharedLocalFolder" Name="BtnSharedLocalFolder" Click="BtnSharedLocalFolder_Click"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Name:" VerticalAlignment="Center"></TextBlock>
<TextBox Name="TbNewDir" Text="NewDir" MinWidth="200"></TextBox>
<Button Content="Create" Name="BtnCreateDir" Click="BtnCreateDir_Click"></Button>
<Button Content="Select" Name="BtnSelectDir" Click="BtnSelectDir_Click"></Button>
<Button Content="Delete" Name="BtnDeleteDir" Click="BtnDeleteDir_Click"></Button>
<Button Content="CopyToLocalFolder" Name="BtnCopyDirToLocalFolder" Click="BtnCopyDirToLocalFolder_Click"></Button>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Name:" VerticalAlignment="Center"></TextBlock>
<TextBox Name="TbNewFile" Text="NewFile" MinWidth="200"></TextBox>
<Button Content="Create" Name="BtnCreateFile" Click="BtnCreateFile_Click"></Button>
<Button Content="Read" Name="BtnReadFile" Click="BtnReadFile_Click"></Button>
<Button Content="Save" Name="BtnSaveFile" Click="BtnSaveFile_Click"></Button>
<Button Content="Delete" Name="BtnDeleteFile" Click="BtnDeleteFile_Click"></Button>
<Button Content="Select" Name="BtnSelectFile" Click="BtnSelectFile_Click"></Button>
<Button Content="CopyToLocalFolder" Name="BtnCopyFileToLocalFolder" Click="BtnCopyToLocalFolder_Click"></Button>
</StackPanel>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition Width="auto"></ColumnDefinition>
<ColumnDefinition Width="auto"></ColumnDefinition>
<ColumnDefinition Width="auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Text="Path:" Grid.Column="0" VerticalAlignment="Center"></TextBlock>
<TextBox Name="TbPath" TextWrapping="Wrap" Grid.Column="1"></TextBox>
</Grid>
</StackPanel>
<SplitView IsPaneOpen="True"
DisplayMode="Inline"
OpenPaneLength="400" Grid.Row="1" PanePlacement="Left">
<SplitView.Pane>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<Button Name="BtnGotoPath" Content="Go" Click="GoToPath_Click" Grid.Column="2"></Button>
<Button Name="BtnGoToParent" Content="GoP" Click="BtnGoToParent_Click" Grid.Column="3"></Button>
<Button Name="BtnGoToParent2" Content="GoP2" Click="BtnGoToParent2_Click" Grid.Column="4"></Button>
</StackPanel>
<ListBox Grid.Row="1" Name="LbDirList" SelectionChanged="LbDirList_SelectionChanged" BorderBrush="Black" BorderThickness="1">
<ListBoxItem>Dir1</ListBoxItem>
</ListBox>
<ListBox Grid.Row="2" Name="LbFileList" SelectionChanged="LbFileList_SelectionChanged" BorderBrush="Black" BorderThickness="1">
<ListBoxItem>File1</ListBoxItem>
</ListBox>
</Grid>
</SplitView.Pane>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Text="Result:" Grid.Row="0"></TextBlock>
<TextBox Name="TbResult" TextWrapping="Wrap" MinHeight="100" Grid.Row="1" Text="1\nb"></TextBox>
<TextBlock Text="Exception:" Grid.Row="2"></TextBlock>
<TextBox Name="TbException" TextWrapping="Wrap" MinHeight="100" Grid.Row="3" TextChanged="TbException_TextChanged"></TextBox>
</Grid>
</SplitView>
</Grid>
</Page>
後臺:
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
{
/// <summary>
/// 可用於自身或導航至 Frame 內部的空白頁。
/// </summary>
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<StorageFolder> 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<StorageFile> 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();
}
}
}
}
最後效果: