Forum > Windows

GetAppConfigDir question

(1/1)

IPguy:
I'm using GetAppConfigDir(xx), which returns the following string (on a windows system):
xx = False: C:\Users\[username]\AppData\Local\PROGRM1
xx = True:  C:\ProgramData\PROGRM1

I would like to store the data in the following location:
C:\Users\[username]\AppData\Local\COMPANY1\PROGRM1
C:\ProgramData\COMPANY1\PROGRM1

I can deconstruct and rebuild that string to insert the company name, but I was wondering if there is a trivial way to insert the company name in that string for all target OS'?

BlueIcaro:
I did somethig like. I take the full path, remove the last word, it's easy found it, because is the name of your application.
May be something like this: (it's a code wrote on the fly, please check the correct works)

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Dir := GetAppConfigDir(false);Pos := Pos (MyappName,Dir);Dir := Copy (Dir,0,Pos-1);Dir:= Dir+MyCompany+PathDelimiter\MyAppCreateFolder (Dir); 
/BlueIcaro


Then create a folder as you want, and store the that in a variable to use in your program.

/BlueIcaro

Bart:

--- Code: ---function VendorName: String;
begin
  Result := 'MyCompany';
end;

function AppName: String;
begin
  Result:= 'MyAppName';
end;

....

begin
  ...
  OnGetVendorName := @VendorName;
  OnGetApplicationName := @AppName;
  Dir := GetAppConfigDir(false);
  //Dir should be something like: C:\Users\[username]\AppData\Local\MyCompany\MyAppName
end; 

Take a look at the code for GetAppConfigDir() function to seehow it works:

[code]
Function GetAppConfigDir(Global : Boolean) : String;
begin
  ...
    If (Result<>'') then
    begin
      if VendorName<>'' then
        Result:=IncludeTrailingPathDelimiter(Result+VendorName);
      Result:=IncludeTrailingPathDelimiter(Result+ApplicationName);
    end
  ...
end;

.....
Function VendorName : String;
begin
  If Assigned(OnGetVendorName) then
    Result:=OnGetVendorName()
  else
    Result:='';
end;

Function ApplicationName : String;
begin
  If Assigned(OnGetApplicationName) then
    Result:=OnGetApplicationName()
  else
    Result:=ChangeFileExt(ExtractFileName(Paramstr(0)),'');
end;

--- End code ---


Here's afunction to get the root of GetAppConfigDir, so you can easily add 'MyCompany'MyAppName' without having to define the functions that act as OnGetApplication =Name and OnGetVendorName.

--- Code: ---function GetAppConfigRoot(const Global: Boolean): String;
var
  OldGetAppName: TGetAppNameEvent;
  OldGetVendor: TGetVendorNameEvent;
begin
  OldGetAppName := OnGetApplicationName;
  OnGetApplicationName := @GetAppName;
  OldGetVendor := OnGetVendorName;
  OnGetVendorName := @GetVendor;
  try
    Result := GetAppConfigDir(Global);
  finally
    OnGetApplicationName := OldGetAppName;
    OnGetVendorName := OldGetVendor;
  end;
end;

--- End code ---

Bart

IPguy:
This is what I did prior to reading your responses.


--- Code: ---  // gets the name of a dir in which the app should store its conf files on the current OS.
  // Also, see if the directory exists; if not, create it.
  // C:\Users\[UserName]\AppData\Local\Company\Project1\
  CfgDir  := GetAppConfigDir(False);   

  // Now, replace project name with Company name & verify it exists
  CfgDir := StringReplace(CfgDir, 'Project1', 'Company',[rfReplaceAll]);
  if not DirectoryExists(CfgDir)
    then CreateDir(CfgDir);

  // Now, append project name & verify it exists
  CfgDir := CfgDir + 'Project'+PathDelim;
  if not DirectoryExists(CfgDir)
    then CreateDir(CfgDir); 
--- End code ---

WackaMold:
This part:
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function AppName: String; // this callback function signature must match the defaultbegin  Result := 'MyAppName';end; begin  OnGetVendorName := @VendorName; requires:
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$LongStrings On} // strings are ANSI instead of shortThe clue is in the compiler error on the assignment if strings are left as default / short:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Error: (4001) Incompatible types:got      "<address                 of function:ShortString;Register>"expected "<procedure variable type of function:AnsiString ;Register>"

Navigation

[0] Message Index

Go to full version