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. Formatting a diskette?

Formatting a diskette?

Scheduled Pinned Locked Moved Visual Basic
csharphelpquestion
5 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.
  • K Offline
    K Offline
    KreativeKai
    wrote on last edited by
    #1

    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

    M D 2 Replies Last reply
    0
    • K KreativeKai

      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

      M Offline
      M Offline
      Mike Ellison
      wrote on last edited by
      #2

      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
      
      1 Reply Last reply
      0
      • K KreativeKai

        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

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

        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

        K 1 Reply Last reply
        0
        • D Dave Kreskowiak

          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

          K Offline
          K Offline
          KreativeKai
          wrote on last edited by
          #4

          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

          D 1 Reply Last reply
          0
          • K KreativeKai

            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

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

            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

            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