代码之家  ›  专栏  ›  技术社区  ›  Doug Null

uwp不会为fileio.writebufferasync在Microsoft示例中编译getBufferFromString

  •  0
  • Doug Null  · 技术社区  · 6 年前

    我正在尝试在 Microsoft example for FileIO.WriteBufferAsync 但是getBufferFromString不编译。

    最后,我想将字节缓冲区写入绝对文件路径。

    这是示例的副本…

    try
    {
        if (file != null)
        {
            IBuffer buffer = GetBufferFromString("Swift as a shadow");
            await FileIO.WriteBufferAsync(file, buffer);
            // Perform additional tasks after file is written
        }
    }
    // Handle errors with catch blocks
    catch (FileNotFoundException)
    {
        // For example, handle file not found
    }
    
    0 回复  |  直到 6 年前
        1
  •  0
  •   Nico Zhu    6 年前

    GetBufferFromString未编译。

    @陈雷蒙的评论非常有说服力。他是UWP官方代码样本的作者。为什么 GetBufferFromString 无法编译是因为您尚未声明它。

    private IBuffer GetBufferFromString(String str)
    {
        using (InMemoryRandomAccessStream memoryStream = new InMemoryRandomAccessStream())
        {
            using (DataWriter dataWriter = new DataWriter(memoryStream))
            {
                dataWriter.WriteString(str);
                return dataWriter.DetachBuffer();
            }
        }
    }
    

    我想写一个字节缓冲到一个绝对文件路径。

    对于将缓冲区写入绝对文件路径,可以使用 PathIO.WriteBufferAsync 方法请注意,您需要确保您的文件可以在uwp中访问。例如,如果文件存储在图片库中,则需要添加图片功能。更多详情请参考UWP file access permissions .