c#ADSL拨号类

时间:2022-02-25 04:49:50

class ADSLHelper
{
/// <summary>
///拨号
/// </summary>
/// <param></param>
/// <param></param>
/// <param></param>
/// <returns></returns>
public string Connect(string connectionName, string user, string pass)
{
string arg = string.Format("rasdial \"{0}\" {1} {2}", connectionName, user, pass);
return InvokeCmd(arg);
}
/// <summary>
/// 断开
/// </summary>
/// <param></param>
/// <returns></returns>
public string Disconnect(string connectionName)
{
string arg = string.Format("rasdial \"{0}\" /disconnect", connectionName);
return 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();