using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using System.DirectoryServices;
using System.Diagnostics;
using System.Net;
using System.Web;
namespace Ella.ToolKit
{
/// <summary>
/// IP操作
/// <para/>Author : AnDequan
/// <para/>Date : 2010-12-23
/// </summary>
public sealed class InternetHelper : Singleton<InternetHelper>
{
[DllImport("wininet.dll", EntryPoint = "InternetGetConnectedState")]
public extern static bool InternetGetConnectedState(out int conState, int reder);
/// <summary>
/// 是否已经连接上网络
/// <para/>Author : AnDequan
/// <para/>Date : 2010-12-23
/// </summary>
/// <returns>true - 是,false - 否</returns>
public bool IsConnectedToInternet()
{
int iDesc = 0;
return InternetGetConnectedState(out iDesc, 0);
}
/// <summary>
/// 获取网络中所有工作组
/// <para/>Author : AnDequan
/// <para/>Date : 2010-12-23
/// </summary>
/// <returns></returns>
public List<string> GetGroup()
{
List<string> group = new List<string>();
try
{
DirectoryEntry MainGroup = new DirectoryEntry("WinNT:");
foreach (DirectoryEntry domain in MainGroup.Children)
{
group.Add(domain.Name);
}
}
catch (Exception ex)
{
LogHelper.Instance.Add(ex);
}
return group;
}
/// <summary>
/// 获取工作组内所有计算机
/// <para/>Author : AnDequan
/// <para/>Date : 2010-12-23
/// </summary>
/// <param name="group">工作组名称</param>
/// <returns></returns>
public List<string> GetComputerOfGroup(string group)
{
List<string> lstComputer = new List<string>();
try
{
DirectoryEntry MainGroup = new DirectoryEntry("WinNT:");
foreach (DirectoryEntry domain in MainGroup.Children)
{
if (domain.Name == group)
{
foreach (DirectoryEntry pc in domain.Children)
{
if (pc.Name != "Schema")//Schema是结束标记
{
lstComputer.Add(pc.Name);
}
}
}
}
}
catch (Exception ex)
{
LogHelper.Instance.Add(ex);
}
return lstComputer;
}
/// <summary>
/// 局域网内发消息
/// <para/>Author : AnDequan
/// <para/>Date : 2010-12-23
/// </summary>
/// <param name="acceptIP">接受者IP</param>
/// <param name="info">消息</param>
public void SendInfo(string acceptIP, string info)
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = @"cmd.exe";
psi.Arguments = @"/c net send" + acceptIP + " " + info + "";
psi.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(psi);
}
/// <summary>
/// 通过IP取得主机名称
/// <para/>Author : AnDequan
/// <para/>Date : 2010-12-23
/// </summary>
/// <param name="IP"></param>
/// <returns></returns>
public string HostOfIP(string IP)
{
IPHostEntry hostInfo = null; ;
try
{
if (ValidateHelper.Instance.IsRightIP(IP))
hostInfo = Dns.Resolve(IP);
}
catch (Exception ex)
{
LogHelper.Instance.Add(ex);
}
return null == hostInfo ? string.Empty : hostInfo.HostName;
}
/// <summary>
/// 取得客户端真实IP。如果有代理则取第一个非内网地址
/// </summary>
public string IPAddress
{
get
{
string result = String.Empty;
result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (result != null && result != String.Empty)
{
//可能有代理
if (result.IndexOf(".") == -1) //没有“.”肯定是非IPv4格式
result = null;
else
{
if (result.IndexOf(",") != -1)
{
//有“,”,估计多个代理。取第一个不是内网的IP。
result = result.Replace(" ", "").Replace("'", "");
string[] temparyip = result.Split(",;".ToCharArray());
for (int i = 0; i < temparyip.Length; i++)
{
if (IsIPAddress(temparyip[i])
&& temparyip[i].Substring(0, 3) != "10."
&& temparyip[i].Substring(0, 7) != "192.168"
&& temparyip[i].Substring(0, 7) != "172.16.")
{
return temparyip[i]; //找到不是内网的地址
}
}
}
else if (IsIPAddress(result)) //代理即是IP格式
return result;
else
result = null; //代理中的内容 非IP,取IP
}
}
string IpAddress = (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null && HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != String.Empty) ? HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] : HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
if (null == result || result == String.Empty)
result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
if (result == null || result == String.Empty)
result = HttpContext.Current.Request.UserHostAddress;
return result;
}
}
/// <summary>
/// 判断是否是IP地址格式 0.0.0.0
/// </summary>
/// <param name="str1">待判断的IP地址</param>
/// <returns>true or false</returns>
private bool IsIPAddress(string value)
{
if (value == null || value == string.Empty || value.Length < 7 || value.Length > 15) return false;
string regformat = @"^d{1,3}[.]d{1,3}[.]d{1,3}[.]d{1,3}___FCKpd___0quot;";
Regex regex = new Regex(regformat, RegexOptions.IgnoreCase);
return regex.IsMatch(value);
}
#region User - Defined Private Function
#endregion
}
}