获取嵌入式资源 Xamarin Forms 的完整路径 [英] Get Fullpath of embedded resource Xamarin Forms
问题描述
我目前正在 xamarin 表单中使用 FilePicker 来选择一个 zip 文件,然后我从中获取完整路径并在 Xamarin.Android 项目中使用此完整路径.使用以下代码:
I am currently using FilePicker in xamarin forms to choose a zip file, then I get fullpath from it and use this fullpath in Xamarin.Android project. Using following code:
var result = await FilePicker.PickAsync(new PickOptions
{
PickerTitle = "Select zip file"
});
if (result != null)
{
FileNameLabel.Text = $"{result.FileName}";
if (!result.FileName.EndsWith("zip", StringComparison.OrdinalIgnoreCase))
{
await DisplayAlert("Wrong File", "Selected file is not right", "Ok");
}
else
{
var stream = result.OpenReadAsync();
_filepath = result.FullPath;
}
}
我想做同样的事情,但没有 FilePicker,而是将我的 zip 文件添加为嵌入资源.我知道我可以使用以下代码访问嵌入式资源,但我无法获得 FullPath.
I want to do the same thing, but without FilePicker, instead add my zip file as embedded resource. I know I can access, embedded resouce using following code, but I am unable to get FullPath.
var assembly = IntrospectionExtensions.GetTypeInfo(typeof(App)).Assembly;
Stream stream = assembly.GetManifestResourceStream("TestFile.json");
是否可以执行以下操作:
is it possible to do something like:
File file = new File(assembly.GetManifestResourceStream("TestFile.Zip"));
_fullname = file.FullName;
推荐答案
是的,您可以使用 Xamarin.Essentials 获取操作系统的缓存目录,然后将嵌入的资源复制到其中.
Yes,you could use Xamarin.Essentials to obtain the OS's cache directory and then copy the embedded resource to it.
例如:
var cacheFile = Path.Combine(FileSystem.CacheDirectory, "your file");
if (File.Exists(cacheFile))
File.Delete(cacheFile);
using (var resource = Assembly.GetExecutingAssembly().GetManifestResourceStream("your embedded resource"))
using (var file = new FileStream(cacheFile, FileMode.Create, FileAccess.Write))
{
resource.CopyTo(file);
}
这篇关于获取嵌入式资源 Xamarin Forms 的完整路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!