不幸的是,目前不可能避免與用戶的交互。在我看來,這很奇怪,但就是這樣。
然後,當用戶選擇一個文件夾時,所有的應用程序都可以使用該文件夾。關於我們如何使用所有功能,文檔不是很清楚。在下面的代碼中,你可以找到一個例子來說明如何實現它。請,如果代碼錯誤或我可以改進它,告訴我。
/// <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<StorageFolder>.</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<List<System.String>>.</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
並呼籲Code
和EventsArgs
,其他兩個文件夾下的文件夾中的文件夾。
然後它調用CreateFileInANewFolder
。該功能的參數StorageFolder
用於所選文件夾,此功能創建一個新文件夾並位於新文件下方。
ReadFile
讀取用CreateFileInANewFolder
創建的文件並返回行。
我希望這個例子可以幫助別人。
您是否希望從字符串中指定文件夾? – GeorgeChond
@GeorgeChond是的。基本上,應用程序會創建一些用戶必須從特定文件夾上傳到銀行系統的文件。你還建議什麼?我知道我可以使用正常的Windows應用程序,但我想了解有關UWP的更多信息。 – Enrico