0

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);
    }

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

enter image description here

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

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

enter image description here

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

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


I tried using the result from:

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

It was completely wrong.

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


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

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164

1 Answers1

0

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);
}   
Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164