C# Xamarin WriteAllBytes 正确的文件夹路径

huangapple go评论77阅读模式
英文:

C# Xamarin WriteAllBytes Proper Folder Path

问题

我试图将从mp3文件转换的一组字节写回到mp3文件,以便在用户移动设备上进行临时存储,但似乎无法获得正确的文件夹路径来进行写入。

我已经尝试使用 Directory.GetCurrentDirectory()Environment.CurrentDirectory 和这些变体来获取当前目录,但只收到空值或一个斜杠 /,所以我不确定是否在这种情况下使用它们是否正确。唯一有效的获取路径的方法是 FileSystem.CacheDirectory,它会给出 /data/user/0/com.companyname.lal/cache/

我认为如果我只是想临时存储这个mp3文件,然后在播放后立即删除它,这可能是使用Android和iOS的缓存的正确方法。它似乎有效,但是我无法找到我的Android模拟器文件位于哪个完整路径下的 /data/user/0/com.companyname.lal/cache/。有一个 data 文件夹,但没有 user 或其他子文件夹。

正如我所说,我最终只想在用户移动设备上暂时写入并存储这个文件,然后在播放后将其删除,但现在我只需要找出如何写入文件,以便在代码中访问它。

英文:

C# Xamarin WriteAllBytes 正确的文件夹路径I am trying to write a set of bytes converted from an mp3 file back to an mp3 file to temporarily store in a users mobile device but cannot seem to get the right folder path of where to write to.

I have tried using Directory.GetCurrentDirectory(), Environment.CurrentDirectory, and other variations of these to get the current directory but am only getting back null or just one /, so I'm not sure if I am using these right for this scenario. The only method of getting any path I have found to work is FileSystem.CacheDirectory which gives /data/user/0/com.companyname.lal/cache/.

I think if I am just temporarily wanting to store this mp3 file and then delete it soon after it plays this is maybe the right idea using the cache for both Android and iOS. It appears to work, however I cannot find this /data/user/0/com.companyname.lal/cache/ full path of where my android emulator files are located. There is a data folder but no user or any of those other subfolders.

Like I said I am ultimately wanting to just write and store this file temporarily on a user's mobile device that I can delete it soon after playing, but for now I need to just figure out how to write the file at all where it can be accessed in code.

答案1

得分: 1

正如Jason所说,您可以使用Environment.SpecialFolderEnvironment.GetFolderPath方法来选择适当的位置来存储和检索文件,以下代码将文件存储在系统目录中:

File.WriteAllBytes(Environment.GetFolderPath(Environment.SpecialFolder.System), test);

如果系统目录返回为空,您可以使用ApplicationData来获取应用数据的路径。我创建了一个Xamarin.Forms项目并在iOS和Android模拟器上进行了测试,可以成功返回路径。

var path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

有关方法的更多信息,您可以参考官方文档:

更新: 如果您需要读取文件,例如使用WriteAllBytes

string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "xxx");
File.WriteAllBytes(fileName, XXX);
var test = File.ReadAllBytes(fileName);

有关更多详细信息,请参考:Xamarin.Forms中的文件处理

英文:

As Jason said, you can use the Environment.SpecialFolder and Environment.GetFolderPath methods to select a suitable location for storage and recall, the following code stores the file in the System directory:

File.WriteAllBytes(Environment.GetFolderPath(Environment.SpecialFolder.System),test);

If the System directory returns empty, you can use ApplicationData to get the path of the appdate. I created a Xamarin.Forms project and tested it on both iOS and Android simulators, and the path can be returned successfully.

var path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

C# Xamarin WriteAllBytes 正确的文件夹路径

For more information on the use of methods, you can refer to the official documentation:
Environment.SpecialFolder Enum

Environment.GetFolderPath Method

Update: If you need to read a file, for example if you use WriteAllBytes:

string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "xxx");
File.WriteAllBytes(fileName, XXX);
var test=File.ReadAllBytes(fileName);

For more details, please refer to: File Handling in Xamarin.Forms

huangapple
  • 本文由 发表于 2023年2月27日 01:21:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定