Validate File Name in VB.Net

bool IsValidFilename(string testName)
{
    Regex containsABadCharacter = new Regex("[" + Regex.Escape(System.IO.Path.InvalidPathChars) + "]");
    if (containsABadCharacter.IsMatch(testName) { return false; };

    // other checks for UNC, drive-path format, etc

    return true;
}

Change .Net Framework in VB.Net


  • Go To Project Property
  • Click "Compile"
  • Click "Advanced Compile Options"
  • Select Framework from the "Target Framework"