Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android上用C#将文本写入文本文件_C#_Android_File_Streamreader_Streamwriter - Fatal编程技术网

如何在Android上用C#将文本写入文本文件

如何在Android上用C#将文本写入文本文件,c#,android,file,streamreader,streamwriter,C#,Android,File,Streamreader,Streamwriter,我想用C语言编写一个代码,在Android中读取一个文本文件,如果它不存在,就创建它。我希望我的应用程序每次运行我的应用程序时都读取相同的文件,并在我第一次打开我的应用程序时创建该文件。我编写了此代码,但它不起作用。它仅在目录不存在时创建一个目录,但不创建我的文本文件。提前感谢您的回答 network=Network.Main; //string _fileName = Path.Combine(, "bitcoin

我想用C语言编写一个代码,在Android中读取一个文本文件,如果它不存在,就创建它。我希望我的应用程序每次运行我的应用程序时都读取相同的文件,并在我第一次打开我的应用程序时创建该文件。我编写了此代码,但它不起作用。它仅在目录不存在时创建一个目录,但不创建我的文本文件。提前感谢您的回答

network=Network.Main;
        
        
        
        //string _fileName = Path.Combine(, "bitcoinwallets.phoenixbtcwallet");
        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))
        {
        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();
        }
        
        
        
        btn.Text = "New Wallet";
        
        bitcoinWalletAddressTextView.Text=bitcoinprivKeys[0];

您正在检查
文件。存在(…)!=null
这有点荒谬,因为
File.Exists
返回一个
bool
值,该值永远不会
null

所以只要做
if(File.Exists(…){…}

if (File.Exists(...)) 
{
     // read file
}
else
{
    // create file
}

如果文本文件不存在,您似乎缺少创建该文本文件的代码。对于目录,则有Directory.exists.BitcoinPrivateKeyFile=filePathDir+“bitcoinwallets.phoenixbtcwallet”。。。真的吗?但我还是会犯同样的错误-你会犯哪种错误?谢谢。问题解决方案我得到了错误null异常引用