Trying to Convert a PHP code to VB.net - vb.net

MY PHP Code is
$geourl = 'http://maps.google.com/maps/geo?key=' . $google_apikey .
'&output=json'. '&q=' . urlencode($_GET['url'] . ', USA').'&gl=us';
My ASPX code looks like this
Dim google_apikey As String="sdasdasd"
Dim geourl As String= "http://maps.google.com/maps/geo?key=" & google_apikey & _
"&output=json" & "&q=" & urlencode(request.QueryString("pc")& ", USA")"&gl=us"
The Error i get is Compiler Error Message: BC30205: End of statement expected.
Instead of Period (.) i have used & is it valid in vb.net, what is the problem with the above string, urlencode ?

Think you missed a concatenator right at the end. Should it be:
Dim geourl As String= "http://maps.google.com/maps/geo?key=" & google_apikey & _
"&output=json" & "&q=" & UrlEncode(request.QueryString("pc")& ", USA") & _
"&gl=us"

I would recommend you using the String.Format method to make your code more readable:
Dim google_apikey As String="sdasdasd"
Dim geourl As String = String.Format("http://maps.google.com/maps/geo?key={0}&output=json&q={1},USA&gl=us", UrlEncode(google_apikey), UrlEncode(Request.QueryString("pc")))

Related

Split text in all lines inside of a string

Originally the Keywords will we pulled out from a text file located on a web server.
Dim KeyWords As String = Split(Split(TempSMTPFile, "# Keywords #")(1), "# Keywords #")(0)
I want to create a function that will split out all the keywords inside a string
The keywords in this example will be:
' This list might be changed to more or less keywords.
Dim KeyWords As String = "[One=Test1]" & vbNewLine & "[Two=Test2]" & vbNewLine & "[Three=Test3]"
' I Need a function to split out all keywords and do below check between all splits.
If KeyWords.ToLower.Contains("[" & Splited_keyword & "=") Then ' One, Two, Three
MsgBox(Split(Split(KeyWords, "[" & Splited_keyword & "=")(1), "]")(0)) ' Test1, Test2, Test3
End If
' This should print OneTest1, TwoTest2, ThreeTest3
Thanks for your comments, I was able to solve this with this solution.
Dim str As String() = keywords.Split(New [Char]() {CChar(vbCrLf)})
For Each s As String In str
If s.Contains("[") Then
Dim SplittedKeyword = Split(Split(s, "[")(1), "=")(0)
If TextUserShortDescription.Text.ToLower.Contains(SplittedKeyword.ToLower) Then
MsgBox(SplittedKeyword & (Split(Split(s, "[" & SplittedKeyword & "=")(1), "]")(0)))
End If
End If
Next
This could help you (untested):
Dim keyValues as String()
keyValues = KeyWords.Split(Environment.NewLine)
For Each (keyvalue as string in keyValues)
MsgBox(keyvalue.SubString(1, keyvalue.IndexOf("["c) + " " + keyvalue.SubString(keyvalue.IndexOf("="c), keyvalue.LastIndexOf("]"c)
End For

VB.net get bitlocker Password ID from Active Directory

I have a VB.net program that I am trying to add a bitlocker lookup tool that will search active directory for the machine name, and display the "Password ID" as well as the "Recovery Password"
So far my script/code works flawlessly for the lookup and displaying the Recovery Password, but I cannot get it to display the Password ID.
I've tried:
Item.Properties("msFVE-RecoveryGuid")(0)
Which returns the error "System.InvalidCastException: Conversion from type 'Byte()' to type 'String' is not valid."
Item.Properties("msFVE-RecoveryGuid")(0).ToString
Which returns "System.Byte[]"
Item.Properties("msFVE-RecoveryGuid").ToString
Which returns "System.DirectoryServices.ResultPropertyValueCollection"
So far in my searching I've only seen C# examples, and I haven't been able to translate.
The same for Recovery Password works however:
(Item.Properties("msFVE-RecoveryPassword")(0))
Here is the larger snippet of what I have for context:
Dim RootDSE As New DirectoryEntry("LDAP://RootDSE")
Dim DomainDN As String = RootDSE.Properties("DefaultNamingContext").Value
Dim ADsearch As New DirectorySearcher("LDAP://" & DomainDN)
ADsearch.Filter = ("(&(objectClass=computer)(name=" & MachineName & "))")
Dim ADresult As SearchResult = ADsearch.FindOne
Dim ADpath As String = ADresult.Path
Dim BTsearch As New DirectorySearcher()
BTsearch.SearchRoot = New DirectoryEntry(ADpath)
BTsearch.Filter = "(&(objectClass=msFVE-RecoveryInformation))"
Dim BitLockers As SearchResultCollection = BTsearch.FindAll()
Dim Item As SearchResult
Dim longTempstring As String = ""
For Each Item In BitLockers
If Item.Properties.Contains("msFVE-RecoveryGuid") Then
Dim tempstring As String = Item.Properties("msFVE-RecoveryGuid")(0).ToString
longTempstring = longTempstring & tempstring & vbNewLine
'ListBox2.Items.Add(Item.Properties("msFVE-RecoveryGuid")(0))
End If
If Item.Properties.Contains("msFVE-RecoveryPassword") Then
ListBox1.Items.Add(Item.Properties("msFVE-RecoveryPassword")(0))
End If
Next
MsgBox(longTempstring)
So I figured out that I needed to convert the bytes to hex in order to get them to match what is viewed in the Microsoft Management Console. Once I began doing that the only problem I ran into is that I discovered the indexing of the byte arrays are not in the same order as they are in Active Directory. -- so instead of looping I had to list out each index of the Byte array and sort them to their proper positions so that they match how they show up in AD.
My end function is:
Function bitread(ByVal GUID As Byte())
Dim tempVar As String
tempVar = GUID(3).ToString("X02") & GUID(2).ToString("X02") _
& GUID(1).ToString("X02") & GUID(0).ToString("X02") & "-" _
& GUID(5).ToString("X02") & GUID(4).ToString("X02") & "-" _
& GUID(7).ToString("X02") & GUID(6).ToString("X02") & "-" _
& GUID(8).ToString("X02") & GUID(9).ToString("X02") & "-" _
& GUID(10).ToString("X02") & GUID(11).ToString("X02") _
& GUID(12).ToString("X02") & GUID(13).ToString("X02") _
& GUID(14).ToString("X02") & GUID(15).ToString("X02")
Return tempVar
End Function
Called with:
bitread(Item.Properties("msFVE-RecoveryGUID")(0))

Writing to textfile not working, no text produced

PupilID = "\" & PupilID & ".txt"
If Dir$(PupilID) = "" Then
Dim swpupilinfo As New StreamWriter(Application.StartupPath & PupilID, True)
swpupilinfo.Close()
End If
If System.IO.File.Exists(filename) = True Then
readerlinecount = File.ReadAllLines(Dir$(PupilID))
End If
Dim objwritere As New System.IO.StreamWriter(filename, True)
objwritere.WriteLine(quiztaken & " correct answers " & correct & " wrong answers " & wrong & " total mark " & totalmark)
objwritere.Close()
This is what I am using to write to the text file, all variables have values but when I open the .txt file it contains nothing.
Not that great at programming just going off what my teacher gave me.
Half of the code you have provided does not seem to relate to the question you have asked? Below is a simple implementation of writing to a text file.
You seem to be using pupilId and fileName as file path locations which could be causing some confusion?
StreamWriter which implements IDisposable via TextWriter, I recommend making use of it. See below
Dim pupilId As Integer = 1
Dim path As String = String.Format("\\{0}.txt", pupilId)
Using sw As New StreamWriter(filePath,True)
sw.WriteLine("Whatever text you want here")
End Using

Retrieve certain data from XML File

My XML File:
<?xml version="1.0" encoding="utf-8"?>
<DisksLibrary>
<Disk>
<DiskName>Test</DiskName>
<DiskPath>testpath</DiskPath>
</Disk>
<Disk>
<DiskName>Test1</DiskName>
<DiskPath>testpath1</DiskPath>
</Disk>
<Disk>
<DiskName>Test2</DiskName>
<DiskPath>testpath2</DiskPath>
</Disk>
</DisksLibrary>
I was wondering since I have multiple tags with the same name such as and in My XML File, is it possible to retrieve a particular set of data from the XML File? For example, if I want to retrieve the DiskPath innertext which is under the same set as the one with the InnerText of "Test". In other words, the DiskPath I should retrieve is "testpath1". How do I ensure it does not confuse with other DiskPath innertext?
I tried searching online but either no solution was provided or the solution provided does not work for me. I found one solution which seem to work but after reading the code I don't think it works if there are multiple tags with the same name.
The solution I found which I am not sure if it resolves my problem is:
Dim document As XmlReader = New XmlTextReader("MyXML.xml")
While (document.Read())
Dim type = document.NodeType
If (type = XmlNodeType.Element) Then
If (document.Name = "FirstName") Then
TextBox1.Text = document.ReadInnerXml.ToString()
End If
If (document.Name = "LastName") Then
TextBox2.Text = document.ReadInnerXml.ToString()
End If
End If
End While
Thank you for your help. Appreciate it.
Reading about XPath and Predicates (http://www.w3schools.com/xpath/xpath_syntax.asp) would be very useful to you. In your instance you want to return the inner text of any "DiskPath" elements whose sibling "DiskName" has a value of "Test". As Styxxy described the XPath /DisksLibrary/Disk[DiskName="Test"]/DiskPath would achieve this:
Dim doc As New XmlDocument
Dim targetPath As String
doc.Load("MyXML.xml")
Dim foundNode As XmlNode = doc.SelectSingleNode("/DisksLibrary/Disk[DiskName='Test']/DiskPath")
If Not (foundNode Is Nothing) Then
targetPath = foundNode.InnerText
End If
I would consider parameterizing the XPath approach to make it scalable.
Imports System.Xml
Module Module1
Sub Main()
Dim xml As String = _
"<?xml version=""1.0"" encoding=""utf-8""?>" & _
"<DisksLibrary>" & _
" <Disk>" & _
" <DiskName>Test1</DiskName>" & _
" <DiskPath>testpath1</DiskPath>" & _
" </Disk>" & _
" <Disk>" & _
" <DiskName>Test2</DiskName>" & _
" <DiskPath>testpath2</DiskPath>" & _
" </Disk>" & _
" <Disk>" & _
" <DiskName>Test3</DiskName>" & _
" <DiskPath>testpath3</DiskPath>" & _
" </Disk>" & _
"</DisksLibrary>"
Dim doc As New XmlDocument
doc.LoadXml(xml)
For i = 1 To 4
Dim foundNode = FindSiblingNode(doc, "Test" & i, "DiskPath")
Console.WriteLine(If(Not foundNode Is Nothing, foundNode.InnerText, "Node not found"))
Next
Console.ReadLine()
End Sub
Public Function FindSiblingNode(ByVal doc As XmlDocument, _
ByVal siblingNodeInnerText As String, _
ByVal searchNodeName As String) As XmlNode
Return doc.SelectSingleNode(String.Format("/DisksLibrary/Disk[DiskName='{0}']/{1}", siblingNodeInnerText, searchNodeName))
End Function
End Module
Results:
testpath1
testpath2
testpath3
Node not found
Here's another option using LINQ to XML and XML literals:
Dim document = XDocument.Load("MyXML.xml")
Dim path As String = (
From disk In document...<Disk>
Where disk.<DiskName>.Value = "Test"
Select disk.<DiskPath>.Value
).SingleOrDefault()
path will either be the value of the matching <DiskPath> element, or Nothing if not found.

DeleteFolder Environ gives an error "Path not found"

here is a portion of the code:
oFSO.DeleteFolder Environ("C:\Users\%USERNAME%\AppData\Local\Temp") & "\* " & oFSO.GetFile(strZipFile).Name, True
when i try to execute it it gives me this error : "Path not found"
Use
oFSO.DeleteFolder _
Environment.ExpandEnvironmentVariables("C:\Users\%USERNAME%\AppData\Local\Temp") & _
......
or use a complicated string concatenation (without the % around the environment variable)
oFSO.DeleteFolder _
"C:\Users\" & Environ("USERNAME") & "\AppData\Local\Temp") & "\* " ....
However when dealing with this kind of paths, the best approach is to use Environment class
Dim userData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Dim tempFolder = Path.Combine(userData, "temp")
Now the rest of your path seems to be a bit wrong.
"* " (a space after the wild card?) followed by a filename doesn't seems to be correct)