Disk Detection and Drive Leeters Assignment in VB.Net

~OSD~ 2,126 Reputation points
2020-12-09T09:28:06.457+00:00

Hi,

Currently I am using Diskpart (Windows command line utility) to manage disks and assign drive letters.
Disk 0 is present almost 99.99% scenarios, however, I need to check for the disk 1 and disk 2 all the time.
If disk 1 is present, assign letter 'Z' to partition 1 and 'X' to partition 2 (if exists). Otherwise, check if disk 2 is present and if yes assign letter 'M' to partition 1 and so on.. as in below screenshot.
Can same check /configured using VB.Net?

46641-image.png

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,345 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,564 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Xingyu Zhao-MSFT 5,356 Reputation points
    2020-12-10T02:35:36.55+00:00

    Hi @~OSD~ ,
    You can also try using Process class to execute the Diskpart command.

            Dim p As Process = New Process()  
            p.StartInfo.UseShellExecute = False  
            ' Redirect the results on screen.  
            p.StartInfo.RedirectStandardOutput = True  
            p.StartInfo.FileName = "C:\Windows\System32\diskpart.exe"  
            p.StartInfo.RedirectStandardInput = True  
            p.Start()  
            p.StandardInput.WriteLine("list disk")  
            p.StandardInput.WriteLine("exit")  
            Dim output As String = p.StandardOutput.ReadToEnd()  
      
            If output.Contains("Disk 1") Then  
                p.Start()  
                p.StandardInput.WriteLine("select disk 1")  
                ' Assign letter 'Z' to partition 1.  
                p.StandardInput.WriteLine("assign ...")  
                p.StandardInput.WriteLine("exit")  
                output = p.StandardOutput.ReadToEnd()  
                p.WaitForExit()  
                Console.WriteLine(output)  
            End If  
            p.WaitForExit()  
            Console.ReadLine()  
    

    Hope it could be helpful.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments