FOLLOW US
softpcapps Software CODE HELP BLOG

Shareware and free Open Source Windows Software Applications and free Online Tools

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;
		}
	}