Problem to write a text file with xamarin under visual studio

Hello everyone and happy birthday party. Sorry for the translation which is not terrible because I am French. Here is my problem I am currently studying computer science and I develop an application on Android Nougat 7.1, and my problem is that I can not write to a text file. here is the code I did:

   [Activity(Label = "Param")]
    public class Param : Activity
    {

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Param);

            //Bouton ok
            Button ok = FindViewById<Button>(Resource.Id.ok);
            //gestion du click
            ok.Click += delegate
            {

               EcritureFichier ("parametre.txt");
              EditText editText = FindViewById<EditText>(Resource.Id.saisieIP);

              var tbun = editText.Text; editText.TextChanged += delegate { };
                 Console.WriteLine(editText);

                Toast.MakeText(this, "Paramètre pris en compte!!", ToastLength.Short).Show();
            };
            //Bouton Annule
            Button annule = FindViewById<Button>(Resource.Id.annule);
            //gestion du click
            annule.Click += delegate
            {
                Toast.MakeText(this, "Annuler!!", ToastLength.Short).Show();
                Intent My_Intent = new Intent(this, typeof(MainActivity));
                StartActivity(My_Intent);
            };
    }


        public void EcritureFichier(string fichier  )
        { 
            try
            {       
                var backingFile = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "parametre.txt");
                var writer = File.CreateText(backingFile);
                {
                   writer.WriteLineAsync(fichier.ToString());                 
                    Console.WriteLine("tfgjyest ecriture fichier");
                    Console.WriteLine(System.Environment.SpecialFolder.Personal);
                    Console.WriteLine(System.Environment.SpecialFolder.Personal.ToString());
                    writer.Close();
                };

            }
            catch (Exception ex)
            {
                // Code exécuté en cas d'exception
                Console.Write("Une erreur est survenue lors de l’écriture du fichier : ");
                Console.Write(ex.Message);
            }            
        }           
    }
}

And here's what to display at the output of visual studio qaund I run the ok button in my application.

12-26 08:57:00.989 I/mono-stdout( 4713): tfgjyest ecriture fichier
12-26 08:57:00.990 I/mono-stdout( 4713): MyDocuments
12-26 08:57:00.991 I/mono-stdout( 4713): MyDocuments
12-26 08:57:01.022 I/mono-stdout( 4713): android.widget.EditText{c21c67 VFED..CL. .F...... 555,113-1080,257 #7f0b0070 app:id/saisieIP}tfgjyest ecriture fichier
MyDocuments
MyDocuments
android.widget.EditText{c21c67 VFED..CL. .F...... 555,113-1080,257 #7f0b0070 app:id/saisieIP}

12-26 08:57:01.652 D/EGL_emulation( 4713): eglMakeCurrent: 0xb1d057e0: ver 3 0 (tinfo 0x90972630)
12-26 08:57:01.683 D/EGL_emulation( 4713): eglMakeCurrent: 0xb1d057e0: ver 3 0 (tinfo 0x90972630)

hoping that you can help me because I strongly hope.
cordially

Sign In or Register to comment.