Error while executing cmd command through vb.net
-
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 tryHere 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
-
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 tryHere 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
-
Use the
/c
parameter switch tocmd.exe
thus:pr.StandardInput.WriteLine("/c format G:" & Convert.ToCha(13))
txtspeak is the realm of 9 year old children, not developers. Christian Graus
-
Thanks for your reply, but I have still some doubt. Could you explain that a little bit more? Thnaks. Gagan
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
-
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 tryHere 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
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... -
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...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
-
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
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...