How to find the windows dialing rules in .NET
Asked Answered
P

3

9

This should be simple, but isn't apparently. Since..Windows 3 or so, there is a control panel called Phone or Phone & Modem. In that control panel is a bunch of information about how a modem would dial up, assuming you have a modem hooked up. For example, do you need to dial 9 to get out, what is the area code, and so forth. How can i access this information programmatically? I am using C# .NET 2010.

Publicity answered 8/12, 2011 at 22:41 Comment(0)
F
8

I couldn't find a way to access it through a .Net TAPI wrapper (after a not so long search) so I fired up procmon an found where it was stored in the registry, and here's the code that accesses it (you can adapt it to your specific needs):

RegistryKey locationsKey =
    Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Telephony\Locations");
if (locationsKey == null) return;
string[] locations = locationsKey.GetSubKeyNames();
foreach (var location in locations)
{
    RegistryKey key = locationsKey.OpenSubKey(location);
    if (key == null) continue;
    Console.WriteLine("AreaCode {0}",key.GetValue("AreaCode"));
    Console.WriteLine("Country {0}",(int) key.GetValue("Country"));
    Console.WriteLine("OutsideAccess {0}", key.GetValue("OutsideAccess"));
}

Note :

  1. I recommend to use an official API if there is a .net compatible one.
  2. This code is not guaranteed to work on other OSes than Win 7
  3. If you need to prompt the user to fill in these details you can start the configuration tool using :

Process.Start(@"C:\Windows\System32\rundll32.exe",@"C:\Windows\System32\shell32.dll,Control_RunDLL C:\Windows\System32\telephon.cpl");

Fioritura answered 9/12, 2011 at 0:12 Comment(0)
M
12

You are going to need to use Tapi in Windows or pull the information from the registry. According to Microsoft Tapi 3.0 was not designed to be used from managed code, though the first link seems to have done it.

Some articles to look at:

  1. Tapi3.0 Application Development
  2. VB.Net accessing TAPI Dialing Rules

From Link #2

Take a look at these TAPI functions:

  1. lineGetTranslateCaps
  2. lineTranslateAddress
  3. lineTranslateDialog
  4. lineSetCurrentLocation
  5. lineGetCountry
  6. tapiGetLocationInfo

The info is stored in the Registry at: HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Telephony\Locations

Moss answered 8/12, 2011 at 23:50 Comment(0)
F
8

I couldn't find a way to access it through a .Net TAPI wrapper (after a not so long search) so I fired up procmon an found where it was stored in the registry, and here's the code that accesses it (you can adapt it to your specific needs):

RegistryKey locationsKey =
    Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Telephony\Locations");
if (locationsKey == null) return;
string[] locations = locationsKey.GetSubKeyNames();
foreach (var location in locations)
{
    RegistryKey key = locationsKey.OpenSubKey(location);
    if (key == null) continue;
    Console.WriteLine("AreaCode {0}",key.GetValue("AreaCode"));
    Console.WriteLine("Country {0}",(int) key.GetValue("Country"));
    Console.WriteLine("OutsideAccess {0}", key.GetValue("OutsideAccess"));
}

Note :

  1. I recommend to use an official API if there is a .net compatible one.
  2. This code is not guaranteed to work on other OSes than Win 7
  3. If you need to prompt the user to fill in these details you can start the configuration tool using :

Process.Start(@"C:\Windows\System32\rundll32.exe",@"C:\Windows\System32\shell32.dll,Control_RunDLL C:\Windows\System32\telephon.cpl");

Fioritura answered 9/12, 2011 at 0:12 Comment(0)
C
0

Bit more code to get the prefix

class Program
{
    static void Main(string[] args)
    {
        string rootLocation = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Telephony\Locations";
        getRegistryValues(rootLocation);
        Console.ReadLine();
    }

    public static void getRegistryValues(string rootLocation)
    {
        RegistryKey locationsKey =
        Registry.LocalMachine.OpenSubKey(rootLocation);
        if (locationsKey == null) return;
        string[] locations = locationsKey.GetSubKeyNames();
        Console.WriteLine(locations.Length.ToString());
        foreach (var location in locations)
        {
            Console.WriteLine(location.ToString());
            RegistryKey key = locationsKey.OpenSubKey(location);
            if (key == null) continue;
            foreach (string keyName in key.GetValueNames())
            {                  

                if (keyName.Equals("Prefixes"))
                {
                    string[] Prefixes = ((string[])(key.GetValue(keyName)));
                    Console.Write("Prefixes ");
                    foreach (string prefix in Prefixes)
                    {
                        Console.Write(prefix);
                    }

                }
                else
                {
                    Console.WriteLine(keyName + " {0}", key.GetValue(keyName));
                }

            }

            getRegistryValues(rootLocation+@"\"+location);


        }

    }
Catina answered 8/1, 2014 at 21:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.