C# 调用Windows拨号程序

lsj_zrp 2008-05-07 04:48:39
请问如何调用windows的拨号程序拨号上网,空闲5分钟后自动断网
最好能有示例代码,谢谢!
...全文
91 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lsj_zrp 2008-05-08
  • 打赏
  • 举报
回复
试一下先
bb_chen 2008-05-07
  • 打赏
  • 举报
回复
Mark
ericzhangbo1982111 2008-05-07
  • 打赏
  • 举报
回复
PS:你一定要有一个拨号的连接...
红色的就是你拨号连接的名字。
你可以修改一下。
加一个连接字符串进去
public void Connect(string connectionStr)
{
Int32 DResult = 0;

DResult = InternetDial(IntPtr.Zero, "My Connection",
Convert.ToInt32(DialUpOptions.INTERNET_DIAL_UNATTENDED), ref
mlConnection, 0);
ericzhangbo1982111 2008-05-07
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
public class Dialer
{

[System.Runtime.InteropServices.DllImport("wininet.dll",
EntryPoint = "InternetGetConnectedState", ExactSpelling = true,
CharSet = System.Runtime.InteropServices.CharSet.Ansi,
SetLastError = true)]
private static extern bool InternetGetConnectedState(ref Int32
lpdwFlags, Int32 dwReserved);

[System.Runtime.InteropServices.DllImport("Wininet.dll",
EntryPoint = "InternetDial", ExactSpelling = true,
CharSet = System.Runtime.InteropServices.CharSet.Ansi,
SetLastError = true)]
private static extern Int32 InternetDial(IntPtr hwndParent, string
lpszConnectoid, Int32 dwFlags, ref Int32 lpdwConnection, Int32
dwReserved);

[System.Runtime.InteropServices.DllImport("Wininet.dll",
EntryPoint = "InternetHangUp", ExactSpelling = true,
CharSet = System.Runtime.InteropServices.CharSet.Ansi,
SetLastError = true)]
private static extern Int32 InternetHangUp(Int32 lpdwConnection,
Int32 dwReserved);

private enum Flags : int
{
//Local system uses a LAN to connect to the Internet.
INTERNET_CONNECTION_LAN = 0X2,
//Local system uses a modem to connect to the Internet.
INTERNET_CONNECTION_MODEM = 0X1,
//Local system uses a proxy server to connect to the Internet.
INTERNET_CONNECTION_PROXY = 0X4,
//Type Visual Basic 6 code here...

//Local system has RAS installed.
INTERNET_RAS_INSTALLED = 0X10
}

//Declaration Used For InternetDialUp.
private enum DialUpOptions : int
{
INTERNET_DIAL_UNATTENDED = 0X8000,
INTERNET_DIAL_SHOW_OFFLINE = 0X4000,
INTERNET_DIAL_FORCE_PROMPT = 0X2000
}

private const int ERROR_SUCCESS = 0X0;
private const int ERROR_INVALID_PARAMETER = 0X87;


private Int32 mlConnection;

public string GetConnectionType()
{
Int32 lngFlags = 0;

if (InternetGetConnectedState(ref lngFlags, 0))
{
//connected.
if ((lngFlags & (int)Flags.INTERNET_CONNECTION_LAN) != 0)
{
//LAN connection.
return "LAN connection.";
}
else if ((lngFlags & (int)Flags.INTERNET_CONNECTION_MODEM) != 0)
{
//Modem connection.
return "Modem connection.";
}
else if ((lngFlags & (int)Flags.INTERNET_CONNECTION_PROXY) != 0)
{
//Proxy connection.
return "Proxy connection.";
}
return "Not connected.";
}
else
{
//not connected.
return "Not connected.";
}
}
public void Connect()
{
Int32 DResult = 0;

DResult = InternetDial(IntPtr.Zero, "My Connection",
Convert.ToInt32(DialUpOptions.INTERNET_DIAL_UNATTENDED), ref
mlConnection, 0);

if (DResult == ERROR_SUCCESS)
Console.WriteLine("Dial Up Successful");
else
Console.WriteLine("UnSuccessFull Error Code");
}
public void Disconnect()
{
Int32 Result = 0;

if (!(mlConnection == 0))
{
Result = InternetHangUp(mlConnection, 0);
if (Result == 0)
Console.WriteLine("Hang up successful");
else
Console.WriteLine("Hang up NOT successful");
}
else
Console.WriteLine("You must dial a connection first!");
}
}

}

yagebu1983 2008-05-07
  • 打赏
  • 举报
回复
帮你顶!!

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧