What is the best way to combine a path and a filename in C#/.NET?

If you want “bad” filenames to generate an error:

if (Path.GetFileName(fileName) != fileName)
{
    throw new Exception("'fileName' is invalid!");
}
string combined = Path.Combine(dir, fileName);

Or, if you just want to silently correct “bad” filenames without throwing an exception:

string combined = Path.Combine(dir, Path.GetFileName(fileName));

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)