<legend id='aOyFu'><style id='aOyFu'><dir id='aOyFu'><q id='aOyFu'></q></dir></style></legend>

<i id='aOyFu'><tr id='aOyFu'><dt id='aOyFu'><q id='aOyFu'><span id='aOyFu'><b id='aOyFu'><form id='aOyFu'><ins id='aOyFu'></ins><ul id='aOyFu'></ul><sub id='aOyFu'></sub></form><legend id='aOyFu'></legend><bdo id='aOyFu'><pre id='aOyFu'><center id='aOyFu'></center></pre></bdo></b><th id='aOyFu'></th></span></q></dt></tr></i><div id='aOyFu'><tfoot id='aOyFu'></tfoot><dl id='aOyFu'><fieldset id='aOyFu'></fieldset></dl></div>

    <small id='aOyFu'></small><noframes id='aOyFu'>

      <bdo id='aOyFu'></bdo><ul id='aOyFu'></ul>
    1. <tfoot id='aOyFu'></tfoot>

      在 Windows 上获取实际文件名(带有正确的大小写

      时间:2023-08-01
        <tbody id='1GH7S'></tbody>

      1. <i id='1GH7S'><tr id='1GH7S'><dt id='1GH7S'><q id='1GH7S'><span id='1GH7S'><b id='1GH7S'><form id='1GH7S'><ins id='1GH7S'></ins><ul id='1GH7S'></ul><sub id='1GH7S'></sub></form><legend id='1GH7S'></legend><bdo id='1GH7S'><pre id='1GH7S'><center id='1GH7S'></center></pre></bdo></b><th id='1GH7S'></th></span></q></dt></tr></i><div id='1GH7S'><tfoot id='1GH7S'></tfoot><dl id='1GH7S'><fieldset id='1GH7S'></fieldset></dl></div>
        <tfoot id='1GH7S'></tfoot>

        <small id='1GH7S'></small><noframes id='1GH7S'>

        1. <legend id='1GH7S'><style id='1GH7S'><dir id='1GH7S'><q id='1GH7S'></q></dir></style></legend>
                <bdo id='1GH7S'></bdo><ul id='1GH7S'></ul>

                本文介绍了在 Windows 上获取实际文件名(带有正确的大小写)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                Windows 文件系统不区分大小写.如何,给定文件/文件夹名称(例如somefile"),我如何获得该文件/文件夹的实际名称(例如,如果资源管理器显示它,它应该返回SomeFile")?

                Windows file system is case insensitive. How, given a file/folder name (e.g. "somefile"), I get the actual name of that file/folder (e.g. it should return "SomeFile" if Explorer displays it so)?

                我所知道的一些方法,所有这些方法看起来都很落后:

                Some ways I know, all of which seem quite backwards:

                1. 给定完整路径,搜索路径上的每个文件夹(通过 FindFirstFile).这给出了每个文件夹的正确大小写结果.在最后一步,搜索文件本身.
                2. 从句柄中获取文件名(如 MSDN 示例中所示)一>).这需要打开一个文件、创建文件映射、获取它的名称、解析设备名称等.相当复杂.它不适用于文件夹或零大小文件.

                我是否遗漏了一些明显的 WinAPI 调用?最简单的,如 GetActualPathName() 或 GetFullPathName() 使用传入的大小写返回名称(例如,如果传入,则返回程序文件",即使它应该是程序文件").

                Am I missing some obvious WinAPI call? The simplest ones, like GetActualPathName() or GetFullPathName() return the name using casing that was passed in (e.g. returns "program files" if that was passed in, even if it should be "Program Files").

                我正在寻找本机解决方案(不是 .NET 解决方案).

                I'm looking for a native solution (not .NET one).

                推荐答案

                我在此回答我自己的问题,基于 来自 cspirz 的原始答案.

                And hereby I answer my own question, based on original answer from cspirz.

                这是一个给定绝对路径、相对路径或网络路径的函数,它将返回大写/小写的路径,就像它在 Windows 上显示的那样.如果路径的某个组件不存在,它将返回从该点传入的路径.

                Here's a function that given absolute, relative or network path, will return the path with upper/lower case as it would be displayed on Windows. If some component of the path does not exist, it will return the passed in path from that point.

                它非常复杂,因为它试图处理网络路径和其他边缘情况.它对宽字符串进行操作并使用 std::wstring.是的,理论上 Unicode TCHAR 可能与 wchar_t 不同;这是读者的练习:)

                It is quite involved because it tries to handle network paths and other edge cases. It operates on wide character strings and uses std::wstring. Yes, in theory Unicode TCHAR could be not the same as wchar_t; that is an exercise for the reader :)

                std::wstring GetActualPathName( const wchar_t* path )
                {
                    // This is quite involved, but the meat is SHGetFileInfo
                
                    const wchar_t kSeparator = L'\';
                
                    // copy input string because we'll be temporary modifying it in place
                    size_t length = wcslen(path);
                    wchar_t buffer[MAX_PATH];
                    memcpy( buffer, path, (length+1) * sizeof(path[0]) );
                
                    size_t i = 0;
                
                    std::wstring result;
                
                    // for network paths (\servershareRestOfPath), getting the display
                    // name mangles it into unusable form (e.g. "\servershare" turns
                    // into "share on server (server)"). So detect this case and just skip
                    // up to two path components
                    if( length >= 2 && buffer[0] == kSeparator && buffer[1] == kSeparator )
                    {
                        int skippedCount = 0;
                        i = 2; // start after '\'
                        while( i < length && skippedCount < 2 )
                        {
                            if( buffer[i] == kSeparator )
                                ++skippedCount;
                            ++i;
                        }
                
                        result.append( buffer, i );
                    }
                    // for drive names, just add it uppercased
                    else if( length >= 2 && buffer[1] == L':' )
                    {
                        result += towupper(buffer[0]);
                        result += L':';
                        if( length >= 3 && buffer[2] == kSeparator )
                        {
                            result += kSeparator;
                            i = 3; // start after drive, colon and separator
                        }
                        else
                        {
                            i = 2; // start after drive and colon
                        }
                    }
                
                    size_t lastComponentStart = i;
                    bool addSeparator = false;
                
                    while( i < length )
                    {
                        // skip until path separator
                        while( i < length && buffer[i] != kSeparator )
                            ++i;
                
                        if( addSeparator )
                            result += kSeparator;
                
                        // if we found path separator, get real filename of this
                        // last path name component
                        bool foundSeparator = (i < length);
                        buffer[i] = 0;
                        SHFILEINFOW info;
                
                        // nuke the path separator so that we get real name of current path component
                        info.szDisplayName[0] = 0;
                        if( SHGetFileInfoW( buffer, 0, &info, sizeof(info), SHGFI_DISPLAYNAME ) )
                        {
                            result += info.szDisplayName;
                        }
                        else
                        {
                            // most likely file does not exist.
                            // So just append original path name component.
                            result.append( buffer + lastComponentStart, i - lastComponentStart );
                        }
                
                        // restore path separator that we might have nuked before
                        if( foundSeparator )
                            buffer[i] = kSeparator;
                
                        ++i;
                        lastComponentStart = i;
                        addSeparator = true;
                    }
                
                    return result;
                }
                

                再次感谢 cspirz 将我指向 SHGetFileInfo.

                Again, thanks to cspirz for pointing me to SHGetFileInfo.

                这篇关于在 Windows 上获取实际文件名(带有正确的大小写)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 下一篇:如何获取窗口的可执行文件名称

                相关文章

                最新文章

                <small id='Be4yh'></small><noframes id='Be4yh'>

                  <bdo id='Be4yh'></bdo><ul id='Be4yh'></ul>
                <i id='Be4yh'><tr id='Be4yh'><dt id='Be4yh'><q id='Be4yh'><span id='Be4yh'><b id='Be4yh'><form id='Be4yh'><ins id='Be4yh'></ins><ul id='Be4yh'></ul><sub id='Be4yh'></sub></form><legend id='Be4yh'></legend><bdo id='Be4yh'><pre id='Be4yh'><center id='Be4yh'></center></pre></bdo></b><th id='Be4yh'></th></span></q></dt></tr></i><div id='Be4yh'><tfoot id='Be4yh'></tfoot><dl id='Be4yh'><fieldset id='Be4yh'></fieldset></dl></div>

                    <tfoot id='Be4yh'></tfoot>

                  1. <legend id='Be4yh'><style id='Be4yh'><dir id='Be4yh'><q id='Be4yh'></q></dir></style></legend>