class Program {
static Regex illegalPathChars = new Regex(@"^\.|[\x00-\x1F,\x7B-\x9F,"",#,%,&,*,/,:,<,>,?,\\]+|(\.\.)+|\.$", RegexOptions.Compiled);
static void Main(string[] args)
{
string fileName = "/new%20file.txt";
string validFileName = fileName;
if (GetValidFileOrFolderName(ref validFileName))
Console.WriteLine("A valid file or folder name for '{0}' is '{1}'.", fileName, validFileName);
else
Console.WriteLine("Could not get a valid file or folder name.");
}
public static bool GetValidFileOrFolderName(ref string path)
{
return GetValidFileOrFolderName(ref path, '_');
}
public static bool GetValidFileOrFolderName(ref string path, char replacementChar)
{
if (path == null)
return false;
path = illegalPathChars.Replace(HttpUtility.UrlDecode(path.Trim()), replacementChar.ToString());
if (path.Length > 128)
{
path = path.Substring(0, 128);
return GetValidFileOrFolderName(ref path, replacementChar);
}
return path.Length > 0;
}
}
No comments:
Post a Comment