Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a more correct type for passing in the file path and file name to a method

What I mean by this question is, when you need to store or pass a URL around, using a string is probably a bad practice, and a better approach would be to use a URI type. However it is so easy to make complex things more complex and bloated.

So if I am going to be writing to a file on disk, do I pass it a string, as the file name and file path, or is there a better type that will be better suited to the requirement?

This code seems to be clunky, and error prone? I would also need to do a whole bit of checking if it is a valid file name, if the string contains data and the list goes on.

private void SaveFile(string fileNameAndPath) { //The normal stuff to save the file }

like image 656
Rihan Meij Avatar asked May 27 '10 06:05

Rihan Meij


1 Answers

A string is fine for the filename. The .Net Framework uses strings for filenames, that seems fine.

To validate the filename you could try and use a regular expression or check for invalid characters against System.IO.Path.GetInvalidFileNameChars. However, it is probably easier to handle the exceptional cases of invalid filenames by handling the exception that will occur when you try and create the file - plus you need to do this anyway....

like image 75
philiphobgen Avatar answered Nov 15 '22 08:11

philiphobgen