質問

ユーザー用のファイル名を維持したい(最近のファイルなど)。

6つの例ファイルを使用しましょう。

  • c:\Documents & Settings\Ian\My Documents\Budget.xls
  • c:\Documents & Settings\Ian\My Documents\My Pictures\Daughter's 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

私は今、特別なフォルダーへのハードコーディングパスです。ユーザーがフォルダーをリダイレクトしたり、別のコンピューターにローミングしたり、オペレーティングシステムをアップグレードしたりすると、パスが壊れます。

私は良い開発者になりたい、そしてこれらのハードコーディングを変換したい 絶対 への道 相対的 適切なパス 特別なフォルダー:

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

難易度は、同じファイルに複数の表現がある可能性があるという事実に伴います。

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

また、Windows XPにも注意してください 私の写真 保存された My Documents:

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

しかし、Vista/7では、それらは別々です:

%CSIDL_Profile%\Documents
%CSIDL_Profile%\Pictures

ノート: 構文に気付きます %CSIDL_xxx%\filename.ext 有効ではない;そのウィンドウは、環境文字列のようにこれらのキーワードを拡張しません。私はこの質問をする方法としてのみ使用しています。内部的には明らかにアイテムを他の方法で保存するでしょう、おそらく CSIDL そして、道の尾、例:

 CSIDL_Personal         \Budget.xls
 CSIDL_MyPictures       \Daughter's 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";
}

参照してください

役に立ちましたか?

解決

CSIDLがパスへのマップをどのようにマップするかを知ることができると思います(次のようなものを使用して ShgetKnownFolderPath)、それらの逆辞書を構築し、辞書のキーのいずれかに一致するパスの開始が辞書のキーの一致かどうかを確認し、一致したCSIDLを削除してから保存します。

あからさまにエレガントではありませんが、作業を完了させるはずです。

他のヒント

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%を探すことで拡張することで行います。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top