Solved

open command prompt and run command in vb

Posted on 2014-10-10
4
152 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

690 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