本文共 824 字,大约阅读时间需要 2 分钟。
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; } }}
转载地址:http://xounb.baihongyu.com/