Solved

open command prompt and run command in vb

Posted on 2014-10-10
4
129 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now