Einzelnen Beitrag anzeigen

DieDolly

Registriert seit: 22. Jun 2018
2.175 Beiträge
 
#12

AW: Netzwerk-Adresse/UNC-Pfad zu normalem Pfad konvertieren

  Alt 23. Feb 2021, 14:30
Ich habe deinen Beitrag noch nicht gesehen bevor ich losgelegt habe.
Hier ist meine Version (ohne Klassen?)

Delphi-Quellcode:
uses System.IOUtils, System.Generics.Collections;

type
 TDriveTypes = (dtDRIVE_UNKNOWN, dtDRIVE_NO_ROOT_DIR, dtDRIVE_REMOVABLE, dtDRIVE_FIXED, dtDRIVE_REMOTE, dtDRIVE_CDROM, dtDRIVE_RAMDISK, dtIgnore);

 {$R *.dfm}

function GetDriveType(const APath: string): TDriveTypes;
var
 Drive: Char;
begin
 if Length(APath) >= 1 then
  begin
   Drive := APath[1];
   Result := TDriveTypes(Winapi.Windows.GetDriveType(PChar(Drive + ':\')));
  end
 else
  Result := TDriveTypes.dtDRIVE_UNKNOWN;
end;

procedure GetDiskDriveLetters(var List: TStringList; LimitedToDriveType: TDriveTypes = TDriveTypes.dtIgnore);
var
 Drives: TArray<string>;
 Drive: string;
begin
 if not Assigned(List) then
  Exit;

 List.BeginUpdate;
 try
  List.Clear;

  Drives := TDirectory.GetLogicalDrives;
  for Drive in Drives do
   begin
    if (LimitedToDriveType = TDriveTypes.dtIgnore) or //
     ((LimitedToDriveType <> TDriveTypes.dtIgnore) and (GetDriveType(Drive) = LimitedToDriveType)) then
     List.Add(Drive);
   end;
 finally
  List.EndUpdate;
 end;
end;

procedure TForm2.Button1Click(Sender: TObject);
var
 S: string;
 List: TStringList;
 UNCPath2DriveLetterDictionary: TDictionary<string, string>;
begin
 List := TStringList.Create;
 try
  // Hole alle Laufwerksbuchstaben des Typs "dtDRIVE_REMOTE" und lege sie in "List" ab
  GetDiskDriveLetters(List, TDriveTypes.dtDRIVE_REMOTE);

  if List.Count > 0 then
   begin
    UNCPath2DriveLetterDictionary := TDictionary<string, string>.Create;
    try
     for S in List do
      begin
       // ExpandUNCFileName('S') >> \\nas\media1\
       UNCPath2DriveLetterDictionary.Add(ExpandUNCFileName(S), S);
      end;

     // Test
     showmessage(UNCPath2DriveLetterDictionary.Items['\\nas\media1\']);
    finally
     UNCPath2DriveLetterDictionary.Free;
    end;
   end;
 finally
  List.Free;
 end;
end;
  Mit Zitat antworten Zitat