Change string into a valid file name

Not all possible characters are suitable for file names. For example path separators cannot be used for file names.

This changes any string into a valid file name by replacing the invalid characters with an underscore:

public static string GetValidFileName(string fileName)
{
  foreach (char c in Path.GetInvalidFileNameChars()) 
  { 
    fileName = fileName.Replace(c, '_'); 
  }
 
  return fileName; 
}

 

 

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.