Search code examples
.netvb.netdiskdisk-partitioning

Programmatically create/delete/wipe disk partition?


Is there any library in the .NET Framework which allows us to create, delete & wipe out disk partition programmatically in .NET? (I am using VB.NET)

At the moment the option I'm using is going through dos command 'diskpart', which I feel is not efficient in coding point of view.


Solution

  • Try this...

    Imports System.Text
    
    Public Class Form1
    
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Try
            Dim partitionLetter As String = "Q"
            Dim sb As StringBuilder = New StringBuilder()
            sb.AppendLine("SELECT DISK=0") 'Select the first disk drive
            sb.AppendLine("CREATE PARTITION PRIMARY") 'create new partition
            sb.AppendLine("ASSIGN LETTER=" + partitionLetter) 'assign letter
            sb.AppendLine("FORMAT FS=FAT32 LABEL=""FD"" QUICK") 'format new partition
            sb.AppendLine("EXIT")
            System.IO.File.WriteAllText("c:\part.scr", sb.ToString()) 'create diskpart script
            Process.Start("diskpart.exe", "/s c:\part.scr") 'run the script
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    
    End Sub
    
    End Class