Alterar letra de disco/unidade no Windows - Código fonte em Free Pascal para Lazarus ou Delphi
Autor: André Aranha | Publicado: 2015-07-13T13:47:00-03:00

Segue o código fonte de um pequeno aplicativo para alterar a letra de unidade de um disco rígido, pendrive ou drive de CD/DVD, para Windows. Pode ser compilado no Lazarus ou no Delphi.
Será necessário adicionar os componentes conforme a declaração Type. Não alterei os nomes dos componentes, pois o aplicativo deveria servir apenas para testes.
O código pode ser melhorado, mas já é funcional.
Para rodar depois de compilado, deverá ser executado como administrador(botão direito no executável=&gt;executar como administrador). 


unit Unit1;

{$mode objfpc}{$H+}
{$R *.lfm}
interface

uses
&nbsp; Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, JwaWinBase;

type

&nbsp; { TForm1 }

&nbsp; TForm1 = class(TForm)
&nbsp;&nbsp;&nbsp; Button1: TButton;
&nbsp;&nbsp;&nbsp; ComboBox1: TComboBox;
&nbsp;&nbsp;&nbsp; ComboBox2: TComboBox;
&nbsp;&nbsp;&nbsp; Label1: TLabel;
&nbsp;&nbsp;&nbsp; Label2: TLabel;
&nbsp;&nbsp;&nbsp; Label3: TLabel;
&nbsp;&nbsp;&nbsp; procedure Button1Click(Sender: TObject);
&nbsp;&nbsp;&nbsp; procedure FormCreate(Sender: TObject);

&nbsp; private
&nbsp;&nbsp;&nbsp; { private declarations }
&nbsp; public
&nbsp;&nbsp;&nbsp; { public declarations }
&nbsp; end;

var
&nbsp; Form1: TForm1;

implementation


var
&nbsp; Drive: Char;
&nbsp; DriveLetter: string;
&nbsp; VolumeName : String;
{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
&nbsp; if (combobox1.ItemIndex = -1) or (ComboBox2.ItemIndex = -1)
&nbsp; then Showmessage('Selecione todas as opções!') else
&nbsp;&nbsp;&nbsp; begin
&nbsp;&nbsp;&nbsp; GetVolumeNameForVolumeMountPoint(Pchar(copy(ComboBox1.Items[ComboBox1.ItemIndex], 1, 3)), Pchar(VolumeName), 50);
&nbsp;&nbsp;&nbsp; DeleteVolumeMountPoint(Pchar(copy(ComboBox1.Items[ComboBox1.ItemIndex], 1, 3)));
&nbsp;&nbsp;&nbsp; SetVolumeMountPoint(Pchar(ComboBox2.Items[ComboBox2.ItemIndex]+':\'), Pchar(VolumeName));
&nbsp;&nbsp;&nbsp; ShowMessage((ComboBox1.Items[ComboBox1.ItemIndex]) + ' alterado para ' + (ComboBox2.Items[ComboBox2.ItemIndex]) + ':\');
&nbsp;&nbsp;&nbsp; end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
&nbsp; for Drive := 'A' to 'Z' do
&nbsp; begin
&nbsp;&nbsp;&nbsp; DriveLetter := Drive + ':\';
&nbsp;&nbsp;&nbsp; if GetDriveType(PChar(DriveLetter)) = DRIVE_UNKNOWN then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ComboBox1.Items.Add(DriveLetter + ' Dispositivo Desconhecido')
&nbsp;&nbsp;&nbsp; else if GetDriveType(PChar(DriveLetter)) = DRIVE_REMOVABLE then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ComboBox1.Items.Add(DriveLetter + ' Pendrive')
&nbsp;&nbsp;&nbsp; else if GetDriveType(PChar(DriveLetter)) = DRIVE_FIXED then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ComboBox1.Items.Add(DriveLetter + ' Disco Local')
&nbsp;&nbsp;&nbsp; else if GetDriveType(PChar(DriveLetter)) = DRIVE_REMOTE then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ComboBox1.Items.Add(DriveLetter + ' Unidade de rede')
&nbsp;&nbsp;&nbsp; else if GetDriveType(PChar(DriveLetter)) = DRIVE_CDROM then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ComboBox1.Items.Add(DriveLetter + ' Drive de CD/DVD')
&nbsp;&nbsp;&nbsp; else if GetDriveType(PChar(DriveLetter)) = DRIVE_RAMDISK then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ComboBox1.Items.Add(DriveLetter + ' RAM Disk')
&nbsp;&nbsp;&nbsp; else
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ComboBox2.Items.Add(Drive);
&nbsp;&nbsp;&nbsp; end;
&nbsp; end;
&nbsp; end.
&nbsp;