Xamarin Forms:如何读取存储在设备外部存储器中的文件的详细信息?
Xamrin Forms: How to read the details of a file stored in device's external storage?
我已经实现了在设备的外部存储中创建文件夹和文件,并使用此 将数据写入该文件。
现在我正在尝试获取文件的详细信息。为此,我在界面中添加了一个新功能,如下所示。
//Interface
public interface IAccessFile
{
void CreateFile(string text);
Java.IO.File GetFileDetails();
}
//Android implementation
public Java.IO.File GetFileDetails()
{
string rootPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
var filePathDir = Path.Combine(rootPath, "StockPDT");
if (File.Exists(filePathDir))
{
string filePath = Path.Combine(filePathDir, "STOCK.TXT");
Java.IO.File file = new Java.IO.File(filePath);
return file;
}
else
{
return null;
}
}
但问题出在接口函数部分,出现以下错误":
The type or namespace name 'Java' could not be found (are you missing a using directive or an assembly reference?)
截图:
如果我return像上面android部分的文件,很容易在可移植项目中获取文件详细信息。我尝试 return 文件路径而不是文件,但它是空的。 除此之外还有其他获取文件详细信息的方法吗?
根据相关注释,我尝试使用其路径获取文件详细信息。
//Interface
public interface IAccessFile
{
string GetFileDetails();
}
//Android implementation
public string GetFileDetails()
{
string rootPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
var filePathDir = Path.Combine(rootPath, "StockPDT");
if (!File.Exists(filePathDir))
{
Directory.CreateDirectory(filePathDir);
}
string filePath = Path.Combine(filePathDir, "STOCK.TXT");
return filePath;
}
//Main Project:
string path = DependencyService.Get<IAccessFile>().GetFileDetails();
string fileDetails = File.ReadAllText(path);
我已经实现了在设备的外部存储中创建文件夹和文件,并使用此
现在我正在尝试获取文件的详细信息。为此,我在界面中添加了一个新功能,如下所示。
//Interface
public interface IAccessFile
{
void CreateFile(string text);
Java.IO.File GetFileDetails();
}
//Android implementation
public Java.IO.File GetFileDetails()
{
string rootPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
var filePathDir = Path.Combine(rootPath, "StockPDT");
if (File.Exists(filePathDir))
{
string filePath = Path.Combine(filePathDir, "STOCK.TXT");
Java.IO.File file = new Java.IO.File(filePath);
return file;
}
else
{
return null;
}
}
但问题出在接口函数部分,出现以下错误":
The type or namespace name 'Java' could not be found (are you missing a using directive or an assembly reference?)
截图:
如果我return像上面android部分的文件,很容易在可移植项目中获取文件详细信息。我尝试 return 文件路径而不是文件,但它是空的。 除此之外还有其他获取文件详细信息的方法吗?
根据相关注释,我尝试使用其路径获取文件详细信息。
//Interface
public interface IAccessFile
{
string GetFileDetails();
}
//Android implementation
public string GetFileDetails()
{
string rootPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
var filePathDir = Path.Combine(rootPath, "StockPDT");
if (!File.Exists(filePathDir))
{
Directory.CreateDirectory(filePathDir);
}
string filePath = Path.Combine(filePathDir, "STOCK.TXT");
return filePath;
}
//Main Project:
string path = DependencyService.Get<IAccessFile>().GetFileDetails();
string fileDetails = File.ReadAllText(path);