如何使用 StreamWriter 写入文件

How do you use StreamWriter to write to a file

好的,问题来了,我需要将数据写入 html 文件以保存它,这种格式允许我添加许多文件都需要的锚点和链接

无法预先确定名称(它们将是角色扮演角色和地点的文件,每个地点或角色 1 个文件)

这是我目前的代码

private async Task savetest(string name)
    {

        //ok so we get the local storage
        StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;

        //create the character folder
        var dataFolder = await local.CreateFolderAsync("Character",
            CreationCollisionOption.OpenIfExists);

        //create the character html
        var file = await dataFolder.CreateFileAsync(name+".html",
        CreationCollisionOption.ReplaceExisting);

        //heres where it will get interesting
        using (StreamWriter writer = new StreamWriter(local+"/"+dataFolder+"/"+file))
        { 
            writer.WriteLine("<html><head><title>Character File for " + z.charNameFirst + " " + z.charNameSecond + "</title></head><body>");
            writer.WriteLine("<html><head><title>Character File for " + z.charNameFirst + " " + z.charNameSecond + "</title></head><body>");
            writer.WriteLine("Name: " + z.charNameFirst + " " + z.charNameSecond + " <br>");
            writer.WriteLine("Gender:" + z.charGender+" <br>");
            writer.WriteLine("Race:" + z.charRace + " <br>");
            writer.WriteLine("Class:" + z.charClass + " <br>");
            writer.WriteLine("Gender:" + z.charGender + " <br>");
            writer.WriteLine("<a href=\"#SomeTest\">SomeTest</a><br>");
            writer.WriteLine("<a href=\"#AnotherTest\">AnotherTest</a><br>");
            for (int i = 20; i > 0; i--) 
            { 
                writer.WriteLine("some test text some test text some test text some test text some test text some test text "); 
            }

            writer.WriteLine("<span style=\"font-weight: bold;\"><a name=\"SomeTest\"></a>SomeTest</span><br>");

            for (int i = 20; i > 0; i--) 
            { 
                writer.WriteLine("some test text some test text some test text some test text some test text some test text "); 
            }
            writer.WriteLine("<span style=\"font-weight: bold;\"><a name=\"AnotherTest\"></a>AnotherTest</span><br>");

            for (int i = 20; i > 0; i--) 
            { 
              writer.WriteLine("some test text some test text some test text some test text some test text some test text "); 
            }

            writer.WriteLine("</body></html>");
        }

    }

但是,以下行存在问题:

using (StreamWriter writer = new StreamWriter(local+"/"+dataFolder+"/"+file))

错误消息说:

The best overloaded method match for 'System.IO.StreamWriter.StreamWriter(System.IO.Stream, System.Text.Encoding)' has some invalid arguments

我不明白为什么我看到的所有例子都使用

new StreamWriter(FILENAME) 

new StreamWriter(FILENAME,true) 

用于将数据附加到现有文件。

这些都是新鲜的 运行。 FILENAME 在这种情况下应该是 <Windows Phone App's Local Storage>/Character/<NAMEOFCHARACTER>.html

我现在打开了很多选项卡,所有人都说这是这样做的方法,但它就是不起作用,我不知道为什么。

注意:所有这些都发生在 Windows Phone 项目的 MainPage.xaml.cs 中。

替换

new StreamWriter(local+"/"+dataFolder+"/"+file)

new StreamWriter(await file.OpenStreamForWriteAsync())