0

In my application I'm using the rasphone function to connect to vpn's When my application launches it gets all the vpn connections in a combobox using this code.

String f = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Microsoft\Network\Connections\Pbk\rasphone.pbk";

        if (System.IO.File.Exists(f))
        {
            List<string> lines = new List<string>();

            using (StreamReader r = new StreamReader(f))
            {
                string line;
                while ((line = r.ReadLine()) != null)
                {

                    lines.Add(line);
                }
            }

            foreach (string s in lines)
            {
                if (s.StartsWith("["))
                {
                    char[] MyChar = { ']' };
                    string NewString = s.TrimEnd(MyChar);
                    char[] MyChar2 = { '[' };
                    string NewString2 = NewString.TrimStart(MyChar2);
                    comboBox1.Items.Add(NewString2);
                }
            }
        }
        else
        {
            MessageBox.Show("PBK File not found.");
        }
        comboBox1.Sorted = true;

Now my question is how I can also get the phonenumer= section to display in a textbox or label, so I know what the IP is.

A pbk file looks like this (had to delete some rows), the problem is that I have multiple vpn connections in the pbk file so also multiple phonenumer= entries.

[VPN Name of connection]
Encoding=1
PBVersion=3
Type=2


DEVICE=vpn
PhoneNumber= 0.0.0.0 <- ip address I want to display in a label or textbox.
AreaCode=
CountryCode=0
CountryID=0
UseDialingRules=0
Comment=
FriendlyName=
LastSelectedPhone=0
PromoteAlternates=0
TryNextAlternateOnFail=1
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
PandaNL
  • 828
  • 5
  • 18
  • 40

3 Answers3

1

If you are looking for a very simple solution and I understand your question correctly this should do the trick, add the following statement after your current if in your foreach statement

else if(str.Contains("PhoneNumber"))
{
    var x = str.Split('=');
    if(x.Length > 1)
    ip = x[1];
}

Please note that ip is the variable were you would like to store your IP-address.

To answer your question in the comments and assuming that you always have a [VPN-Connection] before each PhoneNumber entry you could write something like this

        foreach (string s in lines)
        {
            if (s.StartsWith("["))
            {
                char[] MyChar = { ']' };
                string NewString = s.TrimEnd(MyChar);
                char[] MyChar2 = { '[' };
                string NewString2 = NewString.TrimStart(MyChar2);
                comboBox1.Items.Add(NewString2);
            }
            else if (s.Contains("PhoneNumber"))
            {
                string ip = comboBox1.Items[comboBox1.Items.Count - 1].ToString() + " : ";
                var x = s.Split('=');
                if (x.Length > 1)
                    ip += x[1];
            }
        }

This would get the item that were last added to the combobox and put it before the string of the ip address, still just a simple hack but it is one way to do it.. if you have more advanced needs I would make a class to store the data that you require and the populate the combobox from that.

Karl-Henrik
  • 1,113
  • 1
  • 11
  • 17
  • how can I make this so it starts searching for Phonenumber= ipadress in the pbk file after the given [vpn connection]? – PandaNL Jun 27 '14 at 11:50
0

well. you could read the file contents and use regex to extract it with

PhoneNumber=(?<ip>[^\n]+)
Dbl
  • 5,634
  • 3
  • 41
  • 66
0

You can p/invoke RasGetEntryProperties passing it the pbk file, or you can simply parse out the value from the text file. Its in INI format and there are many INI File reader classes out there.

Community
  • 1
  • 1
Alex K.
  • 171,639
  • 30
  • 264
  • 288