You should now use the following characters ? < > | : \ / * "
? < > | : \ / * "
string PathFix(string path) { List<string> _forbiddenChars = new List<string>(); _forbiddenChars.Add("?"); _forbiddenChars.Add("<"); _forbiddenChars.Add(">"); _forbiddenChars.Add(":"); _forbiddenChars.Add("|"); _forbiddenChars.Add("\\"); _forbiddenChars.Add("/"); _forbiddenChars.Add("*"); _forbiddenChars.Add("\""); for (int i = 0; i < _forbiddenChars.Count; i++) { path = path.Replace(_forbiddenChars[i], ""); } return path; }
Tip. You cannot include double quotation mark ( "
), but you can include 2 quotation mark ( ''
). In this case:
string PathFix(string path) { List<string> _forbiddenChars = new List<string>(); _forbiddenChars.Add("?"); _forbiddenChars.Add("<"); _forbiddenChars.Add(">"); _forbiddenChars.Add(":"); _forbiddenChars.Add("|"); _forbiddenChars.Add("\\"); _forbiddenChars.Add("/"); _forbiddenChars.Add("*"); //_forbiddenChars.Add("\""); Do not delete the double-quote character, so we could replace it with 2 quotes (before the return). for (int i = 0; i < _forbiddenChars.Count; i++) { path = path.Replace(_forbiddenChars[i], ""); } path = path.Replace("\"", "''"); //Replacement here return path; }
Of course, you will use only one of them (or combine them with one function with the bool parameter to replace the quote, if necessary)
source share