How do I get the correct case of a path?

I have a small but itching problem. How do I get the correct case for a Windows path in Qt?

Let's say i have a path c:documents and settingswolfgangdocuments stored in a QString str and i want to know the correct case, here C:Document and SettingsWolfgangDocuments . QDir(str).absolutePath() doesn't get me the path with correct case.

Any suggestions, since I have no clue what else i could try?

Thank you for your time!


There isn't a simple way to do this, but you can try doing a QDir.entryList, and then do a case insensitive search on the results. This will provide you with the correct filename. You'll then need to get the absolutePath for that result.

This should give you the preserved-case for the path/filename.


You can use QFileInfo for that and the function

QString QFileInfo::absoluteFilePath () const will return the absolute file path.

Eg:

QFileInfo yourFileInfo(yourPath);
QString correctedCasePath = yourFileInfo.absoluteFilePath ();

Another advantage is that, yourPath can be a QFile or QString so that you can use it directly with the handle currently you are having. Besides these, there are other operations are also available through QFileInfo that can obtain useful information about the file being referred to..

Hope it helps..

链接地址: http://www.djcxy.com/p/47756.html

上一篇: 将类转换为扩展

下一篇: 我如何得到正确的路径案例?