項目背景:需在后臺(SQL)記錄各PDA系統的使用情況,故需從前端傳入當前設備的ip地址與機器編碼。
得到本機IP:
public string getLocalIP()
{
string strHostName = Dns.GetHostName(); //得到本机的主机名
IPHostEntry ipEntry = Dns.GetHostByName(strHostName); //取得本机IP
string strAddr = ipEntry.AddressList[0].ToString();
return (strAddr);
}
獲取機器名稱:
[DllImport("coredll.dll", EntryPoint = "KernelIoControl", SetLastError = true)]
private extern static bool KernelIoControl(Int32 IoControlCode, IntPtr InputBuffer, Int32 InputBufferSize, byte[] OutputBuffer, Int32 OutputBufferSize, ref Int32 BytesReturned);
private static Int32 FILE_DEVICE_HAL = 0x00000101;
private static Int32 IOCTL_HAL_GET_DEVICEID =
((FILE_DEVICE_HAL) << 16) | ((0x0) << 14) | ((21) << 2) | (0x0);
public string GetPDASerialNumber()
{
byte[] outputBuffer = new byte[256];
Int32 outputBufferSize = outputBuffer.Length;
Int32 bytesReturned = 0;
bool retVal = KernelIoControl(IOCTL_HAL_GET_DEVICEID, IntPtr.Zero, 0, outputBuffer, outputBufferSize, ref bytesReturned);
// If the request failed, exit the method now
if (retVal == false)
{
return String.Empty;
}
Int32 presetIdOffset = BitConverter.ToInt32(outputBuffer, 4);
Int32 platformIdOffset = BitConverter.ToInt32(outputBuffer, 0xc);
Int32 platformIdSize = BitConverter.ToInt32(outputBuffer, 0x10);
StringBuilder sb = new StringBuilder();
sb.Append(String.Format(CultureInfo.InvariantCulture, "{0:X8}-{1:X4}-{2:X4}-{3:X4}-",
BitConverter.ToInt32(outputBuffer, presetIdOffset),
BitConverter.ToInt16(outputBuffer, presetIdOffset + 4),
BitConverter.ToInt16(outputBuffer, presetIdOffset + 6),
BitConverter.ToInt16(outputBuffer, presetIdOffset + 8)));
for (int i = platformIdOffset; i < platformIdOffset + platformIdSize; i++)
{
sb.Append(String.Format(CultureInfo.InvariantCulture, "{0:X2}", outputBuffer[i]));
} // return the device id string
return sb.ToString();
}
String MNum = GetPDASerialNumber(); ? ? ? ? ? ? if (MNum.Length >= 50) ? ? ? ? ? ? ? ? MNum = MNum.Substring(24, 16);
MessageBox.Show("machineNum: " + MNum); //0079005A-006C-006F-006E-00
MessageBox.Show("IP: " + getLocalIP()); //10.4.211.198
傳輸方式就是通過WebService把相應參數傳入SQL,此處不贅述。
附 - 常見異常:
1.引入命名空間
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Net;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;
using System.Globalization;
using System.Windows.Forms;
2.缺少OpenNETCF DLL集合 - > 自己去 CSDN資源 下載吧~
3.在MSDN上提到GetHostByName已过时。替代品是GetHostEntry。但GetHostEntry得到的并不是我們想要的地址。
public string getLocalIP()
{
string strAddr = "";
//string strAddr1 ="";
string strHostName = Dns.GetHostName(); //得到本机的主机名
//IPHostEntry ipEntry = Dns.GetHostByName(strHostName); //取得本机IP 10.4.0.134
IPHostEntry ipEntry1 = Dns.GetHostEntry(strHostName); //取得本机IP fe80::7060:37ec:9e18:31ec%3
for (int i = 0; i < ipEntry1.AddressList.Length; i++)
{
//从IP地址列表中筛选出IPv4类型的IP地址
//AddressFamily.InterNetwork表示此IP为IPv4,
//AddressFamily.InterNetworkV6表示此地址为IPv6类型
if (ipEntry1.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
{
strAddr = ipEntry1.AddressList[i].ToString();//10.4.0.134
}
//else if(ipEntry1.AddressList[i].AddressFamily == AddressFamily.InterNetworkV6)
//{
// strAddr1 = ipEntry1.AddressList[i].ToString();//fe80::7060:37ec:9e18:31ec%3
//}
}
return (strAddr);
}
|