获得本机的计算机名称

来源:互联网 发布:latex 算法 for 编辑:程序博客网 时间:2024/05/24 06:00
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.InteropServices;using System.Windows.Forms;namespace GetLocalNameApp{        class Program    {                [DllImport("kernel32.dll")]        static extern bool GetComputerName(IntPtr p, ref int lpnSize);        static void Main(string[] args)        {            string strLocalName = LocalComputerName();            Console.WriteLine(strLocalName);            Console.Read();                  }        static string LocalComputerName()        {            IntPtr p = Marshal.AllocHGlobal(128);            int len = 128;            GetComputerName(p, ref len);            string strName = Marshal.PtrToStringAnsi(p);            //释放指针占用的内存            Marshal.FreeHGlobal(p);            return strName;        }    }}

原创粉丝点击