背水一戰 Windows 10 (88) - 文件系統: 操作文件夾和文件
阿新 • • 發佈:2018-06-14
mvp rep lns dev div 行為 parent schema round
[源碼下載]
作者:webabcd
介紹
背水一戰 Windows 10 之 文件系統
- 創建文件夾,重命名文件夾,刪除文件夾,在指定的文件夾中創建文件
- 創建文件,復制文件,移動文件,重命名文件,刪除文件
- 打開文件,獲取指定的本地 uri 的文件,通過 StreamedFileDataRequest 或遠程 uri 創建文件或替換文件
示例
1、演示如何創建文件夾,重命名文件夾,刪除文件夾,在指定的文件夾中創建文件
FileSystem/FolderOperation.xaml
<Pagex:Class="Windows10.FileSystem.FolderOperation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Windows10.FileSystem" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="10 0 10 10"> <TextBlock Name="lblMsg" Margin="5" /> <Button Name="btnCreateFolder" Content="創建文件夾" Click="btnCreateFolder_Click" Margin="5" /> <Button Name="btnRenameFolder" Content="重命名文件夾" Click="btnRenameFolder_Click" Margin="5" /> <Button Name="btnDeleteFolder" Content="刪除文件夾" Click="btnDeleteFolder_Click" Margin="5" /> <Button Name="btnCreateFile" Content="在指定的文件夾中創建文件" Click="btnCreateFile_Click" Margin="5" /> </StackPanel> </Grid> </Page>
FileSystem/FolderOperation.xaml.cs
/* * 演示如何創建文件夾,重命名文件夾,刪除文件夾,在指定的文件夾中創建文件 * * StorageFolder - 文件夾操作類 * public IAsyncOperation<StorageFile> CreateFileAsync(string desiredName); * public IAsyncOperation<StorageFile> CreateFileAsync(string desiredName, CreationCollisionOption options); * public IAsyncOperation<StorageFolder> CreateFolderAsync(string desiredName); * public IAsyncOperation<StorageFolder> CreateFolderAsync(string desiredName, CreationCollisionOption options); * public IAsyncAction RenameAsync(string desiredName); * public IAsyncAction RenameAsync(string desiredName, NameCollisionOption option); * public IAsyncAction DeleteAsync(); * public IAsyncAction DeleteAsync(StorageDeleteOption option); * * CreationCollisionOption * GenerateUniqueName - 存在則在名稱後自動追加編號 * ReplaceExisting - 存在則替換 * FailIfExists - 存在則拋出異常 * OpenIfExists - 存在則返回現有項 * * NameCollisionOption * GenerateUniqueName - 存在則在名稱後自動追加編號 * ReplaceExisting - 存在則替換 * FailIfExists - 存在則拋出異常 * * StorageDeleteOption * Default - 默認行為 * PermanentDelete - 永久刪除(不會移至回收站) * * * 註:以上接口不再一一說明,看看下面的示例代碼就基本都明白了 */ using System; using Windows.Storage; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; namespace Windows10.FileSystem { public sealed partial class FolderOperation : Page { private StorageFolder _myFolder = null; public FolderOperation() { this.InitializeComponent(); } // 創建文件夾 private async void btnCreateFolder_Click(object sender, RoutedEventArgs e) { StorageFolder picturesFolder = await KnownFolders.GetFolderForUserAsync(null, KnownFolderId.PicturesLibrary); _myFolder = await picturesFolder.CreateFolderAsync("MyFolder", CreationCollisionOption.OpenIfExists); // 創建文件夾時也可以按照下面這種方式創建多級文件夾 // _myFolder = await picturesFolder.CreateFolderAsync(@"MyFolder\sub\subsub", CreationCollisionOption.OpenIfExists); lblMsg.Text = "創建了文件夾"; } // 重命名文件夾 private async void btnRenameFolder_Click(object sender, RoutedEventArgs e) { if (_myFolder != null) { await _myFolder.RenameAsync("MyFolder_Rename", NameCollisionOption.FailIfExists); lblMsg.Text = "重命名了文件夾"; } } // 刪除文件夾 private async void btnDeleteFolder_Click(object sender, RoutedEventArgs e) { if (_myFolder != null) { await _myFolder.DeleteAsync(StorageDeleteOption.Default); lblMsg.Text = "刪除了文件夾"; _myFolder = null; } } // 在指定的文件夾中創建文件 private async void btnCreateFile_Click(object sender, RoutedEventArgs e) { if (_myFolder != null) { StorageFile myFile = await _myFolder.CreateFileAsync("MyFile", CreationCollisionOption.OpenIfExists); // 創建文件時也可以按照下面這種方式指定子目錄(目錄不存在的話會自動創建) // StorageFile myFile = await _myFolder.CreateFileAsync(@"folder1\folder2\MyFile", CreationCollisionOption.OpenIfExists); lblMsg.Text = "在指定的文件夾中創建了文件"; } } } }
2、演示如何創建文件,復制文件,移動文件,重命名文件,刪除文件
FileSystem/FileOperation.xaml
<Page x:Class="Windows10.FileSystem.FileOperation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Windows10.FileSystem" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="10 0 10 10"> <TextBlock Name="lblMsg" Margin="5" /> <Button Name="btnCreateFolder" Content="創建文件夾" Click="btnCreateFolder_Click" Margin="5" /> <Button Name="btnCreateFile" Content="在指定的文件夾中創建文件" Click="btnCreateFile_Click" Margin="5" /> <Button Name="btnCopyFile" Content="復制文件" Click="btnCopyFile_Click" Margin="5" /> <Button Name="btnMoveFile" Content="移動文件" Click="btnMoveFile_Click" Margin="5" /> <Button Name="btnRenameFile" Content="重命名文件" Click="btnRenameFile_Click" Margin="5" /> <Button Name="btnDeleteFile" Content="刪除文件" Click="btnDeleteFile_Click" Margin="5" /> </StackPanel> </Grid> </Page>
FileSystem/FileOperation.xaml.cs
/* * 演示如何創建文件,復制文件,移動文件,重命名文件,刪除文件 * * StorageFile - 文件操作類 * public IAsyncOperation<StorageFile> CopyAsync(IStorageFolder destinationFolder); * public IAsyncOperation<StorageFile> CopyAsync(IStorageFolder destinationFolder, string desiredNewName); * public IAsyncOperation<StorageFile> CopyAsync(IStorageFolder destinationFolder, string desiredNewName, NameCollisionOption option); * public IAsyncAction CopyAndReplaceAsync(IStorageFile fileToReplace); * public IAsyncAction MoveAsync(IStorageFolder destinationFolder); * public IAsyncAction MoveAsync(IStorageFolder destinationFolder, string desiredNewName); * public IAsyncAction MoveAsync(IStorageFolder destinationFolder, string desiredNewName, NameCollisionOption option); * public IAsyncAction MoveAndReplaceAsync(IStorageFile fileToReplace); * public IAsyncAction RenameAsync(string desiredName); * public IAsyncAction RenameAsync(string desiredName, NameCollisionOption option); * public IAsyncAction DeleteAsync(); * public IAsyncAction DeleteAsync(StorageDeleteOption option); * * CreationCollisionOption * GenerateUniqueName - 存在則在名稱後自動追加編號 * ReplaceExisting - 存在則替換 * FailIfExists - 存在則拋出異常 * OpenIfExists - 存在則返回現有項 * * NameCollisionOption * GenerateUniqueName - 存在則在名稱後自動追加編號 * ReplaceExisting - 存在則替換 * FailIfExists - 存在則拋出異常 * * StorageDeleteOption * Default - 默認行為 * PermanentDelete - 永久刪除(不會移至回收站) * * * 註:以上接口不再一一說明,看看下面的示例代碼就基本都明白了 */ using System; using Windows.Storage; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; namespace Windows10.FileSystem { public sealed partial class FileOperation : Page { private StorageFolder _myFolder = null; public FileOperation() { this.InitializeComponent(); } // 創建文件夾 private async void btnCreateFolder_Click(object sender, RoutedEventArgs e) { StorageFolder picturesFolder = await KnownFolders.GetFolderForUserAsync(null, KnownFolderId.PicturesLibrary); _myFolder = await picturesFolder.CreateFolderAsync("MyFolder", CreationCollisionOption.OpenIfExists); // 創建文件夾時也可以按照下面這種方式創建多級文件夾 // _myFolder = await picturesFolder.CreateFolderAsync(@"MyFolder\sub\subsub", CreationCollisionOption.OpenIfExists); lblMsg.Text = "創建了文件夾"; } // 在指定的文件夾中創建文件 private async void btnCreateFile_Click(object sender, RoutedEventArgs e) { if (_myFolder != null) { StorageFile myFile = await _myFolder.CreateFileAsync("MyFile", CreationCollisionOption.OpenIfExists); // 創建文件時也可以按照下面這種方式指定子目錄(目錄不存在的話會自動創建) // StorageFile myFile = await _myFolder.CreateFileAsync(@"folder1\folder2\MyFile", CreationCollisionOption.OpenIfExists); lblMsg.Text = "在指定的文件夾中創建了文件"; } } // 復制文件 private async void btnCopyFile_Click(object sender, RoutedEventArgs e) { if (_myFolder != null) { try { StorageFile myFile = await _myFolder.GetFileAsync("MyFile"); StorageFile myFile_copy = await myFile.CopyAsync(_myFolder, "MyFile_Copy", NameCollisionOption.ReplaceExisting); lblMsg.Text = "復制了文件"; } catch (Exception ex) { lblMsg.Text = ex.ToString(); } } } // 移動文件 private async void btnMoveFile_Click(object sender, RoutedEventArgs e) { if (_myFolder != null) { try { StorageFile myFile = await _myFolder.GetFileAsync("MyFile"); await myFile.MoveAsync(_myFolder, "MyFile_Move", NameCollisionOption.ReplaceExisting); lblMsg.Text = "移動了文件"; } catch (Exception ex) { lblMsg.Text = ex.ToString(); } } } // 重命名文件 private async void btnRenameFile_Click(object sender, RoutedEventArgs e) { if (_myFolder != null) { try { StorageFile myFile = await _myFolder.GetFileAsync("MyFile_Move"); await myFile.RenameAsync("MyFile_Rename", NameCollisionOption.ReplaceExisting); lblMsg.Text = "重命名了文件"; } catch (Exception ex) { lblMsg.Text = ex.ToString(); } } } // 刪除文件 private async void btnDeleteFile_Click(object sender, RoutedEventArgs e) { if (_myFolder != null) { try { StorageFile myFile = await _myFolder.GetFileAsync("MyFile_Rename"); await myFile.DeleteAsync(StorageDeleteOption.Default); lblMsg.Text = "刪除了文件"; } catch (Exception ex) { lblMsg.Text = ex.ToString(); } } } } }
3、演示如何打開文件,獲取指定的本地 uri 的文件,通過 StreamedFileDataRequest 或遠程 uri 創建文件或替換文件
FileSystem/FileOperation2.xaml
<Page x:Class="Windows10.FileSystem.FileOperation2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Windows10.FileSystem" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="10 0 10 10"> <Image Name="image1" Width="50" Height="50" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5" /> <Image Name="image2" Width="50" Height="50" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5" /> <Image Name="image3" Width="50" Height="50" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5" /> <Image Name="image4" Width="50" Height="50" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5" /> <Image Name="image5" Width="50" Height="50" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5" /> <Button Name="btnGetFile" Content="獲取指定的本地 uri 的文件" Click="btnGetFile_Click" Margin="5" /> <Button Name="btnCreateFile1" Content="通過 StreamedFileDataRequest 創建文件" Click="btnCreateFile1_Click" Margin="5" /> <Button Name="btnCreateFile2" Content="通過遠程 uri 創建文件" Click="btnCreateFile2_Click" Margin="5" /> <Button Name="btnReplaceFile1" Content="通過 StreamedFileDataRequest 替換文件" Click="btnReplaceFile1_Click" Margin="5" /> <Button Name="btnReplaceFile2" Content="通過遠程 uri 替換文件" Click="btnReplaceFile2_Click" Margin="5" /> </StackPanel> </Grid> </Page>
FileSystem/FileOperation2.xaml.cs
/* * 演示如何打開文件,獲取指定的本地 uri 的文件,通過 StreamedFileDataRequest 或遠程 uri 創建文件或替換文件 * * StorageFile - 文件操作類 * public IAsyncOperation<IRandomAccessStream> OpenAsync(FileAccessMode accessMode) - 打開文件,可以指定是只讀方式還是讀寫方式,返回 IRandomAccessStream 流 * public IAsyncOperation<IRandomAccessStreamWithContentType> OpenReadAsync() - 以只讀方式打開文件,返回 IRandomAccessStreamWithContentType 流 * public IAsyncOperation<IInputStream> OpenSequentialReadAsync() - 以只讀方式打開文件,返回 IInputStream 流 * * public static IAsyncOperation<StorageFile> GetFileFromApplicationUriAsync(Uri uri); * public static IAsyncOperation<StorageFile> CreateStreamedFileAsync(string displayNameWithExtension, StreamedFileDataRequestedHandler dataRequested, IRandomAccessStreamReference thumbnail); * public static IAsyncOperation<StorageFile> ReplaceWithStreamedFileAsync(IStorageFile fileToReplace, StreamedFileDataRequestedHandler dataRequested, IRandomAccessStreamReference thumbnail); * public static IAsyncOperation<StorageFile> CreateStreamedFileFromUriAsync(string displayNameWithExtension, Uri uri, IRandomAccessStreamReference thumbnail); * public static IAsyncOperation<StorageFile> ReplaceWithStreamedFileFromUriAsync(IStorageFile fileToReplace, Uri uri, IRandomAccessStreamReference thumbnail); * * * 註:以上接口不再一一說明,看看下面的示例代碼就基本都明白了 */ using System; using Windows.Storage; using Windows.Storage.Streams; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media.Imaging; namespace Windows10.FileSystem { public sealed partial class FileOperation2 : Page { public FileOperation2() { this.InitializeComponent(); } private async void btnGetFile_Click(object sender, RoutedEventArgs e) { // 獲取指定的本地 uri 的文件 StorageFile storageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/hololens.jpg")); // 只讀方式打開文件,返回 IRandomAccessStream 流 IRandomAccessStream stream = await storageFile.OpenReadAsync(); BitmapImage bitmapImage = new BitmapImage(); bitmapImage.SetSource(stream); image1.Source = bitmapImage; } private async void btnCreateFile1_Click(object sender, RoutedEventArgs e) { // 通過 StreamedFileDataRequest 創建文件 StorageFile storageFile = await StorageFile.CreateStreamedFileAsync("GetFileFromApplicationUriAsync.jpg", StreamHandler, null); // 只讀方式打開文件,返回 IRandomAccessStream 流 IRandomAccessStream stream = await storageFile.OpenReadAsync(); BitmapImage bitmapImage = new BitmapImage(); bitmapImage.SetSource(stream); image2.Source = bitmapImage; } private async void btnCreateFile2_Click(object sender, RoutedEventArgs e) { Uri uri = new Uri("http://images.cnblogs.com/mvpteam.gif", UriKind.Absolute); // 通過遠程 uri 創建文件 StorageFile storageFile = await StorageFile.CreateStreamedFileFromUriAsync("CreateStreamedFileFromUriAsync.gif", uri, null); // 只讀方式打開文件,返回 IRandomAccessStream 流 IRandomAccessStream stream = await storageFile.OpenReadAsync(); BitmapImage bitmapImage = new BitmapImage(); bitmapImage.SetSource(stream); image3.Source = bitmapImage; } private async void btnReplaceFile1_Click(object sender, RoutedEventArgs e) { StorageFolder picturesFolder = await KnownFolders.GetFolderForUserAsync(null, KnownFolderId.PicturesLibrary); // 需要被替換的文件 StorageFile storageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(@"webabcdTest\GetFolderForUserAsync.jpg", CreationCollisionOption.ReplaceExisting); // 通過 StreamedFileDataRequest 替換指定的文件,然後通過返回的 newFile 對象操作替換後的文件 StorageFile newFile = await StorageFile.ReplaceWithStreamedFileAsync(storageFile, StreamHandler, null); // 只讀方式打開文件,返回 IRandomAccessStream 流 IRandomAccessStream stream = await newFile.OpenReadAsync(); BitmapImage bitmapImage = new BitmapImage(); bitmapImage.SetSource(stream); image4.Source = bitmapImage; } private async void btnReplaceFile2_Click(object sender, RoutedEventArgs e) { StorageFolder picturesFolder = await KnownFolders.GetFolderForUserAsync(null, KnownFolderId.PicturesLibrary); // 需要被替換的文件 StorageFile storageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(@"webabcdTest\CreateStreamedFileFromUriAsync.jpg", CreationCollisionOption.ReplaceExisting); Uri uri = new Uri("http://images.cnblogs.com/mvpteam.gif", UriKind.Absolute); // 通過遠程 uri 替換指定的文件,然後通過返回的 newFile 對象操作替換後的文件 StorageFile newFile = await StorageFile.ReplaceWithStreamedFileFromUriAsync(storageFile, uri, null); // 只讀方式打開文件,返回 IRandomAccessStream 流 IRandomAccessStream stream = await newFile.OpenReadAsync(); BitmapImage bitmapImage = new BitmapImage(); bitmapImage.SetSource(stream); image5.Source = bitmapImage; } // 一個 StreamedFileDataRequestedHandler private async void StreamHandler(StreamedFileDataRequest stream) { StorageFile storageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///assets/hololens.jpg")); IInputStream inputStream = await storageFile.OpenSequentialReadAsync(); await RandomAccessStream.CopyAndCloseAsync(inputStream, stream); } } }
OK
[源碼下載]
背水一戰 Windows 10 (88) - 文件系統: 操作文件夾和文件