Was getting the below error while uploading attachment to SharePoint document library.

string pattern = "[\\~#%&*{}/:<>?|\"-]";
string replacement = " ";
Regex regEx = new Regex(pattern);
string sanitized = Regex.Replace(regEx.Replace(input, replacement), @"\s+", " ");
Helpful post
http://simplyaprogrammer.com/2008/05/importing-files-into-sharepoint.html
Hope it helps
Discover more from Nishant Rana's Weblog
Subscribe to get the latest posts sent to your email.

Regex.Replace(tempFileName, “[^a-zA-Z0-9_.]+”, “”, RegexOptions.Compiled);
LikeLike