我正在开发仅适用于桌面的 UWP 应用程序。用户可以在设置页面中选择要创建一些文件和文件夹的位置。通常我使用 StreamWriter
像:
using (System.IO.StreamWriter file = new System.IO.StreamWriter(path)) {
file.WriteLine("Something");
}
我现在尝试使用 StorageFolder
StorageFolder folder = ApplicationData.Current.LocalFolder;
但我找不到从字符串中指定文件夹的方法,如 C:\MyFolder
。
提前谢谢你。
最佳答案
您可以让用户使用 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 .
关于c# - UWP 从字符串写入(和读取)文件夹中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40131220/