Windows: how to canonize a file in a special folder?

I want to save some file names for the user (e.g. recent files).

Let me use six sample files:

  • c:\Documents & Settings\Ian\My Documents\Budget.xls
  • c:\Documents & Settings\Ian\My Documents\My Pictures\Daughter Winning Goal.jpg
  • c:\Documents & Settings\Ian\Application Data\uTorrent
  • c:\Documents & Settings\All Users\Application Data\Consonto\SpellcheckDictionary.dat
  • c:\Develop\readme.txt
  • c:\Program Files\Adobe\Reader\WhatsNew.txt

Now I hard-code the path to special folders. If the user redirects his folders, moves to another computer or updates his operating system, the paths will be split:

I want to be a good developer and convert these hard-coded absolute paths to relative paths from the corresponding special folders :

  • %CSIDL_Personal%\Budget.xls
  • %CSIDL_MyPictures%\Daughter Winning Goal.jpg
  • %CSIDL_AppData%\uTorrent
  • %CSIDL_Common_AppData%\Consonto\SpellcheckDictionary.dat
  • c:\Develop\readme.txt
  • %CSIDL_Program_Files%\Adobe\Reader\WhatsNew.txt

The difficulty is that for the same file there can be several views, for example:

  • c:\Documents & Settings\Ian\My Documents\My Pictures\Daughter Winning Goal.jpg
  • %CSIDL_Profile%\My Documents\My Pictures\Daughter Winning Goal.jpg
  • %CSIDL_Personal%\My Pictures\Daughter Winning Goal.jpg
  • %CSIDL_MyPictures%\Daughter Winning Goal.jpg

Please note that in Windows XP My drawings are stored in My Documents :

%CSIDL_Profile%\My Documents
%CSIDL_Profile%\My Documents\My Pictures

But in Vista / 7 they are separated:

%CSIDL_Profile%\Documents
%CSIDL_Profile%\Pictures

: %CSIDL_xxx%\filename.ext ; Windows . . , , , , CSIDL , :

 CSIDL_Personal         \Budget.xls
 CSIDL_MyPictures       \Daughter Winning Goal.jpg
 CSIDL_AppData          \uTorrent
 CSIDL_Common_AppData   \Consonto\SpellcheckDictionary.dat
 -1                     c:\Develop\readme.txt   (-1, since 0 is a valid csidl)
 CSIDL_Program_Files    \Adobe\Reader\WhatsNew.txt

, , , ?


:

void CanonicalizeSpecialPath(String path, ref CSLID cslid, ref String relativePath)
{
   return "todo";
}

.

+5
2

, , CSIDL ( - SHGetKnownFolderPath), , , , , , CSIDL.

, .

+1
function CanonicalizeSpecialPath(const path: string): string;
var
    s: string;
    BestPrefix: string;
    BestCSIDL: Integer;
    i: Integer;
begin
    BestPrefix := ''; //Start with no csidl being the one
    BestCSIDL := 0;

    //Iterate over the csidls i know about today for Windows XP.    
    for i := Low(csidls) to High(csidls) do
    begin
       //Get the path of this csidl. If the OS doesn't understand it, it returns blank
       s := GetSpecialFolderPath(0, i, False);
       if s = '' then
          Continue;

       //Don't do a string search unless this candidate is larger than what we have
       if (BestPrefix='') or (Length(s) > Length(BestPrefix)) then
       begin
          //The special path must be at the start of our string
          if Pos(s, Path) = 1 then //1=start
          begin
             //This is the best csidl we have so far
             BestPrefix := s;
             BestCSIDL := i;
          end;
       end;
    end;

    //If we found nothing useful, then return the original string
    if BestPrefix = '' then
    begin
       Result := Path;
       Exit;
    end;

    {
       Return the canonicalized path as pseudo-environment string, e.g.:

           %CSIDL_PERSONAL%\4th quarter.xls
    }
    Result := '%'+CsidlToStr(BestCSIDL)+'%'+Copy(Path, Length(BestPrefix)+1, MaxInt);
end;

, "" :

function ExpandSpecialPath(const path: string): string;
begin
   ...
end;

:

%CSIDL_PERSONAL%\4th quarter.xls

\\RoamingProfileServ\Users\ian\My Documents\4th quarter.xls

, % xx% .

+1

All Articles