NSHost

Is NSHost not available in MonoMac?

Thanks!

Zambi

Posts

  • GerdVanZegbroeckGerdVanZegbroeck BEBeta ✭✭

    I'm trying to get the computer name, is there any other way ?

  • PreranaPrerana INMember ✭✭

    Hey @Zambiorix‌ ,

    NSHost is available in Xamarin.Mac , I got the name of my machine by using the following line of code

         Console.WriteLine (NSHost.Current.LocalizedName.ToString ());
    


    Hope that helps.

  • GerdVanZegbroeckGerdVanZegbroeck BEBeta ✭✭

    I'm currently using MonoMac, not Xamarin.Mac and the binding is not available in MonoMac (to my knowledge)

    I have solved it in the following way :

        //  ComputerName
    
        internal static string ComputerName
        {
            get
            { 
                string result = "Unknown";
    
                try
                {
                    using (NSPipe pipe = new NSPipe ())
                    {
                        using (NSTask task = new NSTask ())
                        {
                            task.LaunchPath = @"/usr/sbin/scutil";
    
                            task.Arguments = new string [] { "--get", "ComputerName" };
    
                            task.StandardOutput = pipe;
    
                            task.Launch ();
    
                            task.WaitUntilExit ();
                        }
    
                        using (NSFileHandle fileHandle = pipe.ReadHandle)
                        {
                            using (NSData data = fileHandle.ReadDataToEndOfFile ())
                            {
                                if (data != null)
                                {
                                    result = data.ToString (NSStringEncoding.UTF8).ToString().Trim();
                                }
                            }
                        }
                    }
                }
                catch
                {
                    //  ignore
                }
    
                return result;
            }
        }
    
Sign In or Register to comment.