Windows: كيفية وضع ملف إلى المجلد الخاص؟
-
02-10-2019 - |
سؤال
أريد أن أستمر في بعض أسماء الملفات للمستخدم (مثل الملفات الحديثة).
لنستخدم ستة ملفات مثال:
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
غير صالح؛ لن يقوم Windows بتوسيع تلك الكلمات الرئيسية مثل سلاسل البيئة. أنا فقط أستخدمه كوسيلة لطرح هذا السؤال. من الواضح أنني كنت أقوم بتخزين العناصر بطريقة أخرى ، ربما كـ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";
}
أنظر أيضا
- MSDN: تعداد CSIDL
- شيء قديم جديد: احذر من ملفات تعريف المستخدمين المتجولين
- شيء قديم جديد: احذر من المجلدات التي تم إعادة توجيهها أيضًا
- MSDN: وظيفة pathcanonicalize
المحلول
أفترض أنه يمكنك معرفة كيفية خريطة CSIDL للمسارات (باستخدام شيء مثل Shgetkenderfolderpath) ، قم بإنشاء قاموس عكسي لهم ، ثم تحقق مما إذا كانت بداية المسار الذي تريد تخزينه يتطابق مع أي من المفاتيح في القاموس ثم قم بإزالة البداية وتخزين 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 ٪ في بداية السلسلة ، وتوسيعها.