Solved

open command prompt and run command in vb

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

18 Experts available now in Live!

Get 1:1 Help Now