开发者

How to convert legacy "C:\Documents and Settings" paths to "C:\Users"

I have some legacy data that was created on Windows XP. It contains absolute file names to files in users' My Documents directories. On Windows 7, these absolute references are no longer point to the correct place. For example what was "C:\Documents and Settings\Gareth\My Documents\readme.txt" should now be "C:\Users\Gareth\Documents\readme.txt".

Many Win32 functions are happy to take the file names under "C:\Documents and Settings" and to behind the scenes convert them to the new file names, however there are functions we use that don't do this. Is there a Windows API th开发者_运维知识库at I can use to convert these file names to the new locations?

Obviously, I could do string search and replace, but that wouldn't be guaranteed to work under all circumstances. Because there are Windows functions that seem to do the substitution it feels like there ought to be a published function I can call to do the same thing.

Just to clarify my question: I'm looking for a function that does this:

Input: "C:\Documents and Settings\Gareth\My Documents\readme.txt"

Output: "C:\Users\Gareth\Documents\readme.txt"

or

Input: "C:\Documents and Settings\Gareth\My Documents\"

Output: "C:\Users\Gareth\Documents\"


This works with symbolic links. That should work on any function that takes a path, MSDN documentation is here: msdn.microsoft.com/en-us/library/aa365680%28v=VS.85%29.aspx

I verified MapFileAndCheckSum(), it works fine. Windows7, compiled with UNICODE in effect.

#include "stdafx.h"
#include <windows.h>
#include <assert.h>
#include <imagehlp.h>
#pragma comment(lib, "imagehlp.lib")

int _tmain(int argc, _TCHAR* argv[])
{
    DWORD hdrsum, chksum;
    const wchar_t* path = L"c:\\documents and settings\\hpassant\\my documents\\test.exe";
    DWORD retval = MapFileAndCheckSum(path, &hdrsum, &chksum);
    assert(retval == 0);
    return 0;
}


Is it what you need? %USERPROFILE%\My Documents


I'm not sure where exactly you're using this (and it's been a while since I've used Windows), but I remember being able to use the %HOMEPATH% variable to get the current user's Documents & Settings directory: \Documents and Settings{username}. I think that this works with the updated Windows 7 paths, too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜