申请会员ID:chfei
1、申 请 I D:chfei2、个人邮箱:chaihongfei@qq.com
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using NETCONLib;
using System.Net.NetworkInformation;
using System.Drawing;
namespace VirtualWifi
{
class Win7Operating : IOperatingWifi, IWifiInfo
{
/// <summary>
/// 委托
/// </summary>
/// <param name="mess"></param>
/// <param name="color"></param>
public delegate void ShowMess(string mess, Color color);
public event ShowMess _showmes;
/// <summary>
/// 注册表路径
/// </summary>
RegistryKey RegKey = Registry.CurrentUser.OpenSubKey("SOFTWARE", true).OpenSubKey("Microsoft", true);
/// <summary>
/// 网络适配器列表
/// </summary>
Dictionary<string, NetManager> DIClist = new Dictionary<string, NetManager>();
/// <summary>
/// 共享网络适配器名称
/// </summary>
string GxName = null;
/// <summary>
/// 虚拟适配器名称
/// </summary>
string XnName = null;
/// <summary>
/// 运行程序
/// </summary>
public void Run(WifiInfo info)
{
if (CreateWifi(info))//创建WIFi
{
_showmes("Wifi创建成功", Color.Black);
GetInternetName();//获取网络适配器列表
_showmes("开启虚拟网卡...", Color.Black);
if (NetDsable())
{
_showmes("智能获取联网适配器...", Color.Black);
GxName = ZNGetNet();//智能匹配网络适配器
_showmes("正在关闭其他共享设备...", Color.Black);
if (DisableSharing())//关闭共享
{
_showmes("正在开启共享网络...", Color.Black);
if (ShareWIFI())//开启共享
{
_showmes("开启共享成功", Color.Black);
if (StartWifi())//开启wifi
{
_showmes("开启WIFI成功,可以连接设备", Color.Black);
}
}
}
}
}
}
/// <summary>
/// 退出程序
/// </summary>
public void Quit(WifiInfo info)
{
_showmes("正在关闭网络共享...", Color.Black);
if (DisableSharing())
{
_showmes("正在删除虚拟Wifi...", Color.Black);
if (DelWifi())
{
_showmes("正在保存连接信息...", Color.Black);
if (SaveInfo(info))
{
_showmes("关闭Wifi成功", Color.Black);
}
}
}
}
/// <summary>
/// 创建WIFi
/// </summary>
/// <returns></returns>
public bool CreateWifi(WifiInfo info)
{
string cmd = string.Format("netsh wlan set hostednetwork mode=allow ssid={0} key={1}", info.WifiName, info.WifiPwd);//创建wifi
string Context = ExecutionDos.Execute(cmd, 1000);
if (Context.Contains("承载网络模式已设置为允许。") && Context.Contains("已成功更改承载网络的 SSID。") && Context.Contains("已成功更改托管网络的用户密钥密码。"))
return true;
else
{
_showmes(Context, Color.Red);
return false;
}
}
/// <summary>
/// 开启WIFi
/// </summary>
/// <returns></returns>
public bool StartWifi()
{
GxName = ZNGetNet();
string cmd = "netsh wlan start hostednetwork";//开启wifi
string Context = ExecutionDos.Execute(cmd, 1000);
if (Context.Contains("已启动承载网络"))
return true;
else
{
_showmes(Context, Color.Red);
return false;
}
}
/// <summary>
/// 删除wifi
/// </summary>
/// <returns></returns>
public bool DelWifi()
{
string cmd = "netsh wlan set hostednetwork mode=disallow";//删除
string Context = ExecutionDos.Execute(cmd, 1000);
if (Context.Contains("承载网络模式已设置为禁止"))
return true;
else
{
_showmes(Context, Color.Red);
return false;
}
}
/// <summary>
/// 保存信息
/// </summary>
/// <returns></returns>
public bool SaveInfo(WifiInfo info)
{
try
{
RegistryKey TempRegKey = RegKey.CreateSubKey("WIFI");
TempRegKey.SetValue("UserName", info.WifiName);
TempRegKey.SetValue("PassWord", info.WifiPwd);
return true;
}
catch (Exception ex)
{
_showmes(ex.Message, Color.Red);
return false;
}
}
/// <summary>
/// 获取信息
/// </summary>
/// <returns></returns>
public WifiInfo GetInfo()
{
WifiInfo info = new WifiInfo();
try
{
RegistryKey TempRegKey = RegKey.OpenSubKey("WIFI");
if (TempRegKey != null)
{
string tempuser = TempRegKey.GetValue("UserName").ToString();
string temppwd = TempRegKey.GetValue("PassWord").ToString();
info.WifiName = string.IsNullOrEmpty(tempuser) ? info.WifiName : tempuser;
info.WifiPwd = string.IsNullOrEmpty(temppwd) ? info.WifiPwd : temppwd;
}
return info;
}
catch (Exception ex)
{
_showmes(ex.Message, Color.Red);
return info;
}
}
/// <summary>
/// 获取所有网络列表
/// </summary>
/// <returns></returns>
void GetInternetName()
{
try
{
DIClist.Clear();
NetSharingManager manager = new NetSharingManager();
INetSharingEveryConnectionCollection connections = manager.EnumEveryConnection;
foreach (INetConnection c in connections)
{
NetManager netmarager = new NetManager();
netmarager.Props = manager.NetConnectionProps;
netmarager.Sharing = manager.INetSharingConfigurationForINetConnection;
if (netmarager.Props.DeviceName == "Microsoft Virtual WiFi Miniport Adapter")
XnName = netmarager.Props.Name;
DIClist.Add(netmarager.Props.Name, netmarager);
}
}
catch (Exception ex)
{
_showmes(ex.Message, Color.Red);
}
}
# region 智能选择
/// <summary>
/// 判断是否有宽带连接信息是否在连接状态
/// </summary>
/// <returns></returns>
string IFPPPOE()
{
var manager = new NetSharingManager();
var connections = manager.EnumEveryConnection;
foreach (KeyValuePair<string, NetManager> item in DIClist)
{
if (item.Value.Props.MediaType == tagNETCON_MEDIATYPE.NCM_PPPOE && item.Value.Props.Status == tagNETCON_STATUS.NCS_MEDIA_DISCONNECTED)
return item.Key;
}
return null;
}
/// <summary>
/// 获取最流量最高的网卡名称
/// </summary>
/// <returns></returns>
string GetIPV4()
{
string name = null;
long received = 0;
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface ni in interfaces)
{
long item = ni.GetIPv4Statistics().BytesReceived;
if (item > received)
{
name = ni.Name;
received = item;
}
}
return name;
}
/// <summary>
/// 智能选择网络上的网卡信息
/// </summary>
/// <returns></returns>
string ZNGetNet()
{
string name = IFPPPOE();
if (!string.IsNullOrEmpty(name))
return name;
else
name = GetIPV4();
return name;
}
#endregion
/// <summary>
/// 启用网卡
/// </summary>
/// <returns></returns>
bool NetDsable()
{
try
{
if (DIClist.Props.Status == tagNETCON_STATUS.NCS_DISCONNECTED)
return ExecutionDos.ChangeNetworkConnectionStatus(XnName, true);
else
return true;
}
catch (Exception ex)
{
_showmes(ex.Message, Color.Red);
return false;
}
}
/// <summary>
/// 共享网络WIFI
/// </summary>
bool ShareWIFI()
{
try
{
DIClist.Sharing.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC);
DIClist.Sharing.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE);
return true;
}
catch (Exception ex)
{
_showmes(ex.Message, Color.Red);
return false;
}
}
/// <summary>
/// 关闭共享
/// </summary>
bool DisableSharing()
{
try
{
foreach (KeyValuePair<string, NetManager> item in DIClist)
{
if (item.Value.Sharing.SharingEnabled)
item.Value.Sharing.DisableSharing();
}
return true;
}
catch (Exception ex)
{
_showmes(ex.Message, Color.Red);
return false;
}
finally
{
GC.Collect();
}
}
}
}
ID:chfei
邮箱:chaihongfei@qq.com
申请通过,欢迎光临吾爱破解论坛,期待吾爱破解有你更加精彩,ID和密码自己通过邮件密码找回功能修改,请即时登陆并修改密码!
登陆后请在一周内在此帖报道,否则将删除ID信息。
Ps:说实话申请内容过于简单,此类程序在网上开源也较多,期待加入吾爱后有更多的分享吧。 Hmily 发表于 2015-1-22 10:55
ID:chfei
邮箱:
好吧 邮箱发错了 是126的……{:1_937:}
应该是chaihongfei@126.com
这几天测试数据老用qq邮箱{:1_907:} 原谅我这一次吧 游客 58.243.227.x 发表于 2015-1-22 14:08
好吧 邮箱发错了 是126的……
应该是
这几天测试数据老用qq邮箱 原谅我这一次吧
感觉无法原谅啊! 老黑,好久不见 瞬间我就笑了,看完
页:
[1]