Home » Android » c# – Visual Studio – Renaming Files From Uppercase to Lowercase-Exceptionshub

c# – Visual Studio – Renaming Files From Uppercase to Lowercase-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment


Short Version

I found myself losing alot of time renaming + including files in my Xamarin.Forms Projects because any change from Upercase to Lowercase (or vice versa) only in the files, will result in no change what so ever in the Project file, so i have a few questions:

  • Is there an option in Visual Studio that makes it take into account file renaming from Upercase to Lowercase? (See Edit Below)
  • What’s the best way of changing alot of files from Upercase to Lowercase without delete/re-adding them?
  • Is renaming the file includes in the .csproj directly a Good Practice? if not, what is the best for this kind of scenarios?

Longer Version + Adicional Info

I had to include over 2000 images (Android + iOS and their respective sizes). So i started copying the files into the correct directory and include them in the Mobile Projects, the problem came when i already included the files in the project and some of the files had Uppercase letters that i haven’t noticed before, so i made changes by hand to all the files, when i noticed that those changes weren’t reflected in the Solution Explorer/Project File, i tried manually and got this error:

enter image description here

Edit: It seems this issue was apparently resolved in VS2019 arround v.16.1, but only if the file wasn’t renamed externally. Since i did that i got this warning. This anwsers my first question.

And one solution that i found was renaming like: Foo.png > fooo.png > foo.png

But that would be exponentialy time-consuming by the number of files i had to edit, so i made this piece of code:

string filepath = @"C:\Users\(...)";
DirectoryInfo d = new DirectoryInfo(filepath);

foreach (var file in d.GetFiles("*.png", SearchOption.AllDirectories))
    if (file.Name.Any(char.IsUpper))
        File.Move(file.FullName, file.FullName.Replace(file.Name, file.Name.ToLower()));

What it does is basically create a new file but with Lowercase, that means i still have to Remove the old References and Include the new Files. This doesn’t seem right since a simple rename would do.

What’s my go to option here?

How to&Answers:

Windows as operating system is ascendant of the operating system where there was no difference in lower and uppercase. As such at today’s state Windows treats the files with same letters as the same though technically it can remember and display lower and upper cases in file names.

Overall it means your request is not natural in Windows. Maybe someone can provide you with some hack, but if you want to resolve this problem quickly move your project to the Mac where this works differently at the operating system level, perform your operation in Visual Studio for Mac and then you can continue to use Windows if you prefer.

EDIT: Actually I can tell you one hack for Windows. First rename file to whatever you want (like add 1 at the end) and then rename it to the desired file name. It will work properly.