We help IT Professionals succeed at work.

VB.Net execute commandline with process.start

Hi experts,

I can't get this to create the dump file. Even though it works when i paste it in directly from a command prompt.

Essentially, it is a VB.NET winform that executes cmd.exe with arguments which creates a txt output file.
       


       Dim ProcessProperties As New ProcessStartInfo

       ProcessProperties.FileName = "cmd.exe"
        ProcessProperties.Arguments = "/C " & Chr(34) & NetworkSharePath _
            & "code" & Chr(34) & " " & ermCode & " " & Chr(34) & NetworkSharePath & "\" & org & "\" & Year & "\" & Year & "-" & Month & "\val_" & org & ".txt" & Chr(34) & " " & Chr(34) & NetworkSharePath & "\" _
            & org & "\" & Year & "\" & Year & "-" & Month & "\map_" & org & ".txt" & Chr(34) & " " & Chr(34) & File_Sample & Chr(34) & " >> " & Chr(34) & File_CreatedKey & Chr(34)

        ' start the process in a hidden window
        ProcessProperties.WindowStyle = ProcessWindowStyle.Hidden
        ProcessProperties.CreateNoWindow = True
        ProcessProperties.UseShellExecute = False
        Process.Start(ProcessProperties)


NOTE: The giant string for args works fine when i open CMD.exe and past it in to test.

What am i doing wrong here?
Hades666
Comment
Watch Question

Change this
ProcessProperties.FileName = "cmd.exe"
to
ProcessProperties.FileName = "C:\Windows\System32\cmd.exe" test and let us know, if it works or not.
Brad HoweDevOps Manager
Top Expert 2011

Author

Commented:
No luck - Hades666
Inorder to know whats happening redirect the output to console.
here is how to do it but in C#

Process compiler = new Process();
compiler.StartInfo.UseShellExecute = false;
compiler.StartInfo.RedirectStandardOutput = true;
compiler.Start();    
Console.WriteLine(compiler.StandardOutput.ReadToEnd());
compiler.WaitForExit();

Try if you can see any output in the console.
Brad HoweDevOps Manager
Top Expert 2011

Author

Commented:
The window closes to fast. Here is the string i am trying to process.

it needs to call code from \\server\rights code\code with the parameters  635 "\\server\rights code\Type\2010\2010-06\val_product.txt" "\\server\rights code\Type\2010\2010-06\map_product.txt" "D:\KeyGen\sample.txt" >> "D:\KeyGen\generatedkey.txt"

ex:
cmd.exe /C "\\server\rights code\code" 635 "\\server\rights code\Type\2010\2010-06\val_product.txt" "\\server\rights code\Type\2010\2010-06\map_product.txt" "D:\KeyGen\sample.txt" >> "D:\KeyGen\generatedkey.txt"

If i execute this without the cmd.exe /C from an open command prompt, it works.
"\\server\rights code\code" 635 "\\server\rights code\Type\2010\2010-06\val_product.txt" "\\server\rights code\Type\2010\2010-06\map_product.txt" "D:\KeyGen\sample.txt" >> "D:\KeyGen\generatedkey.txt"

If I try this from an open command prompt,
cmd.exe /C "\\server\rights code\code" 635 "\\server\rights code\Type\2010\2010-06\val_product.txt" "\\server\rights code\Type\2010\2010-06\map_product.txt" "D:\KeyGen\sample.txt" >> "D:\KeyGen\generatedkey.txt"

If fails with the error,
'\\server\rights' is not a recognized as an internal or external command,...

Any ideas - Hades666

Commented:
If you look at the CMD.exe help (cmd.exe /?), here is what it says ...

---------->
If /C or /K is specified, then the remainder of the command line after
the switch is processed as a command line, where the following logic is
used to process quote (") characters:

    1.  If all of the following conditions are met, then quote characters
        on the command line are preserved:

        - no /S switch
        - exactly two quote characters
        - no special characters between the two quote characters,
          where special is one of: &<>()@^|
        - there are one or more whitespace characters between the
          two quote characters
        - the string between the two quote characters is the name
          of an executable file.
<-----------

since you have used >>, it is not preserving the quote ("), while passing parameter to the program.

Coming back to the original problem, i just wonder, do you really need the CMD.exe to execute the program? Also, you may have to set the following to get the standard output to the file

ProcessProperties.RedirectStandardOutput = true

Else, the shell may not open a standard output stream for your program.
DevOps Manager
Top Expert 2011
Commented:
While you piped out the help text, the real issue was quotes... I need to double up and chr(34) and internal quotes.

ex:
cmd.exe /C ""\\server\rights code\code" 635 "\\server\rights code\Type\2010\2010-06\val_product.txt" "\\server\rights code\Type\2010\2010-06\map_product.txt" "D:\KeyGen\sample.txt" >> "D:\KeyGen\generatedkey.txt""

Cheers,
Hades666