FOLLOW US
4dots Software CODE BLOG

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


 

Please Donate!

Please Share!