2016-10-19 82 views
0

我正在爲UWP開發僅適用於桌面的應用程序。用戶可以在設置頁面中選擇他想要創建一些文件和文件夾的位置。通常我用StreamWriter喜歡:UWP從字符串的(從)文件夾中寫入(並讀取)文件

using (System.IO.StreamWriter file = new System.IO.StreamWriter(path)) { 
    file.WriteLine("Something"); 
} 

我試圖用現在StorageFolder

StorageFolder folder = ApplicationData.Current.LocalFolder; 

,但我不能找到一種方法,從像C:\MyFolder字符串指定的文件夾。

預先感謝您。

+0

您是否希望從字符串中指定文件夾? – GeorgeChond

+0

@GeorgeChond是的。基本上,應用程序會創建一些用戶必須從特定文件夾上傳到銀行系統的文件。你還建議什麼?我知道我可以使用正常的Windows應用程序,但我想了解有關UWP的更多信息。 – Enrico

回答

1

不幸的是,目前不可能避免與用戶的交互。在我看來,這很奇怪,但就是這樣。

然後,當用戶選擇一個文件夾時,所有的應用程序都可以使用該文件夾。關於我們如何使用所有功能,文檔不是很清楚。在下面的代碼中,你可以找到一個例子來說明如何實現它。請,如果代碼錯誤或我可以改進它,告訴我。

/// <summary> 
/// Picks a folder. 
/// </summary> 
public async void PickFolder() { 
    var folderPicker = new Windows.Storage.Pickers.FolderPicker(); 
    folderPicker.SuggestedStartLocation = 
     Windows.Storage.Pickers.PickerLocationId.Desktop; 

    // unless you want to open a folder, FileTypeFilter is required 
    folderPicker.FileTypeFilter.Add(".cs"); 
    folderPicker.FileTypeFilter.Add(".jpg"); 

    Windows.Storage.StorageFolder folder = await folderPicker.PickSingleFolderAsync(); 
    if (folder != null) { 
     // Application now has read/write access to all contents in the picked folder 
     // (including other sub-folder contents) 
     Windows.Storage.AccessCache.StorageApplicationPermissions. 
      FutureAccessList.AddOrReplace("PickedFolderToken", folder); 

     StorageFolder mainFolder = await folder.CreateFolderAsync("Generator"); 
     await mainFolder.CreateFolderAsync("Code"); 
     await mainFolder.CreateFolderAsync("EventsArgs"); 

     StorageFolder newFolder = 
        await CreateFileInANewFolder(mainFolder, "MyFolder", "MyCode.cs", 
         new List<string>() { "My code line 1", "My code line 2" }); 
     List<string> fileLines = await ReadFile(newFolder, "MyCode.cs"); 

     StorageFile file = 
      await mainFolder.CreateFileAsync("Code.cs", 
              CreationCollisionOption.ReplaceExisting); 
     List<string> lines = new List<string>() { 
       "Hello world!", "This is a second line" 
     }; 

     await FileIO.WriteLinesAsync(file, lines); 
    } 
    else { 
     // the user didn't select any folder 
    } 
} 

另一個函數接收文件夾引用並創建文件夾和文件。

/// <summary> 
/// Creates the file in a new folder. 
/// </summary> 
/// <param name="folder">The folder.</param> 
/// <param name="newFolder">The new folder.</param> 
/// <param name="filename">The filename.</param> 
/// <param name="lineContent">Content of the line.</param> 
/// <returns>Task&lt;StorageFolder&gt;.</returns> 
public async Task<StorageFolder> CreateFileInANewFolder(
    StorageFolder folder, string newFolder, string filename, List<string> lineContent) { 
    StorageFolder myFolder = await folder.CreateFolderAsync(newFolder); 
    StorageFile file = await myFolder.CreateFileAsync(filename, 
         CreationCollisionOption.ReplaceExisting); 
    await FileIO.WriteLinesAsync(file, lineContent); 

    return myFolder; 
} 

該函數從文件夾中讀取文件。

/// <summary> 
/// Reads the file. 
/// </summary> 
/// <param name="folder">The folder.</param> 
/// <param name="filename">The filename.</param> 
/// <returns>Task&lt;List&lt;System.String&gt;&gt;.</returns> 
public async Task<List<string>> ReadFile(StorageFolder folder, string filename) { 
    StorageFile file = await folder.GetFileAsync(filename); 
    IList<string> lines = await FileIO.ReadLinesAsync(file); 
    return lines.ToList(); 
} 

爲此,您必須添加以下using

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using Windows.Storage; 
using Windows.UI.Xaml.Controls; 

當用戶選擇用PickFolder這個函數創建一個下面稱爲Generator並呼籲CodeEventsArgs,其他兩個文件夾下的文件夾中的文件夾。

然後它調用CreateFileInANewFolder。該功能的參數StorageFolder用於所選文件夾,此功能創建一個新文件夾並位於新文件下方。

ReadFile讀取用CreateFileInANewFolder創建的文件並返回行。

我希望這個例子可以幫助別人。

2

您可以讓用戶使用FolderPicker類指定他想要的文件夾。

var folderPicker = new Windows.Storage.Pickers.FolderPicker(); 
folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop; 
Windows.Storage.StorageFolder folder = await folderPicker.PickSingleFolderAsync(); 
if (folder != null) 
{ 
    // Application now has read/write access to all contents in the picked folder 
    // (including other sub-folder contents) 
    Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder); 

    //<<Here, you can write your files in the selected folder.>> 

} 
else 
{ 
    //Operation cancelled. 
} 

你可以找到關於如何使用它here更多信息。

相關問題