Formatting a diskette?
-
I want to add a section in my vb.net application to ask the client if they want to format a diskette or not. If they answer yes, go ahead and format a diskette in drive a:. Does anyone have some code in Vb that will format a diskette? Any Help is Appreciated :-O
-
I want to add a section in my vb.net application to ask the client if they want to format a diskette or not. If they answer yes, go ahead and format a diskette in drive a:. Does anyone have some code in Vb that will format a diskette? Any Help is Appreciated :-O
You could try using the Windows API
SHFormatDrive()
function -- here's a class that wraps it:Public Class ShellUtilities
Public Declare Unicode Function SHFormatDrive \_ Lib "Shell32.dll" \_ (ByVal hwnd As IntPtr, \_ ByVal iDrive As Int32, \_ ByVal iFormatID As Int32, \_ ByVal iOptions As Int32) As Int64 Public Const SHFD\_CAPACITY\_DEFAULT = 0 'default drive capacity Public Const SHFMT\_OPT\_FULL = 1 Public Const SHFMT\_OPT\_SYSONLY = 2 Public Shared Sub FormatDrive(ByVal dialogOwner As IntPtr, ByVal iDrive As Int32) SHFormatDrive(dialogOwner, iDrive, SHFD\_CAPACITY\_DEFAULT, SHFMT\_OPT\_FULL) End Sub
End Class
And here's an example of calling it from a windows form button click:
Const A\_Drive = 0 Const B\_Drive = 1 Const C\_Drive = 2 ' ... etc ... Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ShellUtilities.FormatDrive(Me.Handle, A\_Drive) End Sub
-
I want to add a section in my vb.net application to ask the client if they want to format a diskette or not. If they answer yes, go ahead and format a diskette in drive a:. Does anyone have some code in Vb that will format a diskette? Any Help is Appreciated :-O
Try this:
Const SHFMT\_ID\_DEFAULT = -1 Const SHFMT\_OPT\_FULL = 1 Const SHFMT\_OPT\_SYSONLY = 2 Const SHFMT\_ERROR = -1 ' Error on last format, drive may be formatable Const SHFMT\_CANCEL = -2 ' Last format was canceled Const SHFMT\_NOFORMAT = -3 ' Drive is not formatable ' SHFormatDrive takes 4 parameters: ' hWnd The handle to the window that will be the dialogs parent window. ' Drive The drive number to format, 0 is A:, 1 is B:, 2 is C:, ... ' fmtID Must always be SHFMT\_ID\_DEFAULT, or the value ????. ' options Opitons. Private Declare Function SHFormatDrive Lib "shell32" ( \_ ByVal hWnd As IntPtr, \_ ByVal Drive As Integer, \_ ByVal fmtID As Integer, \_ ByVal options As Integer) \_ As Integer Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim iDrive As Integer Dim rc As Integer iDrive = 0 rc = SHFormatDrive(Me.Handle, iDrive, 0, 0) Select Case rc Case SHFMT\_ERROR MsgBox("Format failed, user pressed CLOSE") Case SHFMT\_CANCEL MsgBox("Format cancelled - user hit CLOSE") Case SHFMT\_NOFORMAT MsgBox("Unable to format this disk drive") Case Else MsgBox("Disk was formatted successfully and returned: " & rc) End Select End Sub
This will show the format dialog that you see when you do a format from the Explorer Shell. The format won't start until the user presses OK. RageInTheMachine9532
-
Try this:
Const SHFMT\_ID\_DEFAULT = -1 Const SHFMT\_OPT\_FULL = 1 Const SHFMT\_OPT\_SYSONLY = 2 Const SHFMT\_ERROR = -1 ' Error on last format, drive may be formatable Const SHFMT\_CANCEL = -2 ' Last format was canceled Const SHFMT\_NOFORMAT = -3 ' Drive is not formatable ' SHFormatDrive takes 4 parameters: ' hWnd The handle to the window that will be the dialogs parent window. ' Drive The drive number to format, 0 is A:, 1 is B:, 2 is C:, ... ' fmtID Must always be SHFMT\_ID\_DEFAULT, or the value ????. ' options Opitons. Private Declare Function SHFormatDrive Lib "shell32" ( \_ ByVal hWnd As IntPtr, \_ ByVal Drive As Integer, \_ ByVal fmtID As Integer, \_ ByVal options As Integer) \_ As Integer Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim iDrive As Integer Dim rc As Integer iDrive = 0 rc = SHFormatDrive(Me.Handle, iDrive, 0, 0) Select Case rc Case SHFMT\_ERROR MsgBox("Format failed, user pressed CLOSE") Case SHFMT\_CANCEL MsgBox("Format cancelled - user hit CLOSE") Case SHFMT\_NOFORMAT MsgBox("Unable to format this disk drive") Case Else MsgBox("Disk was formatted successfully and returned: " & rc) End Select End Sub
This will show the format dialog that you see when you do a format from the Explorer Shell. The format won't start until the user presses OK. RageInTheMachine9532
Thank you very much for your feedback. I used the code that everyone submitted and it worked great!! Is there a way to pass the Label that you want to use when formatting the diskette. I found the SHFormatDrive documentation on MSDN, but I really don't see a way to pass a Label in. Thanks again for the help, Kevin :) Lost in the vast sea of .NET
-
Thank you very much for your feedback. I used the code that everyone submitted and it worked great!! Is there a way to pass the Label that you want to use when formatting the diskette. I found the SHFormatDrive documentation on MSDN, but I really don't see a way to pass a Label in. Thanks again for the help, Kevin :) Lost in the vast sea of .NET
You can't pass that information to SHFormatDrive, but you can set the volume label after the disk is successfully formatted. You can use SetVolumeLabel, declared in kernel32, by passing in the root path of the drive, like A:\, and the name you want:
Private Declare Function SetVolumeLabel Lib "kernel32" \_ Alias "SetVolumeLabelA" ( \_ ByVal rootPath As String, \_ ByVal newLabel As String) \_ As Integer
Dim rc as Integer
rc = SetVolumeLabel( "A:\", "LabelTest" )RageInTheMachine9532