1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,registry,strutils;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Déclarations privées }
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function SetGlobalEnvironment(const Name, Value: string; const User: Boolean = True): Boolean;
resourcestring
REG_MACHINE_LOCATION = 'System\CurrentControlSet\Control\Session Manager\Environment';
REG_USER_LOCATION = 'Environment';
begin
with TRegistry.Create do
try
if User then { User Environment Variable } //current user
Result := OpenKey(REG_USER_LOCATION, True)
else { System Environment Variable } //all users
begin
RootKey := HKEY_LOCAL_MACHINE;
Result := OpenKey(REG_MACHINE_LOCATION, True);
end;
if Result then
begin
WriteString(Name, Value); { Write Registry for Global Environment }
{ Update Current Process Environment Variable }
SetEnvironmentVariable(PChar(Name), PChar(Value));
{ Send Message To All Top Window for Refresh }
SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, LPARAM(PChar('Environment')),SMTO_NORMAL,4,nil);
end;
finally
Free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var path ,lecteur,chemin: string;
l:tstringlist;
present:boolean;
i:integer;
resourcestring
REG_MACHINE_LOCATION = 'System\CurrentControlSet\Control\Session Manager\Environment';
REG_USER_LOCATION = 'Environment';
begin
with TRegistry.Create do
try
if true then { User Environment Variable } //current user
OpenKey(REG_USER_LOCATION, True)
else { System Environment Variable } //all users
begin
RootKey := HKEY_LOCAL_MACHINE;
OpenKey(REG_MACHINE_LOCATION, True);
end;
path:=ReadString('PATH');
finally
Free;
end;
if rightstr(path,1)=';' then
path:=copy(path,1,length(path)-1);
lecteur:= leftstr(GetEnvironmentVariable('PROGRAMFILES'),2);
chemin:=lecteur+'\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin';
try
l:=tstringlist.Create;
l.Delimiter:=';';
l.StrictDelimiter := True;
l.DelimitedText := path;
present:=false;
for I := 0 to l.Count-1 do
if l[i]=chemin then
begin
present:=true;
break;
end;
if not present then
path:=path+';'+chemin;
SetGlobalEnvironment('PATH',path+';', true);
showmessage('Path modifié avec succès !');
finally
l.Free;
end;
end;
end. |