c# - Search rasphone pbk file -
in application i'm using rasphone function connect vpn's when application launches gets vpn connections in combobox using 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 question how can phonenumer= section display in textbox or label, know ip is.
a pbk file looks (had delete rows), problem have multiple vpn connections in pbk file multiple phonenumer= entries.
[vpn name of connection] encoding=1 pbversion=3 type=2 device=vpn phonenumber= 0.0.0.0 <- ip address want display in label or textbox. areacode= countrycode=0 countryid=0 usedialingrules=0 comment= friendlyname= lastselectedphone=0 promotealternates=0 trynextalternateonfail=1
if looking simple solution , understand question correctly should trick, add following statement after current if
in foreach
statement
else if(str.contains("phonenumber")) { var x = str.split('='); if(x.length > 1) ip = x[1]; }
please note ip
variable store ip-address.
to answer question in comments , assuming have [vpn-connection] before each phonenumber entry write 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 item last added combobox , put before string of ip address, still simple hack 1 way it.. if have more advanced needs make class store data require , populate combobox that.
Comments
Post a Comment