Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 我如何判断连接是否按流量计费?_.net_Windows_Network Programming - Fatal编程技术网

.net 我如何判断连接是否按流量计费?

.net 我如何判断连接是否按流量计费?,.net,windows,network-programming,.net,Windows,Network Programming,我写了一个监控IMAP电子邮件帐户的程序。它在我随身携带的笔记本电脑上按时间表运行。有时,当我的互联网连接通过我的移动设备时,它会运行,移动设备有一个按流量计费的连接(也就是说,我按GB付费),我不想让它运行,因为它占用了大量带宽,而且它可以等到带宽空闲时再运行 因此,问题是:.NET程序如何确定何时对其使用的连接进行计量?通过对MSDN的简短搜索,找到了NetworkInformation.GetInternetConnectionProfile函数。看起来它是Metro接口的正式组成部分,但

我写了一个监控IMAP电子邮件帐户的程序。它在我随身携带的笔记本电脑上按时间表运行。有时,当我的互联网连接通过我的移动设备时,它会运行,移动设备有一个按流量计费的连接(也就是说,我按GB付费),我不想让它运行,因为它占用了大量带宽,而且它可以等到带宽空闲时再运行


因此,问题是:.NET程序如何确定何时对其使用的连接进行计量?

通过对MSDN的简短搜索,找到了NetworkInformation.GetInternetConnectionProfile函数。看起来它是Metro接口的正式组成部分,但我听说桌面应用程序可以访问大多数Metro库。

在2021年,您可以使用powershell.NET framework和类似的注册表项来测试您的以太网连接是否设置为计量:

$definition = @"
using System;
using System.Runtime.InteropServices;

namespace Win32Api
{

    public class NtDll
    {
        [DllImport("ntdll.dll", EntryPoint="RtlAdjustPrivilege")]
        public static extern int RtlAdjustPrivilege(ulong Privilege, bool Enable, bool CurrentThread, ref bool Enabled);
    }
}
"@

Add-Type -TypeDefinition $definition -PassThru | Out-Null
[Win32Api.NtDll]::RtlAdjustPrivilege(9, $true, $false, [ref]$false) | Out-Null

#Setting ownership to Administrators
$key = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\DefaultMediaCost",[Microsoft.Win32.RegistryKeyPermissionCheck]::ReadWriteSubTree,[System.Security.AccessControl.RegistryRights]::takeownership)
$acl = $key.GetAccessControl()
# Use your account "Administrators" or any other which is actually an existing one on your account
$acl.SetOwner([System.Security.Principal.NTAccount]"Administrators")
$key.SetAccessControl($acl)

#Giving Administrators full control to the key
$rule = New-Object System.Security.AccessControl.RegistryAccessRule ([System.Security.Principal.NTAccount]"Administrators","FullControl","Allow")
$acl.SetAccessRule($rule)
$key.SetAccessControl($acl)
$path = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\DefaultMediaCost"
# you can check for "Ethernet" "Wifi" "4G" etc.
$name = "Ethernet"
#If the Value is 2 = metered
New-ItemProperty -Path $path -Name $name -Value "2" -PropertyType DWORD -Force | Out-Null
如果出现以下错误:

Exception calling "SetAccessRule" with "1" argument(s): "Some or all identity references could not be translated.".

尝试将“Administrators”用户更改为您帐户上存在且具有管理员权限的任何其他用户。

这是Windows 8吗?我认为Windows 8有一个可以在连接上设置的“is metered”属性,但我在7或更早的版本中没有看到。如果在任意网络连接的情况下可以设置,我会非常惊讶。@Rup:这不是Windows业务。不像它联系ISP并要求提供链接类型。@Xaqron,但Windows知道它是使用wifi还是使用USB调制解调器,而这正是他所要求的。为什么要问ISP?我没想到这会这么复杂。在Windows 8中,您可以手动指定按流量计费的连接。该标志必须存储在某个地方,并通过API或直接注册表读取。我不想建立一个使用启发式或任何东西,只是阅读手册设置。嗯,恭喜你找到了一个解决方案,但我不喜欢必须更改注册表权限的想法。一定有别的办法找到这个。