-2

I need to save a StringBuilder object to a file and be able to retrieve it later.

I have a simple StringBuilder object that I am trying to serialize in my .NET Maui app. But I am having trouble finding the correct FileHelper method that will save to a folder that is part of the .Net Maui file system.

How do I save "sb.ToString()" to a file?

I can read a file from the from MauiAsset file from Resources\Raw folder and write it to a file in the appDirectory, but I can't figure out how to create a stream and where to locate it using a collection of strings. (StringBuilder)

Here is my code:

string targetFileName = "Output1.txt";
        string path = FileSystem.AppDataDirectory;

        StringBuilder sb = new StringBuilder();
        sb.Append("This is line one");
        sb.Append("This is line two");

        //Write the strings to a file here ???

        //This line works for an existing file
        using var stream = await FileSystem.OpenAppPackageFileAsync(@"AboutAssets.txt");
        using var reader = new StreamReader(stream);
        var content = reader.ReadToEnd();

        // Write the file content to the app data directory
        string targetFile = System.IO.Path.Combine(FileSystem.Current.AppDataDirectory, targetFileName);

        using FileStream outputStream = System.IO.File.OpenWrite(targetFile);
        using StreamWriter streamWriter = new StreamWriter(outputStream);

        await streamWriter.WriteAsync(content);
Trey Balut
  • 1,355
  • 3
  • 19
  • 39
  • What do you mean when saying "_where to locate it using a collection of strings. (StringBuilder)_"? How exactly in your case would the desired file/stream location derived from the StringBuilder? Or, what would be such an example location? In your example, the StringBuilder just contains a concatenation of "_This is line one_" and "_This is line two_". That doesn't look like file names or locations to me. (Side note: A StringBuilder is not a collection of strings. StringBuilder is meant to build a string from multiple bits and pieces in a way that avoids creation of temp/ephemeral strings) –  Sep 04 '22 at 21:58
  • I mean by "where to locate", is it the AppDataDirectory for example, or some other location. – Trey Balut Sep 04 '22 at 22:27
  • 1
    Are you asking *us* where *you* want to write a file? – Blindy Sep 04 '22 at 23:15
  • I am not quite sure i understand. You already write Output1.txt to the AppDataDirectory (which seems to be the only directory you can write to from MAUI. Unless you only want to write some temp file in which case consider the app's CacheDirectory). You could just apply the same approach to the content of your StringBuilder, except writing _sb.String()_ instead of the variable _content_. –  Sep 04 '22 at 23:17

1 Answers1

0

I'm not quite sure what you are trying to do. Your code seems to work fine. But maybe this will help:

You can't modify an apps Bundled file. Meaning the AboutAssets.txt file you have cannot be edited. The idea is that you read the file, and then write it out your Output1.txt file.

You then have to use your newly created file as the file you edit and read from in the future.

So I would suggest you do a simple check to see if the Output1.txt file is created first. Then depending on that read/write from either the bundled file or your output one in the AppDataDirectory or Cache

CorrieJanse
  • 2,374
  • 1
  • 6
  • 23
  • The code show that I can read the file "AboutAssets.txt" and save it as another file "Output.txt". I just want to be able to do the same thing with the Stringbuilder output, "sb.toString()". that is create a stream/file of the strings and be able to save them as a file like I can do with the AboutAssets.txt file. – Trey Balut Sep 04 '22 at 22:30
  • Oh, `content = sb.ToString();` try that. – CorrieJanse Sep 05 '22 at 00:01