Original Detects file name case matching under Windows

Source: Internet
Author: User

Cross-platform development there is a well-known, but because only occasionally troubled, ordinary people do not care about the problem, that is, Windows is not sensitive to file name capitalization, and other platforms are sensitive to file name case. As a result, it may be normal for Windows platform development, but deploying/packaging to other platforms will cause the runtime to load files to fail. I am not very concerned about this problem, is generally packaged/deployed when all files are transferred to lowercase save, loading the file will also be the filename to lowercase. Without this approach, the general company also has a process in place to avoid such problems.

But a good process is also required to carry out, if the implementation is not in place, such as the art of uploading resources do not follow the norms, or planning to fill out the specification, there will be problems. So my former colleague asked me if I had any method of detection, so I tried a bit.

The first thing I think about is the name property of FileInfo, which is to do it, but when it is actually used, it is found that if you create fileinfo in lowercase, the Name property of the FileInfo created is also a lowercase name, not the actual name. Then I thought of the step-by-step traversal of the folder, which can always be case-sensitive to find out if it matches. So after several tests, the problem was solved and the final code was as follows:

 Public Static classtools{ Public Static BOOLIscasematch (stringpath) {Path= path. Replace ('\\','/'); string[] pathes = path. Split ('/'); DirectoryInfo dir=NewDirectoryInfo (".");  for(inti =0; I < pathes. Length-1; i++) {directoryinfo[] dirs=dir.            GetDirectories (Pathes[i]); if(dirs. Length = =0)                return false; DirectoryInfo Dir2=NULL;  for(intj =0; J < Dirs. Length; J + +)            {                if(Dirs[j]. Name = =Pathes[i]) {Dir2=Dirs[j];  Break; }            }            if(Dir2 = =NULL)                return false; Dir=Dir2; } fileinfo[] Files= dir. GetFiles (pathes[pathes. Length-1]); if(Files. Length = =0)            return false;  for(inti =0; I < files. Length; i++)        {            if(Files[i]. Name = = Pathes[pathes. Length-1])                return true; }        return false; }}

The current version only detects relative paths and cannot detect absolute paths (actual projects loaded with resources typically do not use absolute paths)

Original Detects file name case matching under Windows

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.