本文介绍了Windows:如何将文件规范化到特殊文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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




  • c:\文档&;设置\Ian\我的文档\Budget.xls

  • c:\Documents&设置\Ian\我的文档\我的图片\女儿的获奖目标.jpg

  • c:\文档和设置\Ian\应用程序数据\uTorrent

  • c:\Documents&设置\所有用户\应用程序数据\Consonto\SpellcheckDictionary.dat

  • c:\Develop\readme。 txt

  • c:\Program Files\Adobe\Reader\WhatsNew.txt



我现在正在硬编码特殊文件夹的路径。如果用户重定向其文件夹,漫游到另一台计算机或升级其操作系统,则路径将被破坏:



我想成为一名优秀的开发人员,并进行转换硬编码的绝对路径从相应的特殊文件夹相对路径:




  • %CSIDL_Personal%\Budget.xls

  • %CSIDL_MyPictures%\女儿的获胜Goal.jpg

  • %CSIDL_AppData%\uTorrent

  • %CSIDL_Common_AppData%\Consonto\SpellcheckDictionary.dat

  • c:\开发\ \readme.txt

  • %CSIDL_Program_Files%\Adobe\Reader\WhatsNew.txt



难点在于,同一文件可以有多种表示形式,例如:




  • c:\Documents&设置\Ian\我的文档\我的图片\女儿的获胜Goal.jpg

  • %CSIDL_Profile%\我的文档\我的图片\女儿的获胜Goal.jpg

  • %CSIDL_Personal%\我的图片\女儿的获胜Goal.jpg

  • %CSIDL_MyPictures%\Daughter's Winning Goal.jpg



还请注意,在Windows XP中,我的图片存储在 我的文档

 %CSIDL_Profile%\我的文档
%CSIDL_Profile%\我的文档\我的图片

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

 %CSIDL_Profile%\文档
%CSIDL_Profile%\图片



问题变成了,






我在想:






我在想:

  void CanonicalizeSpecialPath(字符串路径,ref CSLID cslid,ref字符串relativePath)
{
返回 todo;
}



另请参见




  • MSDN:

  • 新旧事物:

  • 新旧事物:

  • MSDN:


解决方案

我想您可以找到CSIDL如何映射到路径(使用诸如),建立它们的反向字典,然后检查w保留您要存储的路径的开头与字典中的任何键匹配,然后删除开头并存储与之匹配的CSIDL。



不太明显,但是它应该完成工作。


i want to to persist some filenames for the user (e.g. recent files).

Let's use six example files:

  • 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

i'm now hard-coding path to special folders. If the user redirects their folders, roams to another computer, or upgrades their operating system, the paths will be broken:

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

  • %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

The difficulty comes with the fact that there can be multiple representations for the same file, e.g.:

  • 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

Note also that in Windows XP My Pictures are stored in My Documents:

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

But on Vista/7 they are separate:

%CSIDL_Profile%\Documents
%CSIDL_Profile%\Pictures

The question becomes, how to use, as much as possible, paths relative to canonical special folders?


I'm thinking:

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

See also

解决方案

I suppose you could find out how the CSIDL map to paths (using something like SHGetKnownFolderPath), build a reverse dictionary of them, then check whether the beginning of the path you want to store matches any of the keys in the dictionary and then remove the beginning and store the CSIDL that matched.

Not overtly elegant, but it should get the work done.

这篇关于Windows:如何将文件规范化到特殊文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 01:37