PDA

View Full Version : تغییر ip



ghasemloo
شنبه 02 شهریور 1392, 19:33 عصر
سلام بچه ها!!!
چطور میشه با کد نویسی ip رو تغییر داد

ma.rad
شنبه 02 شهریور 1392, 20:51 عصر
برای اینکار دوتابع نیاز دارید. تابع اول مشخصات فعلی شبکه را می دهد و تابع دوم آی پی مورد نظر شما را فعال می کند.
کد:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;

namespace ConfigurationIP
{
class Program
{
static void Main(string[] args)
{
ListIP();
SetIP("192.168.22.77", "255.255.255.0", "192.168.22.22");
}

public static void ListIP()
{
ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMC.GetInstances();

foreach (ManagementObject objMO in objMOC)
{
if (!(bool)objMO["ipEnabled"])
continue;

Console.WriteLine(objMO["Caption"] + "," +
objMO["ServiceName"] + "," + objMO["IPAddress"]);

string[] ipAddresses = (string[])objMO["IPAddress"];
string[] Subnets = (string[])objMO["IPSubnet"];
string[] Gateways = (string[])objMO["DefaultIPGateway"];

Console.WriteLine("Printing Default Gateway Info:");
Console.WriteLine(objMO["DefaultIPGateway"]);

Console.WriteLine("Printing IPGateway info:");
foreach (string sGate in Gateways)
{
Console.WriteLine(sGate);
}

Console.WriteLine("Printing IPAddress info:");
foreach (string sIP in ipAddresses)
{
Console.WriteLine(sIP);
}

Console.WriteLine("Printing SubNet Info:");
foreach (string sNet in Subnets)
{
Console.WriteLine(sNet);
}
}
Console.ReadLine();
}

public static void SetIP(string IPAddress, string SubnetMask, string Gateway)
{
ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMC.GetInstances();

foreach (ManagementObject objMO in objMOC)
{
if (!(bool)objMO["IPEnabled"])
continue;

try
{
ManagementBaseObject objNewIP = null;
ManagementBaseObject objSetIP = null;
ManagementBaseObject objNewGate = null;

objNewIP = objMO.GetMethodParameters("EnableStatic");
objNewGate = objMO.GetMethodParameters("SetGateways");

objNewGate["DefaultIPGateway"] = new string[]
{
Gateway
};
objNewGate["GatewayCostMetric"] = new int[]
{
1
};

objNewIP["IPAddress"] = new string[]
{
IPAddress
};
objNewIP["SubnetMask"] = new string[]
{
SubnetMask
};

objSetIP = objMO.InvokeMethod("EnableStatic", objNewIP, null);
objSetIP = objMO.InvokeMethod("SetGateways", objNewGate, null);

Console.WriteLine("Updated IPAddress, SubnetMask and Default Gateway!");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("Unable to Set IP : " + ex.Message);
Console.ReadLine();
}
}
}
}
}

m.webgard
یک شنبه 03 شهریور 1392, 01:07 صبح
سلام
یه روش هم اینه که کدت رو در CMD بنویسی و به عنوان فایل ذخیره کنی
بعدش در VS اجراش کنی
موفق باشی

ghasemloo
جمعه 08 شهریور 1392, 13:12 عصر
سلامی دوباره !من روی کدهای بال کار کردم ااما از چیزی سر در نیاوردم!!!
یکی باشه بگه نحوه ی استفاده رو بم بگه
من کار با کلاسم ضعیفه:لبخند:

ghasemloo
شنبه 09 شهریور 1392, 12:18 عصر
؟؟؟؟؟؟
!!!!!!!!!

mfaridi
شنبه 09 شهریور 1392, 12:47 عصر
اگه بخوای با cmd تغیر بدی
netsh interface ip set address "Local Area Connection" static 192.168.0.10 255.255.255.0 192.168.0.1 1

khokhan
شنبه 09 شهریور 1392, 16:06 عصر
؟؟؟؟؟؟
!!!!!!!!!


Threading and the Asynchronous Pattern (http://www.winsocketdotnetworkprogramming.com/threadingasynchronouspatterninnetwork3b.html)



تغییر ip در winform (http://social.msdn.microsoft.com/Forums/vstudio/en-US/8bf269bd-128e-457f-8c64-09834e995b59/how-to-change-ip-address-of-local-system-using-c)

لینک1 (http://joefreeman.co.uk/blog/2010/03/creating-a-setup-project-for-a-windows-wcf-service-with-visual-studio/) لینک 2 (http://stackoverflow.com/questions/5591457/get-ip-address-location-from-windows-application) لینک 3 (http://www.codeproject.com/Articles/11576/IP-TextBox) لینک 4 (http://www.codeproject.com/Articles/9352/A-C-IP-Address-Control) لینک 5 (http://social.msdn.microsoft.com/Forums/windows/en-US/35ea8aeb-e729-474c-b6d2-544fc3c48d8d/how-to-change-a-c-winform-screen-savers-title-friendly-name-in-display-properties) لینک6 (http://www.dotnetobject.com/Thread-Get-router-ip-address-in-winfom-c)

ghasemloo
شنبه 09 شهریور 1392, 17:41 عصر
khokhanدوست داریم!!!khokhanدوست داریم!!!khokhanدوست داریم