Fixing the problem with File Paths

There are often occasions when you have to work with the FILE Paths in your projects. There are problems with getting the right Directory Paths. And who can say they don’t have a problem with the trailing slashes.

.Net provides an API in the System.IO.Path Class. The Combine method of this class serves as solution to all such problems.  

 

This has very simple straight-forward syntax:

 

COMBINE ( System.String path1 , System.String path2 )

 

The good thing about this method is that, the second parameter can be an absolute path or only the filename.

 

For e.g.:  If you have a file “Tip.txt” in “C:\\Test” directory.

              You could use this function as:

                        strFilePath = System.IO.Path.Combine("C:\\ Test", Tip.txt);

                                                OR

strFilePath = System.IO.Path.Combine("C:\\ Test\\", Tip.txt);

                                                OR

                        strFilePath = System.IO.Path.Combine("C:\\ Test", “C:\\Test\\Tip.txt”);

 

All of them yield the same result. And the problem of trailing slash is removed.

By [)ia6l0 iii   Popularity  (916 Views)