gpt4 book ai didi

Windows:如何将文件规范化到特殊文件夹?

转载 作者:可可西里 更新时间:2023-11-01 13:26:43 25 4
gpt4 key购买 nike

我想为用户保留一些文件名(例如最近的文件)。

让我们使用六个示例文件:

  • 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 Pictures 存储在 My Documents 中:

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

但在 Vista/7 上它们是分开的:

%CSIDL_Profile%\Documents
%CSIDL_Profile%\Pictures

Note: i realize the syntax%CSIDL_xxx%\filename.ext is not valid; thatWindows will not expand those keywordslike they are environment strings. i'm onlyusing it as a way to ask thisquestion. Internally i would obviouslystore the items some other way, perhaps as a CSIDL parentand the tail of the path, e.g.:

 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。

不是太优雅,但应该可以完成工作。

关于Windows:如何将文件规范化到特殊文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3181120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com