怎样通过代码创建本地连接?

Six_dimensional 2010-09-17 10:44:17
我想建立一个本地连接,步骤如下:
控制面板 -> 添加硬件,在向导中选择“是,已经连接了此硬件” -> “添加新的硬件设备” -> “安装我手动从列表中选择的硬件” -> “网络适配器” -> “Microsoft Loopback Adapter”,添加完成后,到网络连接那里刷新,会发现新建了个“本地连接 2”然后设置IP为
设置如下:
IP地址:10.10.10.10
子网掩码:255.255.255.0
网关:空

请问怎样用C#代码实现上面的操作?
...全文
468 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
Six_dimensional 2010-09-25
  • 打赏
  • 举报
回复
怎么做?楼上的请明示!
KnowKjava 2010-09-25
  • 打赏
  • 举报
回复
应该可以的
Six_dimensional 2010-09-25
  • 打赏
  • 举报
回复
每天顶一下,期待高手!
Six_dimensional 2010-09-24
  • 打赏
  • 举报
回复
自己再顶一下!
Six_dimensional 2010-09-24
  • 打赏
  • 举报
回复
跪求答案,哪个好心的帮我解决一下,小生永世不忘!
ysz89757 2010-09-24
  • 打赏
  • 举报
回复
帮顶,学习下
娃ha哈 2010-09-23
  • 打赏
  • 举报
回复
很复杂的说 帮顶
Six_dimensional 2010-09-23
  • 打赏
  • 举报
回复
大家中秋快乐!
zhao_zps 2010-09-22
  • 打赏
  • 举报
回复
帮顶....
Six_dimensional 2010-09-22
  • 打赏
  • 举报
回复
这个是更改的吧,我想添加一个本地连接,具体步骤如下:
控制面板 -> 添加硬件,在向导中选择“是,已经连接了此硬件” -> “添加新的硬件设备” -> “安装我手动从列表中选择的硬件” -> “网络适配器” -> “Microsoft Loopback Adapter”,添加完成后,到网络连接那里刷新,会发现新建了个“本地连接 2”然后设置IP为
设置如下:
IP地址:10.10.10.10
子网掩码:255.255.255.0
网关:空

用C#实现以上的操作,求大侠们帮忙!
Six_dimensional 2010-09-22
  • 打赏
  • 举报
回复
多谢楼上的
JiuchunYoung 2010-09-21
  • 打赏
  • 举报
回复

C#更改IP地址源码
using System;
using System.Management; //引用

namespace ArLi.CommonPrj
{
public class ChangeIP
{

///
/// Build of ArLi 2003.6.3
///
public static readonly System.Version myVersion = new System.Version(1,1);

private ManagementBaseObject iObj = null;
private ManagementBaseObject oObj = null;
private ManagementClass mc = new ManagementClass( "Win32_NetworkAdapterConfiguration ");
private readonly ManagementObjectCollection moc;

///
/// example:
///
/// ArLi.CommonPrj.ChangeIP o = new ArLi.CommonPrj.ChangeIP();
/// string[] ipList = new string[]{ "192.168.0.253 ", "192.168.0.250 "};
/// string[] subnetList = new string[]{ "255.255.255.0 ", "255.255.255.0 "};
/// o.ChangeTo(ipList,subnetList);
///
///
public ChangeIP()
{
moc = mc.GetInstances();
}

/// cortrol
/// IPAddr List
/// subnetMask List
public void ChangeTo(string[] ipAddr,string[] subnetMask)
{
foreach(ManagementObject mo in moc)
{
if(! (bool) mo[ "IPEnabled "]) continue;

iObj = mo.GetMethodParameters( "EnableStatic " );
iObj[ "IPAddress "] = ipAddr;
iObj[ "SubnetMask "] = subnetMask;
oObj = mo.InvokeMethod( "EnableStatic ", iObj, null);
}
}

/// cortrol
/// IPAddr List
/// subnetMask List
/// gateway List
/// gateway CostMetric List, example: 1
public void ChangeTo(string[] ipAddr, string[] subnetMask, string[] gateways, string[] gatewayCostMetric)
{
foreach(ManagementObject mo in moc)
{
if(! (bool) mo[ "IPEnabled "]) continue;

iObj = mo.GetMethodParameters( "EnableStatic ");
iObj[ "IPAddress "] = ipAddr;
iObj[ "SubnetMask "] = subnetMask;
oObj = mo.InvokeMethod( "EnableStatic ", iObj, null);

iObj = mo.GetMethodParameters( "SetGateways ");
iObj[ "DefaultIPGateway "] = gateways;
iObj[ "GatewayCostMetric "] = gatewayCostMetric;
oObj = mo.InvokeMethod( "SetGateways ", iObj, null);
}
}

/// cortrol
/// IPAddr List
/// subnetMask List
/// gateway List
/// gateway CostMetric List, example: 1
/// DNSServer List
public void ChangeTo(string[] ipAddr, string[] subnetMask, string[] gateways, string[] gatewayCostMetric, string[] dnsServer)
{
foreach(ManagementObject mo in moc)
{
if(! (bool) mo[ "IPEnabled "]) continue;

iObj = mo.GetMethodParameters( "EnableStatic ");
iObj[ "IPAddress "] = ipAddr;
iObj[ "SubnetMask "] = subnetMask;
oObj = mo.InvokeMethod( "EnableStatic ", iObj, null);

iObj = mo.GetMethodParameters( "SetGateways ");
iObj[ "DefaultIPGateway "] = gateways;
iObj[ "GatewayCostMetric "] = gatewayCostMetric;
oObj = mo.InvokeMethod( "SetGateways ", iObj, null);

iObj = mo.GetMethodParameters( "SetDNSServerSearchOrder ");
iObj[ "DNSServerSearchOrder "] = dnsServer;
oObj = mo.InvokeMethod( "SetDNSServerSearchOrder ", iObj, null);
}
}

/// DHCPEnabled
public void EnableDHCP()
{
foreach(ManagementObject mo in moc)
{
if(! (bool) mo[ "IPEnabled "]) continue;

if(! (bool)mo[ "DHCPEnabled "])
{
iObj = mo.GetMethodParameters( "EnableDHCP ");
oObj = mo.InvokeMethod( "EnableDHCP ", iObj, null);
}
}
}
}
}



对我有用[0] 丢个板砖[0] 引用 举报 管理 TOP 精华推荐:SQLServer2005 String类的移植尝试

dugupiaoyun

(独孤飘云)

等 级:

#3楼 得分:0回复于:2007-03-31 12:42:09//通过WMI修改IP,而实现不用重新启动?
using System;
using System.Management;
using System.Threading;
using System.Net;
using System.Net.Sockets;

namespace WmiIpChanger
{
class IpChanger
{
[MTAThread]
static void Main(string[] args)
{
ReportIP();
// SwitchToDHCP();
SwitchToStatic();
Thread.Sleep( 5000 );
ReportIP();

Console.WriteLine( "end. " );

Console.ReadLine();
}

static void SwitchToDHCP()
{
ManagementBaseObject inPar = null;
ManagementBaseObject outPar = null;
ManagementClass mc = new ManagementClass( "Win32_NetworkAdapterConfiguration ");
ManagementObjectCollection moc = mc.GetInstances();
foreach( ManagementObject mo in moc )
{
if( ! (bool) mo[ "IPEnabled "] )
continue;

inPar = mo.GetMethodParameters( "EnableDHCP ");
outPar = mo.InvokeMethod( "EnableDHCP ", inPar, null );
break;
}
}

static void SwitchToStatic()
{
ManagementBaseObject inPar = null;
ManagementBaseObject outPar = null;
ManagementClass mc = new ManagementClass( "Win32_NetworkAdapterConfiguration ");
ManagementObjectCollection moc = mc.GetInstances();
foreach( ManagementObject mo in moc )
{
if( ! (bool) mo[ "IPEnabled " ] )
continue;

inPar = mo.GetMethodParameters( "EnableStatic " );
inPar[ "IPAddress "] = new string[] { "10.1.11.78 " };
inPar[ "SubnetMask "] = new string[] { "255.255.255.0 " };
outPar = mo.InvokeMethod( "EnableStatic ", inPar, null );
break;
}
}

static void ReportIP()
{
Console.WriteLine( "****** Current IP addresses: " );
ManagementClass mc = new ManagementClass( "Win32_NetworkAdapterConfiguration ");
ManagementObjectCollection moc = mc.GetInstances();
foreach( ManagementObject mo in moc )
{
if( ! (bool) mo[ "IPEnabled " ] )
continue;

Console.WriteLine( "{0}\n SVC: '{1}\n ' MAC: [{2}] ", (string) mo[ "Caption "],
(string) mo[ "ServiceName "], (string) mo[ "MACAddress "] );

string[] addresses = (string[]) mo[ "IPAddress " ];
string[] subnets = (string[]) mo[ "IPSubnet " ];

Console.WriteLine( " Addresses : " );
foreach(string sad in addresses)
Console.WriteLine( "\t '{0} ' ", sad );

Console.WriteLine( " Subnets : " );
foreach(string sub in subnets )
Console.WriteLine( "\t '{0} ' ", sub );
}
}
}









@echo off
rem eth
set eth= "本地链接 "
rem ip
set ip=XXX.XXX.XXX
rem gw // 网关
set gw=XXX.XXX.XXX.XXX
rem netmasks //子网掩码
set netmasks=255.255.255.0
rem
if %gw%==none netsh interface ip set address %eth% static %ip% %netmasks% %gw% > nul
if not %gw%==none netsh interface ip set address %eth% static %ip% %netmasks% %gw% 1 > nul

close

使用C#执行这段 批处理即可
Six_dimensional 2010-09-21
  • 打赏
  • 举报
回复
求解,那位大侠解释一下
Six_dimensional 2010-09-20
  • 打赏
  • 举报
回复
有没有那位能详细介绍一下,高分悬赏啊,重赏之下竟然没有勇夫?
amy_guoguo 2010-09-18
  • 打赏
  • 举报
回复
帮顶。。。。。。。。。。。。。。。。。。
Six_dimensional 2010-09-18
  • 打赏
  • 举报
回复
其实我是想创建一个本地连接!
Six_dimensional 2010-09-18
  • 打赏
  • 举报
回复
多谢!
还有没有踊跃发言的童鞋!
hao1hao2hao3 2010-09-18
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 six_dimensional 的回复:]
其实我是想创建一个本地连接!
[/Quote]

不懂,帮顶,蹭分。
liulangdeyuyu 2010-09-18
  • 打赏
  • 举报
回复
混分~~~~~~~~
长汕 2010-09-18
  • 打赏
  • 举报
回复
API
加载更多回复(3)

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧