I'm building a .Net Core 2.0 console application that will run on both Windows and Ubuntu systems. I have a string that needs to be converted into a safe file name. Currently I'm using the following code to achieve this:
var safeName = string.Join("-", name.Split(Path.GetInvalidFileNameChars()));
It works, but it will produce different results on different operating system as Linux allows characters that Windows will not allow. I like a solution that produces the same result on all of the systems.
Is there a cross platform version of GetInvalidFileNameChars
that will return characters for all platforms?
NET Core is cross-platform. It runs on Windows, OS X and multiple distributions of Linux.
. NET Core is an open-source and free framework for creating cross-platform apps targeting Linux, Windows, and macOS. It can run applications on the cloud, the IoT, and devices. 4 cross-platform scenarios are supported by it; namely, command-line apps, ASP.NET Core Web apps, libraries as well as Web APIs. .
With this framework, programmers can write code once and have it run on multiple operating systems and environments such as Linux, macOS, and Windows. Applications based on . NET Core technology are standalone and contain the required runtime for execution thus making them platform independent.
I don't think there is a "cross-platform" version in the BCL, but you can easily make one yourself by copying the Windows version of GetInvalidFileNameChars, because the Unix version is just a subset of these and they're not likely to change often:
public static char[] GetInvalidFileNameChars() => new char[]
{
'\"', '<', '>', '|', '\0',
(char)1, (char)2, (char)3, (char)4, (char)5, (char)6, (char)7, (char)8, (char)9, (char)10,
(char)11, (char)12, (char)13, (char)14, (char)15, (char)16, (char)17, (char)18, (char)19, (char)20,
(char)21, (char)22, (char)23, (char)24, (char)25, (char)26, (char)27, (char)28, (char)29, (char)30,
(char)31, ':', '*', '?', '\\', '/'
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With