Go to Google Groups Home
Groups  
Advanced Groups Search    Preferences    Groups Help 
 
Viewing message <#EwffAXACHA.2308@tkmsftngp02> 
 DynamicPDF Generator.NET ? Total output control! - New v1.6 Web Invoices, Schedules & Reports. ? dynamicpdf.com Sponsored Links 
 C# TraceListener Article ? Need a free C# TraceListener? Use Reflection to leverage .NET ? www.codeproject.com
 C# tutorial and resources ? AspRelated.com offers free C# tutorials, resources, articles ? www.asprelated.com
From: NETMaster (spam.netmaster@swissonline.ch)
Subject: Re: WMI or How to change my IP address
View: Complete Thread (6 articles)
Original Format
Newsgroups: microsoft.public.dotnet.framework
Date: 2002-05-22 02:09:48 PST
If you like, low-level IP-Helper API:
  http://msdn.microsoft.com/library/en-us/tcpip/iphpport_7vz9.asp

  PInvoke with C# (no IP change impl.?)
    http://www.gotdotnet.com/team/p2p/
    http://www.gotdotnet.com/userfiles/herveyw/netsamples.zip


WMI:
  Win32_NetworkAdapterConfiguration:
    http://msdn.microsoft.com/library/en-us/wmisdk/r_32hard4_6oq6.asp

  Methods: EnableStatic + EnableDHCP
    http://msdn.microsoft.com/library/en-us/wmisdk/r_32hard4_0ujy.asp
    http://msdn.microsoft.com/library/en-us/wmisdk/r_32hard4_27u6.asp

  Changeip VB-Scripts:
    http://desktopengineer.com/index.php?topic=0080WMI
    http://cwashington.netreach.net/depo/view.asp?Index=628

VERY simplified  WMI sample:
// ===============================================================================
using System;
using System.Management;
using System.Threading;

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


 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[] { "192.168.1.1" };
   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}'   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 );
  }
 }
}
}

// ===============================================================================

WARNING: do MUCH more error checking, multiple NIC tests, timing!...
  use all at at your own risk!



--
  NETMaster  (Thomas Scheidegger)
  http://www.cetus-links.org/oo_csharp.html






"Marco Scheel" <atWork@Visual-eVolution.de> wrote in message news:14765441.1022081866849.JavaMail.SYSTEM@oscar...
> i'm looking for a way to change the local ip address auf my notebook. i'd
> like to write some code that can change my configuration from static ip to
> an dhcp configuration. looked at System.Managment, but i've got no idea how
> this wmi thing works.


Google Home - Advertise with Us - Business Solutions - Services & Tools - Jobs, Press, & Help

?2003 Google