Search code examples
c#xamarinstreamwriter

How do I specify a specific file in my Xamarin/VisualStudio Project using Environment.SpecialFolder?


I am trying to append text to a text file, but I can't find anywhere how to actually locate an existing file.

 partial void MainBtn_TouchUpInside(UIButton sender)
        {
            var Pp = ("Selected Form Of Exchange: Paypal");
            string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments(WHAT DO I WRITE IN THIS SPACE??);
            using (StreamWriter outputFile = new **StreamWriter(Path.Combine(mydocpath, "SelectPayment.txt")))

//I know that the file already exists, I just am trying different things.

            {

                outputFile.WriteLine(Pp);

}

So what do I do? Thanks Josh


Solution

  • you specify a file by combining a folder path and a file name to create a file path

    // this returns the path to a folder
    string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
    
    // combine that with a file name to create a file path
    string file = Path.Combine(path, "myFile.text");
    
    // append text to file
    File.AppendAllText(file, "this is the text I want to append);