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));

How can I create a full path to a file from parts (e.g. path to the folder, name and extension)?

This works fine: os.path.join(dir_name, base_filename + ‘.’ + filename_suffix) Keep in mind that os.path.join() exists only because different operating systems use different path separator characters. It smooths over that difference so cross-platform code doesn’t have to be cluttered with special cases for each OS. There is no need to do this for file name “extensions” … Read more

How to make an NSString path (file name) safe

This will remove all invalid characters anywhere in the filename based on Ismail’s invalid character set (I have not verified how complete his set is). – (NSString *)_sanitizeFileNameString:(NSString *)fileName { NSCharacterSet* illegalFileNameCharacters = [NSCharacterSet characterSetWithCharactersInString:@”/\\?%*|\”<>”]; return [[fileName componentsSeparatedByCharactersInSet:illegalFileNameCharacters] componentsJoinedByString:@””]; } Credit goes to Peter N Lewis for the idea to use componentsSeparatedByCharactersInSet: NSString – Convert … Read more

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