c# - UWP 从字符串写入(和读取)文件夹中的文件

标签 c# uwp

我正在开发仅适用于桌面的 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/

相关文章:

winapi - 使用 win32 在 UWP 中自定义驱动程序访问

c# - x 的设置值 :Uid programmatically

c# - ScrollViewer.ChangeView 处于事件状态时触摸屏幕会破坏应用程序 UWP

c++ - 当 C++ winrt 页面类与 XAML 页面一起使用时,不是成员错误

c# - 如何在 Unity 中根据混音器的音量设置 slider 的音量?

c# - ASP.NET MVC 3 : Blog Posts's model Tags Binding Best Practices

c# - 如何将字节数组转换为十六进制字符串,反之亦然?

wpf - UWP 网格中的内部项目不拉伸(stretch) XAML

c# - 如何覆盖 PropertyGrid 中 DockStyle 编辑器的行为

c# - 如何使用 GetType GetValue 区分两个对象的属性值?