当前位置: 首页 > 编程语言 > C#

如何修复Xamarin.formsSystem.IO.FileNotFoundException:找不到文件分享

时间:2023-04-11 03:21:07 C#

并阅读。我有lastusername.txt作为文本文件,我将它的构建操作设置为“内容”,实际上我试图按如下方式读取文件:varfilename=System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.个人),"lastusername.txt");if(filename!=null)returnSystem.IO.File.ReadAllText(filename);//这里发生错误elsereturn"";我收到以下错误:System.IO.FileNotFoundException:未找到文件将您的文件放入AndroidAssets文件夹并为其分配构建类型“AndroidAsset”。由于您的应用程序的资产是只读的,您可以通过AssetManager读取它,如果它不存在(即第一次运行该应用程序),请将其保存(复制)到其他地方:varfileName="MyAssetBasedFile.txt";if(!File.Exists(Path.Combine(CacheDir.Path,fileName))){AssetManagerassets=this.Assets;使用(StreamReadersr=newStreamReader(assets.Open(fileName)))使用(StreamWritersw=newStreamWriter(Path.Combine(CacheDir.Path,fileName),附加:false))sw.Write(sr.ReadToEnd());}字符串内容;使用(StreamReadersr=newStreamReader(Path.Combine(CacheDir.Path,fileName))){content=sr.ReadToEnd();}Log.Debug("SO",内容);下次应用程序运行时,您将在缓存目录中选择一个。以上就是C#学习教程:HowtofixXamarin.formsSystem.IO.FileNotFoundException:Cannotfindfilesharing的所有内容,如果对大家有用,需要详细了解C#学习教程,希望大家多多关注——本文来自网络收藏,不代表立场,如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: