如何使用StreamWriter写入文件

时间:2015-04-09 15:00:39

标签: c# windows-phone-8.1 win-universal-app

好的,所以继续这个问题,我需要将数据写入一个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))

错误消息显示:

  

最佳重载方法匹配&#39; System.IO.StreamWriter.StreamWriter(System.IO.Stream,System.Text.Encoding)&#39;有一些无效的论点

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

new StreamWriter(FILENAME) 

new StreamWriter(FILENAME,true) 

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

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

我现在有很多标签打开,所有人都说这是做这件事的方法,但它只是不起作用,我不明白为什么。

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

1 个答案:

答案 0 :(得分:0)

替换

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

new StreamWriter(await file.OpenStreamForWriteAsync())