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.
- string rootPath = Android.App.Application.Context.GetExternalFilesDir(null).ToString();
- var filePathDir = Path.Combine(rootPath, "PhoenixWallet/");
- if (!File.Exists(filePathDir)) {
- Directory.CreateDirectory(filePathDir);
- }
- bitcoinPrivateKeysFile = filePathDir + "bitcoinwallets.phoenixbtcwallet";
- string bitcoinprivateKeyItem;
- if (File.Exists(bitcoinPrivateKeysFile) != null) {
- StreamReader sr = File.OpenText(bitcoinPrivateKeysFile);
- while (!sr.EndOfStream) {
- bitcoinprivateKeyItem = sr.ReadLine();
- bitcoinprivKeys.Add(bitcoinprivateKeyItem);
- }
- Console.WriteLine("File Exists");
- sr.Close();
- } else {
- Key bitcoinKey = new Key();
- BitcoinSecret bitcoinsecret = new BitcoinSecret(bitcoinKey, network);
- StreamWriter sw;
- sw = File.CreateText(bitcoinPrivateKeysFile);
- sw.WriteLine(bitcoinsecret.PrivateKey);
- Console.WriteLine("File Does not Exist");
- sw.Close();
- }