FOLLOW US
softpcapps Software CODE HELP BLOG



Freeware Open Source Windows Software Applications and Free Online Tools

For Windows 11 : Download and run .NET 3.5 Installer if the application is not running

How to check if filename is legal

Not all characters are allowed in a filename in Windows. For example the : or \ are not allowed.
To quickly check if the filename is legal we can use the FileInfo class. Do something like the following :
	

	public static bool IsLegalFilename(string name)
	{
		try
		{
			System.IO.FileInfo fileInfo = new System.IO.FileInfo(name);
			return true;
		}
		catch
		{
			return false;
		}
	}