Solved

open command prompt and run command in vb

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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.

773 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