Get the current working directory for a terminal application RRS feed

  • Question

  • User119139 posted

    So, here's my problem. I have a console application that I run under windows. It's a discreet application that is paired with a sister app (the GUI). When I ported this using Xamarin.Mac, I created a single app, and in the main threat decided whether the output should generate just console output or actually render the windowing application. All that works fine.

    The problem I've having is getting the current working directory. If I have the terminal open, and am in a folder (let's say /users/me/Desktop) and try to pass just the filename to my process -- I would expect that I could use System.Environment.CurrentDirectory and get the path to the Desktop folder, but instead, this call returns the path to the Mac bundle. The same is try if I use the System.IO.Directory.GetCurrentDirectory function.

    I'm sure I'm missing something - but I'm just unsure how to get the directory where the console process was started because right now, I'm stuck always getting the bundle path.



    Monday, August 29, 2016 2:49 AM

All replies

  • User35201 posted

    This is a known issue, that we've hit before. The way that our launcher currently works is overwriting the currently launch directory.


    If you are willing to depend on some less that documented behavior (though it is likely not changing) then you could do something like:

        [DllImport ("__Internal")]
        public static extern IntPtr xamarin_get_original_working_directory_path ();
        public override void DidFinishLaunching (NSNotification notification)
            string real_path = Marshal.PtrToStringAuto (xamarin_get_original_working_directory_path ());
            Console.WriteLine (real_path);

    It's a hack, but it'll solve your use case.

    Feel free to file me a bug to promote this to a "real" supported feature if you find it useful.

    Monday, September 12, 2016 5:39 PM
  • User119139 posted

    Thanks Chris --

    I'll definitely file the report to promote this. I figured this had to do with the launchers internals, I just couldn't figure out how to get what I needed.



    Monday, September 12, 2016 9:21 PM
  • User119139 posted

    Worked like a treat -- bug report submitted.

    Tuesday, September 13, 2016 1:53 PM
  • User35201 posted

    https://bugzilla.xamarin.com/show_bug.cgi?id=44248 for those playing at home.

    Tuesday, September 13, 2016 8:41 PM
  • User35201 posted

    @TerryReese - I apparently didn't have enough coffee yesterday.

    ObjCRuntime.Runtime.OriginalWorkingDirectory is what you want. All fancy and supported like.

    Tuesday, September 13, 2016 8:48 PM