Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Error while executing cmd command through vb.net

Error while executing cmd command through vb.net

Scheduled Pinned Locked Moved Visual Basic
csharphelp
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gagan 20
    wrote on last edited by
    #1

    Hi all. I am trying to format my pen drive by creating a process of command prompt and using "format" command (without quotes) in vb.net. The code I am using is below :

    Try
    Dim pr As New Process
    With pr.StartInfo
    .FileName = "cmd.exe"
    .RedirectStandardInput = True
    .RedirectStandardOutput = True
    .UseShellExecute = False
    .WindowStyle = ProcessWindowStyle.Normal
    End With
    pr.Start()
    pr.StandardInput.WriteLine("format G:" & Convert.ToCha(13)) 'enter to take the pen drive.
    pr.StandardInput.WriteLine("{ENTER}") 'next enter to start the formatting process.
    catch ex as exception
    msgbox(ex.message)
    end try

    Here my pen drive's drive is G: drive which I want to format. This code starts the formatting process but it is not formatting the pen drive and also the command prompt window remain open always. Suggest me what should I do to format my pen drive using vb.net code. Thanks. Gagan

    L D 2 Replies Last reply
    0
    • G Gagan 20

      Hi all. I am trying to format my pen drive by creating a process of command prompt and using "format" command (without quotes) in vb.net. The code I am using is below :

      Try
      Dim pr As New Process
      With pr.StartInfo
      .FileName = "cmd.exe"
      .RedirectStandardInput = True
      .RedirectStandardOutput = True
      .UseShellExecute = False
      .WindowStyle = ProcessWindowStyle.Normal
      End With
      pr.Start()
      pr.StandardInput.WriteLine("format G:" & Convert.ToCha(13)) 'enter to take the pen drive.
      pr.StandardInput.WriteLine("{ENTER}") 'next enter to start the formatting process.
      catch ex as exception
      msgbox(ex.message)
      end try

      Here my pen drive's drive is G: drive which I want to format. This code starts the formatting process but it is not formatting the pen drive and also the command prompt window remain open always. Suggest me what should I do to format my pen drive using vb.net code. Thanks. Gagan

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Use the /c parameter switch to cmd.exe thus:

      pr.StandardInput.WriteLine("/c format G:" & Convert.ToCha(13))

      txtspeak is the realm of 9 year old children, not developers. Christian Graus

      G 1 Reply Last reply
      0
      • L Lost User

        Use the /c parameter switch to cmd.exe thus:

        pr.StandardInput.WriteLine("/c format G:" & Convert.ToCha(13))

        txtspeak is the realm of 9 year old children, not developers. Christian Graus

        G Offline
        G Offline
        Gagan 20
        wrote on last edited by
        #3

        Thanks for your reply, but I have still some doubt. Could you explain that a little bit more? Thnaks. Gagan

        L 1 Reply Last reply
        0
        • G Gagan 20

          Thanks for your reply, but I have still some doubt. Could you explain that a little bit more? Thnaks. Gagan

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Gagan.20 wrote:

          Thanks for your reply, but I have still some doubt. Could you explain that a little bit more?

          What more is there to explain? Open a command prompt window and type "cmd /?" for explanation of the switches and parameter options.

          txtspeak is the realm of 9 year old children, not developers. Christian Graus

          1 Reply Last reply
          0
          • G Gagan 20

            Hi all. I am trying to format my pen drive by creating a process of command prompt and using "format" command (without quotes) in vb.net. The code I am using is below :

            Try
            Dim pr As New Process
            With pr.StartInfo
            .FileName = "cmd.exe"
            .RedirectStandardInput = True
            .RedirectStandardOutput = True
            .UseShellExecute = False
            .WindowStyle = ProcessWindowStyle.Normal
            End With
            pr.Start()
            pr.StandardInput.WriteLine("format G:" & Convert.ToCha(13)) 'enter to take the pen drive.
            pr.StandardInput.WriteLine("{ENTER}") 'next enter to start the formatting process.
            catch ex as exception
            msgbox(ex.message)
            end try

            Here my pen drive's drive is G: drive which I want to format. This code starts the formatting process but it is not formatting the pen drive and also the command prompt window remain open always. Suggest me what should I do to format my pen drive using vb.net code. Thanks. Gagan

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            No need to use a Process object at all. Read this.[^]

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007, 2008
            But no longer in 2009...

            G 1 Reply Last reply
            0
            • D Dave Kreskowiak

              No need to use a Process object at all. Read this.[^]

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007, 2008
              But no longer in 2009...

              G Offline
              G Offline
              Gagan 20
              wrote on last edited by
              #6

              I have a doubt in the example you had suggested. I am using a combo box to select drive name. In the code

              Call SHFormatDrive(Me.hWnd, _
              drvToFormat, _
              SHFD_CAPACITY_DEFAULT, _
              SHFD_FORMAT_QUICK)

              where drvToFormat is declared as integer. When I am passing drive name to drvToFormat it is giving error that conversion from string to integer is not valid. What value should I paas as to drvToFormat to format my pen drive? Thanks. Gagan

              D 1 Reply Last reply
              0
              • G Gagan 20

                I have a doubt in the example you had suggested. I am using a combo box to select drive name. In the code

                Call SHFormatDrive(Me.hWnd, _
                drvToFormat, _
                SHFD_CAPACITY_DEFAULT, _
                SHFD_FORMAT_QUICK)

                where drvToFormat is declared as integer. When I am passing drive name to drvToFormat it is giving error that conversion from string to integer is not valid. What value should I paas as to drvToFormat to format my pen drive? Thanks. Gagan

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                You might want to read the documentation on SHFormatDrive[^]. You're passing in a string when you should be pass in a number. The number represents which drive to format. Read the documentation and you can figure out pretty easily, what that number is supposed to be.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                     2006, 2007, 2008
                But no longer in 2009...

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups