确定当前用户的Firefox配置文件

时间:2019-10-26 06:20:12

标签: delphi

如何用Delphi找出当前用户的firefox配置文件的配置文件名称和文件夹?有时,appdata\roaming文件夹中为用户提供了多个firefox配置文件。因此,我将不得不阅读profile.ini文件。

有人可以用Delphi代码帮助我吗?

安德烈亚斯

1 个答案:

答案 0 :(得分:3)

下面的Delphi控制台应用程序读取Profiles.Ini文件,并将配置文件名称和路径从每个[ProfileX]节写入控制台。有点long 但是,如果您不太熟悉在Delphi中使用Ini文件,它将向您展示如何获得所需的内容。它在拉撒路也应该能正常工作。

program FirefoxProfiles;

{$APPTYPE CONSOLE}

uses
  Classes, SysUtils, IniFiles;


procedure GetProfiles(IniFile : TIniFile; Sections, Profiles : TStringList);
var
  i,
  j : Integer;
  Find,
  SectionName,
  ProfileName : String;
  UserName : String;
begin
  UserName := GetEnvironmentVariable('UserName');

  IniFile.ReadSections(Sections);

  Find := 'Profile';
  for i := 0 to Sections.Count - 1 do begin
    SectionName := Sections[i];
    if CompareText('Profile', Copy(SectionName, 1, Length(Find))) = 0 then begin
      Profiles.Add(SectionName);
      writeln(ProfileName);
    end;
  end;


  for j := 0 to Profiles.Count - 1 do begin
    ProfileName := Profiles[j];
    writeln('Profile: ', ProfileName);
    writeln('Name: ', IniFile.ReadString(ProfileName, 'Name', ''));
    writeln('Path: ', IniFile.ReadString(ProfileName, 'Path', ''));
    writeln;
  end;
end;

var
  IniFile : TIniFile;
  Sections,
  Profiles : TStringList;
  i,
  j : Integer;
begin
  IniFile := TIniFile.Create('c:\Users\ma\appdata\roaming\mozilla\firefox\profiles.ini');

  try
   Sections := TStringList.Create;
   try
     Profiles := TStringList.Create;
     try
       GetProfiles(IniFile, Sections, Profiles);
     finally
       Profiles.Free;
     end;
   finally
     Sections.Free;
   end;
  finally
    IniFile.Free;
    readln;
  end
end.

潜在的问题是识别当前用户的个人资料。上面的代码显示了如何从OS环境中获取当前的UserName,但是UserName不一定与任何配置文件中的Name值相对应。 F.i.在这台笔记本电脑中,我设置了四个配置文件,但是没有一个Name与我的OS用户名相同。显然,如果您的情况 具有使用OS用户名的配置文件,则可以通过将其与返回值IniFile.ReadString(ProfileName, 'Name', '')进行比较来进行搜索。