Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

writetextasync doesnt write all text to a file

Tags:

c#

windows-8

So I want to create a simple text file with this code. But when I open the created file , the text is not displayed entirely, the only text I got is from tb1.text (id[2]).

Is there anything wrong here?

        private async void Save_Reg() 
        {
                var myfile = (tb3.Text+".xml");
                var folderUsed = ApplicationData.Current.LocalFolder;
                var folderOp = Windows.Storage.CreationCollisionOption.ReplaceExisting;
                var createFile = await folderUsed.CreateFileAsync(myfile, folderOp);


                var password = tb2.Text;
                var recov = tb1.Text;
                string[] id = { myfile,password, recov };
                await Windows.Storage.FileIO.WriteTextAsync(createFile, id[0]);
                await Windows.Storage.FileIO.WriteTextAsync(createFile, id[1]);
                await Windows.Storage.FileIO.WriteTextAsync(createFile, id[2]);
        }
like image 388
Andy Surya Avatar asked Feb 26 '26 02:02

Andy Surya


1 Answers

is there anything wrong here?

Yes. WriteTextAsync replaces the contents of the current file. It's equivalent to the synchronous File.WriteAllText method. If you want to append, you'll need AppendTextAsync.

Alternatively, concatenate the ids so you know the full contents you want to write, and then call WriteTextAsync just once.

like image 61
Jon Skeet Avatar answered Feb 27 '26 20:02

Jon Skeet