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

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

So, if I am going to write to a file on disk, am I passing him a string as the file name and file path, or is there a better type that is better suited for this requirement?

Does this code seem awkward, and error prone? I will also need to check if this is a valid file name, if the row contains data, and the list goes on.

private void SaveFile (line fileNameAndPath) {// Normal material for saving the file}

+5
source share
5 answers

A string is great for a file name ... The Net Framework uses strings for filenames, which seems great.

To check the file name, you can try and use a regular expression or check for invalid characters in System.IO.Path.GetInvalidFileNameChars. However, it is probably easier to handle exceptional cases of invalid file names, handling the exception that will occur when trying to create a file, plus you need to do this anyway ....

+5
source

Unfortunately, since this is a string, this is an idiomatic way to do this in .NET. If you look at things like constructors FileStream, etc., they use strings.

FileInfo ( DirectoryInfo), .

+4

FileInfo ( System.IO), , .

Path.GetFileName({yourstring}), .

+1

, , , .

0

, .

URL-, System.Uri, .

Uri myUri = new Uri(myUrl, UriKind.Absolute);

, uri.Host, uri.Absolute .. () URL-.

MSDN : System.Uri

0
source

All Articles