Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to save text file in Xamarin Studio using C#

Thanks in advance
Posted
Updated 12-Oct-14 22:52pm
v2

1 solution

You write files in Xamarin pretty much as you would any file in C#.

You can use the namespace
Android.OS.Environment
to give you the system folders e.g. to get the path to the SD card you would use
Android.OS.Environment.ExternalStorageDirectory


Here is an example of saving an image to the SD card.

C#
private string SaveToSd()
        {
            var folder = Android.OS.Environment.ExternalStorageDirectory + Java.IO.File.Separator + "ImageFolder";
            var extFileName = folder +
                              Java.IO.File.Separator +
                              Guid.NewGuid() + ".png";

            try
            {
                if (!Directory.Exists(folder))
                    Directory.CreateDirectory(folder);
                using (var fs = new FileStream(extFileName, FileMode.OpenOrCreate))
                {
                    //TODO perform any validation / compression as required
                    return extFileName;
                }
            }
            catch
            {
                RunOnUiThread(() =>
                {
                    var builder = new AlertDialog.Builder(this);
                    builder.SetMessage("Saving image went wrong");
                    builder.SetTitle("Unable to save image");
                    builder.Show();
                });
                return "";
            }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900