该如何获得当前计算机的代理服务器地址及端口?

发财蛇 2004-10-14 04:51:34
~
...全文
458 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
felixwoo 2004-10-15
  • 打赏
  • 举报
回复
可以用WMI,但推荐使用注册表,因为效率要高。下面把两种方法都告诉你。

WMI:
string proxyServer = null;
ManagementClass mc = new ManagementClass("Win32_Proxy");
ManagementObjectCollection moc = mc.GetInstances();
foreach(ManagementObject mo in moc)
{
proxyServer=(string)mo["ProxyServer"]+":"+(string)mo["ProxyPortNumber"];
}

注册表:
private RegistryKey _InternetSettings = currentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true);
string value = (string)_InternetSettings.GetValue( "ProxyServer", string.Empty );
_InternetSettings.Close();

用WMI可分别获得代理地址和端口,用注册表获得的是代理的完整地址(地址:端口)。
saucer 2004-10-15
  • 打赏
  • 举报
回复
use System.Net.WebProxy's GetDefaultProxy method, for example

System.Net.WebProxy wp = System.Net.WebProxy.GetDefaultProxy();
Uri u = wp.Address;
Console.WriteLine(u.ToString());
Console.WriteLine(u.Host);
Console.WriteLine(u.Port);
发财蛇 2004-10-15
  • 打赏
  • 举报
回复
ding~
mbh0210 2004-10-15
  • 打赏
  • 举报
回复
学习中。。。。。。。。。
happyRose 2004-10-15
  • 打赏
  • 举报
回复
都给人抢先说了。顶一下吧
cancersyf 2004-10-15
  • 打赏
  • 举报
回复
Public Function GetProxyServerName() As String

Dim UseProxy As New Net.WebProxy()
Try 'if no proxy is specified, an exception is
'thrown by the frameworks and must be caught

Return UseProxy.GetDefaultProxy.Address.Host

Catch 'catch the error when no proxy is specified in IE

Return "Not Specified"

End Try

End Function


Public Function GetProxyServerPort() As String

Dim UseProxy As New Net.WebProxy()

Try 'if no proxy is specified, an exception is
'thrown by the frameworks and must be caught

Return UseProxy.GetDefaultProxy.Address.Port

Catch 'catch the error when no proxy is specified in IE

Return "Not Specified"

End Try

End Function
wangxt 2004-10-14
  • 打赏
  • 举报
回复
帮你顶
dahuzizyd 2004-10-14
  • 打赏
  • 举报
回复
试试WMI吧,
http://www.cnblogs.com/dahuzizyd/archive/2004/08/17/34058.aspx
后面评论里提供的程序也不错

110,536

社区成员

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

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

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