我试图在Xamarin Studio中为我的移动应用程序读取Json文件,但它却抛出了一个错误
找不到文件
虽然我在控制台应用程序中尝试相同,但每件事情都可以。 我试着用这种方式阅读文件:
string Jason = File.ReadAllText(@"C:\Folder\file.txt")
答案 0 :(得分:0)
此信息取自Xamarin大学的Xam160课程。在移动应用程序中,平台会为您的应用程序生成一个独立的文件夹区这些是存储特定于您的应用程序的文件的首选位置,AppHome代表沙盒区域的根。
Android - AppHome / files
iOS - AppHome / Lirbrary / [子目录]
Windows Phone - AppHome \ Local
要访问这些路径,您可以使用Environment.SpecialFolder枚举来使用Environment.GetFolder()方法访问这些位置。
//Android
string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
//iOS
string docFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string libFolder = System.IO.Path.Combine(docFolder,"..","Library");
// Windows has different implementation. GetFolder Path with throw exception on windows.
string path = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
每个这些都可以让您访问所需的路径,以便能够在您所在的平台上本地读取和写入文件。