private
static
readonly
Regex InvalidCharsRegex =
new
Regex(@
"[\*\?\|\\\t/:"
"'<>#{}%~&]"
, RegexOptions.Compiled);
Regex InvalidRulesRegex =
"\.{2,}"
Regex StartEndRegex =
"^[\. ]|[\. ]$"
Regex ExtraSpacesRegex =
Regex(
" {2,}"
/// <summary>
/// Returns a folder or file name that
/// conforms to SharePoint's naming restrictions
/// </summary>
/// <param name="original">
/// The original file or folder name.
/// For files, this should be the file name without the extension.
/// </param>
public
string
GetSharePointFriendlyName(
original)
{
// remove invalid characters and some initial replacements
var friendlyName = ExtraSpacesRegex.Replace(
InvalidRulesRegex.Replace(
InvalidCharsRegex.Replace(
original, String.Empty).Trim()
,
"."
)
" "
);
// Check beginning and end for periods and spaces
while
(StartEndRegex.IsMatch(friendlyName))
friendlyName = StartEndRegex.Replace(
friendlyName, String.Empty);
return
friendlyName.Length > 180 ? friendlyName.Substring(0, 180) : friendlyName;
}
Gokan Ozcifci edited Revision 2. Comment: Tag & Title