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

C# UWP从字符串写入(和读取)文件夹中的文件,c#,uwp,C#,Uwp,我正在开发一个只用于桌面的UWP应用程序。用户可以在设置页面中选择要创建一些文件和文件夹的位置。通常我使用的StreamWriter如下: using (System.IO.StreamWriter file = new System.IO.StreamWriter(path)) { file.WriteLine("Something"); } 我尝试使用nowStorageFolder StorageFolder folder = ApplicationData.Current.Lo

我正在开发一个只用于桌面的UWP应用程序。用户可以在设置页面中选择要创建一些文件和文件夹的位置。通常我使用的
StreamWriter
如下:

using (System.IO.StreamWriter file = new System.IO.StreamWriter(path)) {
    file.WriteLine("Something");
}
我尝试使用now
StorageFolder

StorageFolder folder = ApplicationData.Current.LocalFolder;
但是我找不到一种方法从字符串中指定文件夹,例如
C:\MyFolder


提前感谢。

您可以让用户使用该类指定他想要的文件夹

var folderPicker=new Windows.Storage.Pickers.folderPicker();
folderPicker.SuggestedStartLocation=Windows.Storage.Pickers.PickerLocationId.Desktop;
Windows.Storage.StorageFolder folder=等待folderPicker.PickSingleFolderAsync();
如果(文件夹!=null)
{
//应用程序现在具有对所选文件夹中所有内容的读/写访问权限
//(包括其他子文件夹内容)
Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.AddOrReplace(“PickedFolderToken”,文件夹);
//
}
其他的
{
//行动取消。
}

您可以找到有关如何使用它的更多信息。

您可以让用户指定他想要使用该类的文件夹

var folderPicker=new Windows.Storage.Pickers.folderPicker();
folderPicker.SuggestedStartLocation=Windows.Storage.Pickers.PickerLocationId.Desktop;
Windows.Storage.StorageFolder folder=等待folderPicker.PickSingleFolderAsync();
如果(文件夹!=null)
{
//应用程序现在具有对所选文件夹中所有内容的读/写访问权限
//(包括其他子文件夹内容)
Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.AddOrReplace(“PickedFolderToken”,文件夹);
//
}
其他的
{
//行动取消。
}

您可以找到有关如何使用它的更多信息。

不幸的是,目前无法避免与用户的交互。在我看来,这很奇怪,但仅此而已

然后,当用户选择了一个文件夹时,所有应用程序都可以处理该文件夹。文档中没有非常清楚地说明如何使用所有功能。在下面的代码中,您可以找到如何实现该功能的示例。如果代码错误或者我可以改进,请告诉我

/// <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
    }
}
当用户使用
PickFolder
选择文件夹时,此函数将在名为
Generator
的下面创建一个文件夹,并在名为
code
EventsArgs
的其他两个文件夹下创建一个文件夹

然后它调用新文件夹中的
createfileinnewfolder
。此函数将所选文件夹的
StorageFolder
作为参数,并在新文件下创建一个新文件夹

ReadFile
读取用
CreateFileInNewFolder
创建的文件并返回行


我希望这个例子能帮助一些人。

不幸的是,目前无法避免与用户的交互。在我看来,这很奇怪,但仅此而已

然后,当用户选择了一个文件夹时,所有应用程序都可以处理该文件夹。文档中没有非常清楚地说明如何使用所有功能。在下面的代码中,您可以找到如何实现该功能的示例。如果代码错误或者我可以改进,请告诉我

/// <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
    }
}
当用户使用
PickFolder
选择文件夹时,此函数将在名为
Generator
的下面创建一个文件夹,并在名为
code
EventsArgs
的其他两个文件夹下创建一个文件夹

然后它调用新文件夹中的
createfileinnewfolder
。此函数将所选文件夹的
StorageFolder
作为参数,并在新文件下创建一个新文件夹

ReadFile
读取用
CreateFileInNewFolder
创建的文件并返回行


我希望此示例可以帮助其他人。

是否希望从字符串中以独占方式指定文件夹?@geogechond Yes。基本上,应用程序创建一些文件,用户必须从特定文件夹上传到银行系统中。你还有什么建议?我知道我可以使用普通的Windows应用程序,但我想了解有关UWP的更多信息。是否希望通过字符串专门指定文件夹?@geogechond Yes。基本上,应用程序创建一些文件,用户必须从特定文件夹上传到银行系统中。你还有什么建议?我知道我可以使用普通的Windows应用程序,但我想了解更多关于UWP的信息。
/// <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 System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.UI.Xaml.Controls;