从正在运行的iOS/macOS应用程序实例(Xamarin Forms)获取资源文件夹路径的正确方法是什么 [英] What is the correct way to obtain the Resource folder path from the running iOS / macOS app instance (Xamarin Forms)

查看:131
本文介绍了从正在运行的iOS/macOS应用程序实例(Xamarin Forms)获取资源文件夹路径的正确方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从应用程序的资源"文件夹中复制某些文件时遇到困难:

I am having difficulties copying some files from my application Resources folder:

   var myDocuments = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Library");

    // Confirm these paths are OK for iOS too
    var root = Path.Combine(myDocuments, "VisitsRota.MacOS");
    var styles = Path.Combine(root, "Styles");
    var stylesheet = Path.Combine(styles, "ElderlyInfirm-Schedule-v1.css");
    var db = Path.Combine(root, "ElderlyInfirmRotaData.xml");

    var defaultroot = Path.Combine( ".", "Resources");

    // Main folder
    if (!Directory.Exists(root))
    {
        Directory.CreateDirectory(root);
    }

    // Database
    if (!File.Exists(db))
    {
        var defaultdb = Path.Combine(defaultroot, "ElderlyInfirmRotaData.xml");
        File.Copy(defaultdb, db);
    }

    // Styles folder
    if (!Directory.Exists(styles))
    {
        Directory.CreateDirectory(styles);
    }

    // Stylesheet
    if (!File.Exists(stylesheet))
    {
        var defaultstylesheet = Path.Combine(defaultroot, "Styles", "ElderlyInfirm-Schedule-v1.css");
        File.Copy(defaultstylesheet, stylesheet);
    }

问题在于确定应用程序文件夹的Resources文件夹,此刻,我得到此异常:

The problem is determining the application folders Resources folder, At the moment I get this exception:

访问资源"文件夹的正确方法是什么(对于iOS或macOS)?

What is the correct way to get to the Resources folder (for either iOS or macOS)?

在IDE中,我仅看到一个Resource文件夹:

In the IDE I only see just the one Resource folder:

我的两个资源都将B 统一操作设置为内容.

Both of my resources have a Build Action set to Content.

感谢您指出正确的方向来解决此问题.

Thank you for pointing me in the right direction to resolve this.

我尝试使用以下结果:

public static string GetExecutingDirectoryName()
{
    var location = new Uri(Assembly.GetEntryAssembly().GetName().CodeBase);
    return new FileInfo(location.AbsolutePath).Directory.FullName;
}

那是完全错误的.

我正在VS for Mac(macOS构建)的Debugger中运行该应用程序.

I am running the app in the Debugger in VS for Mac (macOS build).

我认为对这些文件使用Resource文件夹是可以的.除非有更好的方法可以做到这一点?

I assume that using the Resource folder for these files was OK. Unless there is a better way to do this?

推荐答案

使用BundleResource容易得多:

It was much easier to use BundleResource:

var myDocuments = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Library");

// Create the folders first

// Main folder
var root = Path.Combine(myDocuments, "VisitsRota.MacOS");
if (!Directory.Exists(root))
{
    Directory.CreateDirectory(root);
}

// Styles folder
var styles = Path.Combine(root, "Styles");
if (!Directory.Exists(styles))
{
    Directory.CreateDirectory(styles);
}

// Now copy the files

// Database
var db = Path.Combine(root, "ElderlyInfirmRotaData.xml");
var defaultdb = NSBundle.MainBundle.PathForResource("ElderlyInfirmRotaData", ofType: "xml");
if (!File.Exists(db))
{
    File.Copy(defaultdb, db);
}

// Stylesheet
var stylesheet = Path.Combine(styles, "ElderlyInfirm-Schedule-v1.css");
var defaultstylesheet = NSBundle.MainBundle.PathForResource("ElderlyInfirm-Schedule-v1", ofType: "css");
if (!File.Exists(stylesheet))
{
    File.Copy(defaultstylesheet, stylesheet);
}   

这篇关于从正在运行的iOS/macOS应用程序实例(Xamarin Forms)获取资源文件夹路径的正确方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆