Parham Torabi

Parham Torabi

  • NA
  • 4
  • 1.4k

Android Write and Read Text File in C#

Dec 7 2020 6:27 AM
I want to write a code in C# that reads a text file in Android and if it doesn't exist, create it. I want my application to read the same file everytime I run my App and create that file the first time I open my app.I wrote this code but it doesn't work.It only creates a directory if it doesn't exist but it doesn't create my text file.Thank you for your answer in advance.
  1. string rootPath = Android.App.Application.Context.GetExternalFilesDir(null).ToString();  
  2. var filePathDir = Path.Combine(rootPath, "PhoenixWallet/");  
  3. if (!File.Exists(filePathDir)) {  
  4.   Directory.CreateDirectory(filePathDir);  
  5. }  
  6. bitcoinPrivateKeysFile = filePathDir + "bitcoinwallets.phoenixbtcwallet";  
  7. string bitcoinprivateKeyItem;  
  8. if (File.Exists(bitcoinPrivateKeysFile) != null) {  
  9.   StreamReader sr = File.OpenText(bitcoinPrivateKeysFile);  
  10.   while (!sr.EndOfStream) {  
  11.     bitcoinprivateKeyItem = sr.ReadLine();  
  12.     bitcoinprivKeys.Add(bitcoinprivateKeyItem);  
  13.   }  
  14.   Console.WriteLine("File Exists");  
  15.   sr.Close();  
  16. else {  
  17.   Key bitcoinKey = new Key();  
  18.   BitcoinSecret bitcoinsecret = new BitcoinSecret(bitcoinKey, network);  
  19.   StreamWriter sw;  
  20.   sw = File.CreateText(bitcoinPrivateKeysFile);  
  21.   sw.WriteLine(bitcoinsecret.PrivateKey);  
  22.   Console.WriteLine("File Does not Exist");  
  23.   sw.Close();  
  24. } 

Answers (1)