Is there any way to get the Xamarin Studio Project Folder path?

15mgm1515mgm15 USMember ✭✭✭✭

Hello I've been struggling with this for a couple of days, so the question is this:

Is there a way to get the project folder path using code behind? I want to read json files within my project but as you may know the project can be located anywhere in your local pc/mac.

For example I want to get this path:

If the Operating System is OSX:
/Users/MyUser/mobileapp/Project.UI.Tests/Resources/

If the Operating System is Windows:
C:\Users\MyUser\Documents\mobileapp\Project.UI.Tests\Resources\

Is there any way to do this?

Posts

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai

    Is this a platform specific project, a Unit Test project, or a UITest project? Form the name I will guess a UITest project. Generally unless the app is an app that runs on the same platform as your development machine, e.g.. a Xam.Mac app or Mono console app, then you need to include the file in your app bundle and access it via the platform APIs to open resources (guide for iOS and for Android ).

    If this is a Unit Test app, then generally the data should be in your app itself and the platform app should be able to access that data. If a UITest app, again you could include the JSON files in the platform app and then use a backdoor method to return the JSON string to the UITest project.

    Forgive me if I have misunderstood, and if so please clarify.

  • 15mgm1515mgm15 USMember ✭✭✭✭

    Hello @JGoldberger So here is a little more context:

    I have a UITest project inside on my main solution which has a Xamarin.iOS and Xamarin.Android project, since I getting the view tree by the REPL and writing json objects with each id of the view of the tree. (this json file are being created on the UITest project not each platform file system)

    Then with T4 template I am reading those json files and generating classes and properties inside that classes based on the json files.

    The problem: Since I am generating json files for Android and iOS some of my properties are being generated twice per platform, So I wan to read the Json android files on my Xamarin.iOS project check if the properties inside the file and do not add the property to the Json iOS file if it is already added.

    *You may have the question, why do I use T4 templates, well In the future we are going to add more Views on our app and this template will automatically generate the properties depending on the View tree.

    I hope you get my idea.

  • mattwardmattward GBMember Xamurai

    Still not sure what you are trying to do. Where are you trying to get the path from? The T4 template needs to find a file relative to the project's directory? Or does the UITest itself need to get that path?

    In the UITest you should be able to find the location of your compiled UITest assembly using .NET code similar to:

    Path.GetDirectoryName (typeof (YourType).Assembly.Location);
    

    That will give you your bin/Debug directory. Then you can go relative to that to find the project's directory.

    From T4 you should be able to use the standard macros such as $(SolutionDir). I believe these are working now. You can also use Host.ResolvePath from your .t4 template, so things like the following will work:

    <#@ template hostspecific="true" language="C#" #>  
    <#@ output extension=".txt" #>  
    <#@ import namespace="System.IO" #>  
    <#  
     // Find a path within the same project as the text template:  
     string myFile = File.ReadAllText(this.Host.ResolvePath("MyFile.txt"));  
    #>  
    Content of MyFile.txt is:  
    <#= myFile #>  
    

    However I am not totally clear on what you are trying to do.

  • 15mgm1515mgm15 USMember ✭✭✭✭

    Hello @mattward Getting this path inside my Xamarin.iOS project will be the best:

    /Users/MyUser/mobileapp/Project.UI.Tests/Resources/...

    or If it is Visual Studio:

    C:\Users\MyUser\Documents\mobileapp\Project.UI.Tests\Resources\

    So I can read the Json files inside my Xamarin.iOS project (That will be the best case).

    Thanks for replying!

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai
    edited January 2017

    OK, first I hope you realize that you can not run local iOS tests from Windows, only from a Mac using Xamarin Studio

    That said, You can use Path.Combine to get the correct separators (/ or \) depending on the system you are running on, e.g.:

    var pathToFile = Path.Combine(Path.Combine(Path.Combine(Path.Combine("Users", "MyUser"), "mobileapp"), "Project.UI.Tests"), "Resources");
    

    However as you noted there is the additional issue of the solution possibly being in a different path based on the user working with the solution and where they put it (not to mention the different user name). I would think using a relative path here would be OK, but not sure where the Test assembly runs and testing it it seems that an absolute path is required. When I run Environment.CurrentDirectory from the UITest project, it only returns "/", so it seems it is actually running in the root folder of the file system (at least on Mac OS X, did not test on Windows). As such a relative path from the solution is not useful and you will have to provide the full path to the file on each system.

  • 15mgm1515mgm15 USMember ✭✭✭✭
    edited January 2017

    @JGoldberger Thanks that helped a lot! I've put aside the other solution and started with this one so, from the UITest project I have called:

    Environment.CurrentDirectory

    And I am getting back:

    /Users/MyUser/mobileapp/MyProject.UI.Tests/bin/Debug

    Si I am using this to go up two folders: "@"../../"

    It may not be accurate but It works for now:

    var projectRoot = Path.GetFullPath(Path.Combine(Assembly.GetExecutingAssembly().Location, @"../../../"));

    I am not sure if with Visual Studio will work though.

    Thanks!

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai

    Glad that helped. Not sure why Environment.CurrentDirectory was returning "\" for me though. I would have thought it would return the debug directory for the UITest project. Anyway, glad you found a solution. I would think it should work on Windows, but you may need to Path.Combine the ".." parts as well to get the correct separator.

  • var filePath = NSBundle.MainBundle.PathForResource("YourFileName", "FileType");
    Eg: var filePath = NSBundle.MainBundle.PathForResource("Help_Tutorial", "pdf");
    Worked perfect for me for opening PDF file stored in Resource folder of iOS with Build Action as "Bundle Resources".

Sign In or Register to comment.