Solved

open command prompt and run command in vb

Posted on 2014-10-10
4
143 Views
Last Modified: 2015-01-18
Hello,

i am trying to simply open a command prompt and run a command but its not running just flashing a command prompt can someone help


       Dim p As New Process()
        p.StartInfo.StandardOutputEncoding = System.Text.Encoding.ASCII
        p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
        p.StartInfo.RedirectStandardOutput = True
        p.StartInfo.UseShellExecute = False
        p.StartInfo.FileName = "cmd.exe"
        p.StartInfo.WorkingDirectory = "c:\Windows\Microsoft.NET\Framework\v4.0.30319\"
        p.StartInfo.Arguments = "aspnet_regiis -pef" & "connectionStrings" & Path & cmbApps.Text
        Try
            p.Start()
            Dim output As String = p.StandardOutput.Read()
            lstdata.Items.Add(output)
            p.WaitForExit()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally

            If Not (p Is Nothing) Then
                p.Dispose()
            End If

        End Try

Open in new window

0
Comment
Question by:Chris Jones
  • 3
4 Comments
 
LVL 22

Expert Comment

by:rspahitz
ID: 40373718
When using the old Shell command, you would do something like this:

"cmd /c DOSCmd"
to run the command and close the window
or
"cmd /k DOSCmd"
to run the command and keep the window open.

Does that work here?

p.StartInfo.FileName = "cmd.exe /k"

--
Most likely, there's a DOS syntax error so keeping the window open will probably help you identify what's wrong.

My guess would be that you need a space after (and maybe before) "connectionStrings" -> "connectionStrings "
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 40373729
Another way to help is to first convert this into a value:

"aspnet_regiis -pef" & "connectionStrings" & Path & cmbApps.Text

Then open a cmd window from the start button and enter:
cmd /k aspnet_regiis -pefconnectionStrings {Path} {cmbApps.Text}

Open in new window


Where the last two parts are the values.  You should see the error in the command window
0
 
LVL 1

Author Comment

by:Chris Jones
ID: 40373772
Hello rspahitz

this did work when i ran from the start menu and command prompt without the /k

but the above did not work

"cmd /c DOSCmd" 
to run the command and close the window
or
"cmd /k DOSCmd"
to run the command and keep the window open.

Does that work here?

p.StartInfo.FileName = "cmd.exe /k"

Open in new window

0
 
LVL 22

Accepted Solution

by:
rspahitz earned 500 total points
ID: 40373937
I've never done it the way you're doing it, but that's probably the "new way".
For now you can see if this works:

Shell "cmd /k aspnet_regiis -pef" & "connectionStrings" & Path & cmbApps.Text

If so, you may see the error message then you can go back to your code so you can capture the process info.

Otherwise, see if you can locate a method to KeepWindowOpen or possibly this:

p.StartInfo.Arguments = "aspnet_regiis -pef" & "connectionStrings" & Path & cmbApps.Text & " > C:\errorfile.txt"

After the window flashes (runs the command and closes the window!) then you should have a file called errorfile.txt located on the root of your C-drive which might clue you in on the problem.
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SSRS 2016 Rendering HTML tables 3 55
Visual studio 2015 1 34
SSRS Deployment problem 5 90
reading excel file in .net 2 30
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question