用c#实现拨号
沿袭上一篇的猥琐方法,贴一段用c#拨号的代码
1: class AutoDialer
2: {
3: public void Connect(string connectionName, string user, string pass)
4: {
5: string arg = string.Format("rasdial \"{0}\" {1} {2}", connectionName, user, pass);
6: InvokeCmd(arg);
7: }
8:
9: public void Disconnect(string connectionName)
10: {
11: string arg = string.Format("rasdial \"{0}\" /disconnect", connectionName);
12: InvokeCmd(arg);
13: }
14:
15: private static string InvokeCmd(string cmdArgs)
16: {
17: Process p = new Process();
18: p.StartInfo.FileName = "cmd.exe";
19: p.StartInfo.UseShellExecute = false;
20: p.StartInfo.RedirectStandardInput = true;
21: p.StartInfo.RedirectStandardOutput = true;
22: p.StartInfo.RedirectStandardError = true;
23: p.StartInfo.CreateNoWindow = true;
24: p.Start();
25:
26: p.StandardInput.WriteLine(cmdArgs);
27: p.StandardInput.WriteLine("exit");
28:
29: return p.StandardOutput.ReadToEnd();
30: }
31: }
写这段代码之前,也g了一把,不甚满意,大多数是人云亦云,且所提供代码一般没虾米用处,不过这篇可以参考一下
至于用法嘛,很简单了,而且,最关键的是,完全不需要手动再去点击拨号连接


浙公网安备 33010602011771号