简体   繁体   中英

How do you use StreamWriter to write to a file

ok so heres the issue, i need to write data to a html file to save it, this format allows me to add anchors and links in which will be needed in many of the files

the names can not be predetermined (they will be files for roleplay characters, and places, 1 file per place or character)

heres the code i have so far

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>");
        }

    }

However, there is an issue with the following line:

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

The error message says:

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

I don't understand why all the examples I have seen use

new StreamWriter(FILENAME) 

or

new StreamWriter(FILENAME,true) 

for appending data to existing files.

These will be fresh on each run. FILENAME in this case should be <Windows Phone App's Local Storage>/Character/<NAMEOFCHARACTER>.html

I have so many tabs open right now that all say this is the way to do it, but it's just not working and I have no idea why.

NOTE: All of this takes place in the MainPage.xaml.cs in the Windows Phone project.

Replace

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

with

new StreamWriter(await file.OpenStreamForWriteAsync())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM