-
Dec 17th, 2020, 09:43 AM
#1
Thread Starter
Junior Member
Process.Start () multiple arguments and saving output in VB.Net
Hi,
I am using following command line to get and save the IP Address information in Windows 10.
curl ipinfo.io >C:\Users\%Username%\myData\IPAddress.txt
In VB, I have tried Process.Start() but that seems to be not working I think due to ">" symbol as:
Code:
Process.Start("curl ipinfo.io " & " " > " " & " C:\Users\%Username%\myData\IPAddress.txt")
Used following approach as well but no luck as well:
Code:
Dim getIPinfo As New ProcessStartInfo("curl")
getIPinfo.Arguments = "ipinfo.io > C:\Users\%Username%\myData\IPAddress.txt"
Process.Start(getIPinfo)
Any thoughts?
-
Dec 17th, 2020, 09:59 AM
#2
Re: Process.Start () multiple arguments and saving output in VB.Net
That first code makes no sense at all. Do you know what that is actually doing?
vb.net Code:
Dim s1 As String = "curl ipinfo.io " & " "
Dim s2 As String = " " & " C:\Users\%Username%\myData\IPAddress.txt"
Dim b As Boolean = s1 > s2
Process.Start(b)
That wouldn't even compile with Option Strict On and that's a perfect example of why it should be On. It will alert you to abominations like that code. It makes no sense at all to concatenate multiple String literals. That code should have been simply:
vb.net Code:
Process.Start("curl ipinfo.io > C:\Users\%Username%\myData\IPAddress.txt")
but even that's wrong. If you'd read the documentation then you'd know that the file name and arguments must be provided separately:
vb.net Code:
Process.Start("curl", "ipinfo.io > C:\Users\%Username%\myData\IPAddress.txt")
That's basically equivalent to what you have in the second code snippet, so it may still not work, but at least it makes sense.
As for the issue with the second code, you need to tell us what actually happens. Just as a doctor uses symptoms to diagnose illness, developers use symptoms to diagnose bugs. What actually happens and how it differs to what you expect is an indication of what's wrong with your code.
-
Dec 17th, 2020, 10:17 AM
#3
Re: Process.Start () multiple arguments and saving output in VB.Net
Use .NET console redirect:
VB.NET Code:
Dim psi = New ProcessStartInfo("curl.exe")
psi.UseShellExecute = False
psi.RedirectStandardOutput = True
psi.Arguments = "ipinfo.io"
Dim p = New Process()
p.StartInfo = psi
p.Start()
Dim rdr = p.StandardOutput
Dim output = rdr.ReadToEnd()
Or just use the web API provided by ipinfo.io:
VB.NET Code:
Private Function GetIPInfoFromIpInfoIO() As String
Using http = New WebClient()
Return http.DownloadString("http://ipinfo.io")
End Using
End Function
Sub Main()
Console.WriteLine(GetIPFromIpInfoIO())
Console.WriteLine()
Console.WriteLine("Press Enter to exit...")
Console.ReadLine()
End Sub
-
Dec 18th, 2020, 03:09 AM
#4
Thread Starter
Junior Member
Re: Process.Start () multiple arguments and saving output in VB.Net
Originally Posted by jmcilhinney
That first code makes no sense at all. Do you know what that is actually doing?
vb.net Code:
Dim s1 As String = "curl ipinfo.io " & " "
Dim s2 As String = " " & " C:\Users\%Username%\myData\IPAddress.txt"
Dim b As Boolean = s1 > s2
Process.Start(b)
That wouldn't even compile with Option Strict On and that's a perfect example of why it should be On. It will alert you to abominations like that code. It makes no sense at all to concatenate multiple String literals. That code should have been simply:
vb.net Code:
Process.Start("curl ipinfo.io > C:\Users\%Username%\myData\IPAddress.txt")
but even that's wrong. If you'd read the documentation then you'd know that the file name and arguments must be provided separately:
vb.net Code:
Process.Start("curl", "ipinfo.io > C:\Users\%Username%\myData\IPAddress.txt")
That's basically equivalent to what you have in the second code snippet, so it may still not work, but at least it makes sense.
As for the issue with the second code, you need to tell us what actually happens. Just as a doctor uses symptoms to diagnose illness, developers use symptoms to diagnose bugs. What actually happens and how it differs to what you expect is an indication of what's wrong with your code.
Please try to focus on the question asked and if you don't understand, leave it for others but avoid being judgmental if question /code makes scenes or not (it's rude) and no one if forcing you to leave a response. No more comments, thanks for understanding.
-
Dec 18th, 2020, 04:48 AM
#5
Re: Process.Start () multiple arguments and saving output in VB.Net
Or, I'll post what I see fit, when I see fit and you can ignore it if you don't like it. I'm not inclined to concern myself with whether someone's ego is so fragile that they take a critique of their code as a judgement about them as a person. I'm concerned with people who post here being the best developers they can be and I will post as I see fit with that in mind. Thanks for understanding.
-
Dec 18th, 2020, 06:04 AM
#6
Re: Process.Start () multiple arguments and saving output in VB.Net
-OSD- -> Have you noticed jmcilhinney's Join Date and Post Count.... He didn't get to this point by accident.
I'm going to close this thread... If you'd like to start over here or somewhere else is your call.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|