Results 1 to 6 of 6

Thread: Process.Start () multiple arguments and saving output in VB.Net

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2020
    Posts
    14

    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?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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:
    1. Dim s1 As String = "curl ipinfo.io " & " "
    2. Dim s2 As String = " " & " C:\Users\%Username%\myData\IPAddress.txt"
    3. Dim b As Boolean = s1 > s2
    4.  
    5. 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:
    1. 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:
    1. 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.

  3. #3
    Fanatic Member
    Join Date
    Jun 2019
    Posts
    557

    Re: Process.Start () multiple arguments and saving output in VB.Net

    Use .NET console redirect:
    VB.NET Code:
    1. Dim psi = New ProcessStartInfo("curl.exe")
    2. psi.UseShellExecute = False
    3. psi.RedirectStandardOutput = True
    4. psi.Arguments = "ipinfo.io"
    5. Dim p = New Process()
    6. p.StartInfo = psi
    7. p.Start()
    8.  
    9. Dim rdr = p.StandardOutput
    10. Dim output = rdr.ReadToEnd()


    Or just use the web API provided by ipinfo.io:
    VB.NET Code:
    1. Private Function GetIPInfoFromIpInfoIO() As String
    2.     Using http = New WebClient()
    3.         Return http.DownloadString("http://ipinfo.io")
    4.     End Using
    5. End Function
    6.  
    7. Sub Main()
    8.     Console.WriteLine(GetIPFromIpInfoIO())
    9.  
    10.     Console.WriteLine()
    11.     Console.WriteLine("Press Enter to exit...")
    12.     Console.ReadLine()
    13. End Sub

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Dec 2020
    Posts
    14

    Re: Process.Start () multiple arguments and saving output in VB.Net

    Quote Originally Posted by jmcilhinney View Post
    That first code makes no sense at all. Do you know what that is actually doing?
    vb.net Code:
    1. Dim s1 As String = "curl ipinfo.io " & " "
    2. Dim s2 As String = " " & " C:\Users\%Username%\myData\IPAddress.txt"
    3. Dim b As Boolean = s1 > s2
    4.  
    5. 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:
    1. 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:
    1. 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.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  6. #6
    Administrator Steve R Jones's Avatar
    Join Date
    Apr 2012
    Location
    Largo, FL.
    Posts
    1,826

    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
  •  



Click Here to Expand Forum to Full Width