What is the best way to combine a path and a filename in C#/.NET? -
What is the best way to add a path to a file name?
The same C: \ foo
and bar.txt
, I want to c: \ foo \ bar.txt
.
C: \ foo
and .. \ bar.txt
, I either have an error or c: \ foo \ bar.txt
should be (so i path.cabine ()
directly). For the same type as c: \ foo
and bar / baz.txt
, send me an error or c: \ foo \ baz.txt
(not c: \ foo \ bar \ baz.txt
).
I know, I can check that the file name does not have '\' or '/', but is it enough? If not, then what is the correct check?
If you want to create a "bad" filename with an error:
< Pre = if the path (filename)! = Filename) {throw a new exception ("'filename is invalid!')}} String joint = path.cobine (dir, filename);
Or, if you want to silently silence the "bad" file names without throwing an exception:
string composite = path.combin (directories, path.getFileName (file name) ));
< / Div>
Comments
Post a Comment