views:

101

answers:

5

Hi

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 }

+4  A: 

Unfortunate as it is, string is the idiomatic way of doing this in .NET - if you look at things like FileStream constructors etc, they use strings.

You could consider using FileInfo (or DirectoryInfo) but that would be somewhat unusual.

Jon Skeet
That is the thing, you will need to use 2 objects just for the location and the file name. However is the safety that would give you not worth the overhead in memory? I realize that strings is the norm, but is that correct?
Rihan Meij
@Rihan: It depends - what safety are you thinking about, exactly? I'm not sure that `FileInfo` does much validation. I suggest you think about what mistakes you want to prevent, and see whether `FileInfo` actually helps you or not.
Jon Skeet
+1  A: 

You could use FileInfo (from System.IO) to pass it around, but strings are more or less standard when referring to files.

You could always use Path.GetFileName({yourstring}) to get the filename from the path.

riffnl
+6  A: 

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....

stupid-phil
Hmm I am trying to avoid other people using my code not make mistakes, would regular expressions not increase the likely hood of my code failing?
Rihan Meij
I think you have a point. My personal approach here would not be to validate the filename in your method - but those were the two validation ideas that came to mind. The user of your method should, arguably, ensure they pass a valid filename to your method, or expect an exception to be raised.
stupid-phil
A: 

String is fine, but you should put in some effort to ensure that the file is being saved into the directory you expect.

Bernard Chen
A: 

If you're working with a filepath a string is usual.

If you're working with a URL you could consider using the System.Uri class e.g.

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

This will allow you to work with properties such as uri.Host, uri.Absolute path etc. It will also give you a string array (Segments) for the separate subfolders in the url.

MSDN info here: System.Uri

B.Mo
Well that is exactly the reason why I asked this question. I have been working with URL's as strings because of legacy code, and then got the chance to implement some new functionality, and changed a section of the code, to not use strings, but rather a Uri. And it makes the code so much more readable and robust. I am just surprised that will still treat a location to a file on disk as a string. I guess that is just the way it is at this point.
Rihan Meij
Filenames as strings are the norm. Don't forget that you can use System.IO.Path to more safely manipulate those strings, e.g. Path.Combine(), Path.GetFileName() and Path.GetFullPath()...
B.Mo