我們做一些軟件的時候常常要用到換IP的操作,其實簡單的換IP的方法就是重新撥號啊,下面就是我實踐成功的重新撥號的代碼,很簡單的,是一個單獨的類。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;//診斷,調用進程
namespace QQ號批量注冊
{
class ADSLHelper
{
public void Connect(string connectionName, string user, string pass)
{
string arg = string.Format("rasdial \"{0}\" {1} {2}", connectionName, user, pass);
InvokeCmd(arg);
}
public void Disconnect(string connectionName)
{
string arg = string.Format("rasdial \"{0}\" /disconnect", connectionName);
InvokeCmd(arg);
}
public static string InvokeCmd(string cmdArgs)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine(cmdArgs);
p.StandardInput.WriteLine("exit");
return p.StandardOutput.ReadToEnd();
}
}
}