標籤:win8 檔案
在操作檔案之前,先對應用的應用功能聲明進行設定。使用者通過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 提供對其中包含使用者內容的常見位置的訪問。 這包括使用者的本地庫(如照片、文檔、音樂或視頻)中的內容、可行動裝置、家庭組以及媒體伺服器裝置。對於訪問磁碟檔案來說,這隻局限於圖片,音樂,文檔和視頻四個檔案夾。
樣本1:將伺服器上的一個檔案下載到“圖片”檔案夾下的一個子檔案(需動態建立)中
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); }