I'm wondering if there's any inbuilt functionality available in .Net Core using which I can check if the file name contains illegal characters in a platform (OS) neutral way.
There's Path.GetInvalidFileNameChars which will give me the list of invalid characters however the list returned is specific to the platform on which the code is executed. For example, when I run the sample code mentioned in the link on Mac and Windows, I get different set of invalid characters based on the OS where I am running the code (rightly so).
The issue with this is a user may have run the code on Mac and created a file (let's say the user has saved the file in cloud) with a name that is invalid in Windows and then try to download that file on a Windows machine.
I am wondering if there's anything available in .Net Core which will help me with this or do I have to roll out my own solution by writing some kind of regular expression. I found one such example here: https://richjenks.com/filename-regex/.
Here's the code I used from Microsoft's documentation:
using System;
using System.IO;
namespace PathExample
{
class GetCharExample
{
public static void Main()
{
// Get a list of invalid path characters.
char[] invalidPathChars = Path.GetInvalidPathChars();
Console.WriteLine("The following characters are invalid in a path:");
ShowChars(invalidPathChars);
Console.WriteLine();
// Get a list of invalid file characters.
char[] invalidFileChars = Path.GetInvalidFileNameChars();
Console.WriteLine("The following characters are invalid in a filename:");
ShowChars(invalidFileChars);
}
public static void ShowChars(char[] charArray)
{
Console.WriteLine("Char\tHex Value");
// Display each invalid character to the console.
foreach (char someChar in charArray)
{
if (Char.IsWhiteSpace(someChar))
{
Console.WriteLine(",\t{0:X4}", (int)someChar);
}
else
{
Console.WriteLine("{0:c},\t{1:X4}", someChar, (int)someChar);
}
}
}
}
}
UPDATE
To elaborate the use case, let's take a fictitious example: Let's consider an application which connects a content creator with a content consumer. A content creator creates some content (say a video file) and uploads it into the application. The actual content is stored in some kind of cloud object storage and the metadata about the content (name, size etc.) is stored in some database. Let's say the content creator creates uses this application on Mac. Now a content consumer (on a Windows machine) views this content and wants to download this content and save the file with the same name as given by content creator. If the content creator gives a file name which is invalid on a Windows machine, the content consumer will not be able to save the file with the same name.
What I want to do is prevent the content creator giving a name to the file which may not be valid for all the platform and force that person to give a file name which is safe across all the platforms at the time of saving the content.
I am hoping that there's something in .Net Core that can handle this scenario but I am getting a feeling that there's none and I have to roll out my own solution for that purpose.