The following are the special characters which are not allowed in filename.
\/:*?"<>|#%&.{}~
Note: Period(.) is allowed but as filename can't be start or end with it and consecutive periods(.. or ...) are not allowed, considering it as invalid char for filename.
using System;
using System.Linq;
private static string GetValidFileName(string rawFileName)
{
string fileName = rawFileName;
//special chars not allowed in filename
string specialChars = @"\/:*?""<>|#%&.{}~";
//Replace special chars in raw filename with empty spaces to make it valid
Array.ForEach(specialChars.ToCharArray(), specialChar => fileName = fileName.Replace(specialChar, ' '));
fileName = fileName.Replace(" ", string.Empty);//Recommended to remove the empty spaces in filename
return fileName;
}
To know more about filename constraints, look at the detailed post here.
You could use Path.GetInvalidPathChars instead of hardcode the characters.
ReplyDeleteeven better -> Path.GetInvalidFileNameChars
DeleteThat's not sufficient. SharePoint has additional characters, that are not allowed, not mentioned in "InvalidFileNameChars"
Deletevar invalidChars = Path.GetInvalidFileNameChars();
var additionalInvalidSharePointSpecialCharacters = new char[7] { '~', '#', '%', '&', '{', '}', '+' };
// the plus sign '+' does not cause an error, but it is mentioned here to be not allowed: http://support.microsoft.com/kb/905231/en-us
invalidChars = invalidChars.Concat(additionalInvalidSharePointSpecialCharacters.AsEnumerable()).ToArray();
var invalidCharsRemoved = fileName.Where(x => !invalidChars.Contains(x)).ToArray();
After that, you have to remove consecutive dots in the filename and dots at the end and beginning, as mentioned by Murali and in the kb article.
The best options which I have come across and use for a long time are:
ReplyDeleteprivate static string GetValidFileName(string fileName)
{
// remove any invalid character from the filename.
return Regex.Replace(fileName.Trim(), "[^A-Za-z0-9_. ]+", "");
}
private static string GetValidFileName(string fileName)
{
return Path.GetInvalidFileNameChars().Aggregate(fileName, (current, c) => current.Replace(c.ToString(), string.Empty));
}
Its explained in this post:
http://codeskaters.blogspot.ae/2013/11/c-remove-invalid-filename-characters.html