using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.IO;
using System.Runtime.InteropServices;

namespace DiskDriveSerialNumber
{
    public partial class frm81DiskDriveSerialNumber : Form
    {
        [DllImport("kernel32.dll")]
        private static extern long GetVolumeInformation(
            string PathName,
            StringBuilder VolumeNameBuffer,
            UInt32 VolumeNameSize,
            ref UInt32 VolumeSerialNumber,
            ref UInt32 MaximumComponentLength,
            ref UInt32 FileSystemFlags,
            StringBuilder FileSystemNameBuffer,
            UInt32 FileSystemNameSize);

        public frm81DiskDriveSerialNumber()
        {
            InitializeComponent();
        }

        // List the available drives.
        private void frm81DiskDriveSerialNumber_Load(object sender, EventArgs e)
        {
            driveComboBox.DataSource = DriveInfo.GetDrives();
        }

        // Display the selected drive's information.
        private void driveComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Get information available via DriveInfo.
            DriveInfo info = new DriveInfo(driveComboBox.Text);
            driveTypeTextBox.Text = info.DriveType.ToString();
            nameTextBox.Text = info.Name;
            rootTextBox.Text = info.RootDirectory.Name;
            isReadyTextBox.Text = info.IsReady.ToString();

            if (info.IsReady)
            {
                volumeLabelTextBox.Text = info.VolumeLabel;
                driveFormatTextBox.Text = info.DriveFormat;
                totalSizeTextBox.Text = info.TotalSize.ToString("N0");
                totalFreeSizeTextBox.Text = info.TotalFreeSpace.ToString("N0");
                availableFreeSpaceTextBox.Text = info.AvailableFreeSpace.ToString("N0");

                // Get more information.
                string volumeName, fileSystemName;
                uint serialNumber, maxComponentLength;
                UInt32 flags;
                GetVolumeInfo(driveComboBox.Text, out volumeName,
                    out serialNumber, out maxComponentLength,
                    out fileSystemName, out flags);

                volumeNameTextBox.Text = volumeName;
                serialNumberTextBox.Text = serialNumber.ToString();
                maxComponentLengthTextBox.Text = maxComponentLength.ToString();
                fileSystemTextBox.Text = fileSystemName;
                flagsTextBox.Text = "H" + flags.ToString("x");
            }
            else
            {
                volumeLabelTextBox.Clear();
                driveFormatTextBox.Clear();
                totalSizeTextBox.Clear();
                totalFreeSizeTextBox.Clear();
                availableFreeSpaceTextBox.Clear();

                volumeNameTextBox.Clear();
                serialNumberTextBox.Clear();
                maxComponentLengthTextBox.Clear();
                fileSystemTextBox.Clear();
                flagsTextBox.Clear();
            }
        }

        // Get file system information.
        private void GetVolumeInfo(string driveLetter, out string volumeName,
            out uint serialNumber, out uint maxComponentLength,
            out string fileSystemName, out UInt32 flags)
        {
            uint serialNum = 0;
            uint maxComponentLen = 0;
            StringBuilder volumeNameSb = new StringBuilder(256);
            UInt32 fileSystemFlags = new UInt32();
            StringBuilder fileSystemNameSb = new StringBuilder(256);

            if (GetVolumeInformation(driveLetter, volumeNameSb,
                (UInt32)volumeNameSb.Capacity, ref serialNum,
                ref maxComponentLen, ref fileSystemFlags, fileSystemNameSb,
                (UInt32)fileSystemNameSb.Capacity) == 0)
            {
                throw new Exception("Error getting volume information");
            }

            volumeName = volumeNameSb.ToString();
            serialNumber = serialNum;
            maxComponentLength = maxComponentLen;
            fileSystemName = fileSystemNameSb.ToString();
            flags = fileSystemFlags;
        }


        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this._Label1_0 = new System.Windows.Forms.Label();
            this.driveComboBox = new System.Windows.Forms.ComboBox();
            this.label1 = new System.Windows.Forms.Label();
            this.availableFreeSpaceTextBox = new System.Windows.Forms.TextBox();
            this.driveFormatTextBox = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.driveTypeTextBox = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.isReadyTextBox = new System.Windows.Forms.TextBox();
            this.label4 = new System.Windows.Forms.Label();
            this.totalSizeTextBox = new System.Windows.Forms.TextBox();
            this.label5 = new System.Windows.Forms.Label();
            this.totalFreeSizeTextBox = new System.Windows.Forms.TextBox();
            this.label6 = new System.Windows.Forms.Label();
            this.rootTextBox = new System.Windows.Forms.TextBox();
            this.label7 = new System.Windows.Forms.Label();
            this.nameTextBox = new System.Windows.Forms.TextBox();
            this.label8 = new System.Windows.Forms.Label();
            this.volumeLabelTextBox = new System.Windows.Forms.TextBox();
            this.label9 = new System.Windows.Forms.Label();
            this.volumeNameTextBox = new System.Windows.Forms.TextBox();
            this.label10 = new System.Windows.Forms.Label();
            this.serialNumberTextBox = new System.Windows.Forms.TextBox();
            this.label11 = new System.Windows.Forms.Label();
            this.label12 = new System.Windows.Forms.Label();
            this.maxComponentLengthTextBox = new System.Windows.Forms.TextBox();
            this.flagsTextBox = new System.Windows.Forms.TextBox();
            this.label13 = new System.Windows.Forms.Label();
            this.fileSystemTextBox = new System.Windows.Forms.TextBox();
            this.label14 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // _Label1_0
            // 
            this._Label1_0.AutoSize = true;
            this._Label1_0.BackColor = System.Drawing.SystemColors.Control;
            this._Label1_0.Cursor = System.Windows.Forms.Cursors.Default;
            this._Label1_0.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this._Label1_0.ForeColor = System.Drawing.SystemColors.ControlText;
            this._Label1_0.Location = new System.Drawing.Point(10, 15);
            this._Label1_0.Name = "_Label1_0";
            this._Label1_0.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this._Label1_0.Size = new System.Drawing.Size(35, 14);
            this._Label1_0.TabIndex = 44;
            this._Label1_0.Text = "Drive:";
            // 
            // driveComboBox
            // 
            this.driveComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.driveComboBox.FormattingEnabled = true;
            this.driveComboBox.Location = new System.Drawing.Point(139, 15);
            this.driveComboBox.Name = "driveComboBox";
            this.driveComboBox.Size = new System.Drawing.Size(106, 21);
            this.driveComboBox.TabIndex = 0;
            this.driveComboBox.SelectedIndexChanged += new System.EventHandler(this.driveComboBox_SelectedIndexChanged);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(25, 272);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(111, 13);
            this.label1.TabIndex = 52;
            this.label1.Text = "Available Free Space:";
            // 
            // availableFreeSpaceTextBox
            // 
            this.availableFreeSpaceTextBox.Location = new System.Drawing.Point(142, 269);
            this.availableFreeSpaceTextBox.Name = "availableFreeSpaceTextBox";
            this.availableFreeSpaceTextBox.ReadOnly = true;
            this.availableFreeSpaceTextBox.Size = new System.Drawing.Size(106, 20);
            this.availableFreeSpaceTextBox.TabIndex = 9;
            // 
            // driveFormatTextBox
            // 
            this.driveFormatTextBox.Location = new System.Drawing.Point(142, 191);
            this.driveFormatTextBox.Name = "driveFormatTextBox";
            this.driveFormatTextBox.ReadOnly = true;
            this.driveFormatTextBox.Size = new System.Drawing.Size(106, 20);
            this.driveFormatTextBox.TabIndex = 6;
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(25, 194);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(70, 13);
            this.label2.TabIndex = 54;
            this.label2.Text = "Drive Format:";
            // 
            // driveTypeTextBox
            // 
            this.driveTypeTextBox.Location = new System.Drawing.Point(142, 61);
            this.driveTypeTextBox.Name = "driveTypeTextBox";
            this.driveTypeTextBox.ReadOnly = true;
            this.driveTypeTextBox.Size = new System.Drawing.Size(106, 20);
            this.driveTypeTextBox.TabIndex = 1;
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(9, 64);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(59, 13);
            this.label3.TabIndex = 56;
            this.label3.Text = "DriveType:";
            // 
            // isReadyTextBox
            // 
            this.isReadyTextBox.Location = new System.Drawing.Point(142, 139);
            this.isReadyTextBox.Name = "isReadyTextBox";
            this.isReadyTextBox.ReadOnly = true;
            this.isReadyTextBox.Size = new System.Drawing.Size(106, 20);
            this.isReadyTextBox.TabIndex = 4;
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(9, 142);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(52, 13);
            this.label4.TabIndex = 58;
            this.label4.Text = "Is Ready:";
            // 
            // totalSizeTextBox
            // 
            this.totalSizeTextBox.Location = new System.Drawing.Point(142, 217);
            this.totalSizeTextBox.Name = "totalSizeTextBox";
            this.totalSizeTextBox.ReadOnly = true;
            this.totalSizeTextBox.Size = new System.Drawing.Size(106, 20);
            this.totalSizeTextBox.TabIndex = 7;
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(25, 220);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(57, 13);
            this.label5.TabIndex = 66;
            this.label5.Text = "Total Size:";
            // 
            // totalFreeSizeTextBox
            // 
            this.totalFreeSizeTextBox.Location = new System.Drawing.Point(142, 243);
            this.totalFreeSizeTextBox.Name = "totalFreeSizeTextBox";
            this.totalFreeSizeTextBox.ReadOnly = true;
            this.totalFreeSizeTextBox.Size = new System.Drawing.Size(106, 20);
            this.totalFreeSizeTextBox.TabIndex = 8;
            // 
            // label6
            // 
            this.label6.AutoSize = true;
            this.label6.Location = new System.Drawing.Point(25, 246);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(92, 13);
            this.label6.TabIndex = 64;
            this.label6.Text = "Total Free Space:";
            // 
            // rootTextBox
            // 
            this.rootTextBox.Location = new System.Drawing.Point(142, 113);
            this.rootTextBox.Name = "rootTextBox";
            this.rootTextBox.ReadOnly = true;
            this.rootTextBox.Size = new System.Drawing.Size(106, 20);
            this.rootTextBox.TabIndex = 3;
            // 
            // label7
            // 
            this.label7.AutoSize = true;
            this.label7.Location = new System.Drawing.Point(9, 116);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(33, 13);
            this.label7.TabIndex = 62;
            this.label7.Text = "Root:";
            // 
            // nameTextBox
            // 
            this.nameTextBox.Location = new System.Drawing.Point(142, 87);
            this.nameTextBox.Name = "nameTextBox";
            this.nameTextBox.ReadOnly = true;
            this.nameTextBox.Size = new System.Drawing.Size(106, 20);
            this.nameTextBox.TabIndex = 2;
            // 
            // label8
            // 
            this.label8.AutoSize = true;
            this.label8.Location = new System.Drawing.Point(9, 90);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(38, 13);
            this.label8.TabIndex = 60;
            this.label8.Text = "Name:";
            // 
            // volumeLabelTextBox
            // 
            this.volumeLabelTextBox.Location = new System.Drawing.Point(142, 165);
            this.volumeLabelTextBox.Name = "volumeLabelTextBox";
            this.volumeLabelTextBox.ReadOnly = true;
            this.volumeLabelTextBox.Size = new System.Drawing.Size(106, 20);
            this.volumeLabelTextBox.TabIndex = 5;
            // 
            // label9
            // 
            this.label9.AutoSize = true;
            this.label9.Location = new System.Drawing.Point(25, 168);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(74, 13);
            this.label9.TabIndex = 68;
            this.label9.Text = "Volume Label:";
            // 
            // volumeNameTextBox
            // 
            this.volumeNameTextBox.Location = new System.Drawing.Point(402, 61);
            this.volumeNameTextBox.Name = "volumeNameTextBox";
            this.volumeNameTextBox.ReadOnly = true;
            this.volumeNameTextBox.Size = new System.Drawing.Size(85, 20);
            this.volumeNameTextBox.TabIndex = 10;
            // 
            // label10
            // 
            this.label10.AutoSize = true;
            this.label10.Location = new System.Drawing.Point(273, 64);
            this.label10.Name = "label10";
            this.label10.Size = new System.Drawing.Size(76, 13);
            this.label10.TabIndex = 70;
            this.label10.Text = "Volume Name:";
            // 
            // serialNumberTextBox
            // 
            this.serialNumberTextBox.Location = new System.Drawing.Point(402, 87);
            this.serialNumberTextBox.Name = "serialNumberTextBox";
            this.serialNumberTextBox.ReadOnly = true;
            this.serialNumberTextBox.Size = new System.Drawing.Size(85, 20);
            this.serialNumberTextBox.TabIndex = 11;
            // 
            // label11
            // 
            this.label11.AutoSize = true;
            this.label11.Location = new System.Drawing.Point(273, 90);
            this.label11.Name = "label11";
            this.label11.Size = new System.Drawing.Size(76, 13);
            this.label11.TabIndex = 72;
            this.label11.Text = "Serial Number:";
            // 
            // label12
            // 
            this.label12.AutoSize = true;
            this.label12.Location = new System.Drawing.Point(273, 116);
            this.label12.Name = "label12";
            this.label12.Size = new System.Drawing.Size(123, 13);
            this.label12.TabIndex = 72;
            this.label12.Text = "Max Component Length:";
            // 
            // maxComponentLengthTextBox
            // 
            this.maxComponentLengthTextBox.Location = new System.Drawing.Point(402, 113);
            this.maxComponentLengthTextBox.Name = "maxComponentLengthTextBox";
            this.maxComponentLengthTextBox.ReadOnly = true;
            this.maxComponentLengthTextBox.Size = new System.Drawing.Size(85, 20);
            this.maxComponentLengthTextBox.TabIndex = 12;
            // 
            // flagsTextBox
            // 
            this.flagsTextBox.Location = new System.Drawing.Point(402, 165);
            this.flagsTextBox.Name = "flagsTextBox";
            this.flagsTextBox.ReadOnly = true;
            this.flagsTextBox.Size = new System.Drawing.Size(85, 20);
            this.flagsTextBox.TabIndex = 14;
            // 
            // label13
            // 
            this.label13.AutoSize = true;
            this.label13.Location = new System.Drawing.Point(273, 168);
            this.label13.Name = "label13";
            this.label13.Size = new System.Drawing.Size(35, 13);
            this.label13.TabIndex = 74;
            this.label13.Text = "Flags:";
            // 
            // fileSystemTextBox
            // 
            this.fileSystemTextBox.Location = new System.Drawing.Point(402, 139);
            this.fileSystemTextBox.Name = "fileSystemTextBox";
            this.fileSystemTextBox.ReadOnly = true;
            this.fileSystemTextBox.Size = new System.Drawing.Size(85, 20);
            this.fileSystemTextBox.TabIndex = 13;
            // 
            // label14
            // 
            this.label14.AutoSize = true;
            this.label14.Location = new System.Drawing.Point(273, 142);
            this.label14.Name = "label14";
            this.label14.Size = new System.Drawing.Size(63, 13);
            this.label14.TabIndex = 75;
            this.label14.Text = "File System:";
            // 
            // frm81DiskDriveSerialNumber
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(499, 301);
            this.Controls.Add(this.flagsTextBox);
            this.Controls.Add(this.label13);
            this.Controls.Add(this.fileSystemTextBox);
            this.Controls.Add(this.label14);
            this.Controls.Add(this.maxComponentLengthTextBox);
            this.Controls.Add(this.label12);
            this.Controls.Add(this.serialNumberTextBox);
            this.Controls.Add(this.label11);
            this.Controls.Add(this.volumeNameTextBox);
            this.Controls.Add(this.label10);
            this.Controls.Add(this.volumeLabelTextBox);
            this.Controls.Add(this.label9);
            this.Controls.Add(this.totalSizeTextBox);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.totalFreeSizeTextBox);
            this.Controls.Add(this.label6);
            this.Controls.Add(this.rootTextBox);
            this.Controls.Add(this.label7);
            this.Controls.Add(this.nameTextBox);
            this.Controls.Add(this.label8);
            this.Controls.Add(this.isReadyTextBox);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.driveTypeTextBox);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.driveFormatTextBox);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.availableFreeSpaceTextBox);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.driveComboBox);
            this.Controls.Add(this._Label1_0);
            this.Name = "frm81DiskDriveSerialNumber";
            this.Text = "DiskDriveSerialNumber";
            this.Load += new System.EventHandler(this.frm81DiskDriveSerialNumber_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion
        public System.Windows.Forms.Label _Label1_0;
        private System.Windows.Forms.ComboBox driveComboBox;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox availableFreeSpaceTextBox;
        private System.Windows.Forms.TextBox driveFormatTextBox;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox driveTypeTextBox;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.TextBox isReadyTextBox;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.TextBox totalSizeTextBox;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.TextBox totalFreeSizeTextBox;
        private System.Windows.Forms.Label label6;
        private System.Windows.Forms.TextBox rootTextBox;
        private System.Windows.Forms.Label label7;
        private System.Windows.Forms.TextBox nameTextBox;
        private System.Windows.Forms.Label label8;
        private System.Windows.Forms.TextBox volumeLabelTextBox;
        private System.Windows.Forms.Label label9;
        private System.Windows.Forms.TextBox volumeNameTextBox;
        private System.Windows.Forms.Label label10;
        private System.Windows.Forms.TextBox serialNumberTextBox;
        private System.Windows.Forms.Label label11;
        private System.Windows.Forms.Label label12;
        private System.Windows.Forms.TextBox maxComponentLengthTextBox;
        private System.Windows.Forms.TextBox flagsTextBox;
        private System.Windows.Forms.Label label13;
        private System.Windows.Forms.TextBox fileSystemTextBox;
        private System.Windows.Forms.Label label14;
    }
}