Question

I need to write an application that is able to format the USB stick in a way that the partition becomes only 1 MB in size.

We are transforming 4 gig USB sticks in USB dongles with a license file on it that our tecnicians will Always have with them. This allows them to access some special settings of our software. The problem is that our tecnicians use this stick also for private use (and they shouldnt) so we want to avoid this by reducing its size to 1 MB. So far I have done this via a program called bootice but now our secretary has to do it and she is not so much into computers and we need a easier solution.

For this reason I need to implement the formatting in the software we have that generates the license key. so with a single click it would format the drive and add the license.

Is it possible to format the drive with a different then standard size?

I do not need to have the remaining 4 gigs of the stick usable! they can remain hidden or unformatted.

--------------- UPDATE -----------

i was able to repartition the stick via CMD using diskpart. now the problem is that i have to do it via program. the list of commands i had to execute is this:

diskpart
list disk    (REMEMBER DISK NUMBER FROM LIST)
select disk x (REPLACE X WITH DISK NUMBER)
list partition (REMEMBER PARTITION FROM LIST, USUALLY 1 IF ONLY ONE PARTITION IS THERE)
select partition x (REPLACE X WITH PARTITION NUMBER)
delete partition
create partition primary size=1
format

so far so good. my problem is that i need to find out what disk number my usb disk has. i know the drive letter but not the disk number returned by the list disk command.

Was it helpful?

Solution

i found a solution! following code does exactly what i need. all i have to know is the driveletter.

                   Dim driveletter as string
                   Dim scope As ManagementScope
                    scope = New ManagementScope("\\localhost\root\cimv2")
                    scope.Connect()
                    Dim query As ObjectQuery
                    query = New ObjectQuery("SELECT * FROM Win32_PerfRawData_PerfDisk_PhysicalDisk")
                    Dim objOS As ManagementObjectSearcher
                    objOS = New ManagementObjectSearcher(scope, query)



                    Dim objMgmt As ManagementObject
                    Dim aryHardDiskOnSystem As New ArrayList

                    For Each objMgmt In objOS.Get
                        ' aryHardDiskOnSystem.Add(objMgmt("name").ToString())
                        Debug.WriteLine(objMgmt("name").ToString())
                        If objMgmt("name").ToString().Contains(driveletter) Then
                            Dim drivenumber As Integer = objMgmt("name").ToString().Substring(0, 1)
                            Dim FILE_NAME As String = "part.scp"
                            If System.IO.File.Exists(FILE_NAME) = False Then
                                System.IO.File.Create(FILE_NAME).Dispose()
                            End If
                            Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
                            objWriter.WriteLine("select disk " & drivenumber)
                            objWriter.WriteLine("select partition 1")
                            objWriter.WriteLine("delete partition")
                            objWriter.WriteLine("create partition primary size=1") 'size is in MB. i am reducing the partition size to 1 MB.
                            objWriter.WriteLine("format label=" & TextBox2.Text)
                            objWriter.Close()


                            Dim oProcess As New Process()
                            Dim oStartInfo As New ProcessStartInfo("diskpart", "/s part.scp")
                            oStartInfo.UseShellExecute = False
                            oStartInfo.RedirectStandardOutput = True
                            oProcess.StartInfo = oStartInfo
                            oProcess.Start()
                            oProcess.WaitForExit()
                            oProcess.Close()

                            File.Delete("part.scp")
                        End If

                        Next
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top