I'm working on a C# project where I must build paths to various files and folders. These are all under one root folder which I have specified in my Web.config file.
For example:
My paths are correct, but I just want to know what the best practice is for incomplete paths. Should I add a "\" to the end of every folder ("D:\Builds\5.2\" + "test.txt"), or add one to the start of every folder/file I add to the path ("D:\Builds" + "\5.2" + "\test.txt")? Currently, I'm, doing it both ways, and want to choose one uniform way of doing it.
A path is either relative or absolute. An absolute path always contains the root element and the complete directory list required to locate the file. For example, /home/sally/statusReport is an absolute path. All of the information needed to locate the file is contained in the path string.
A file path describes the location of a file in a web site's folder structure. File paths are used when linking to external files, like: Web pages. Images.
Use the Path
class to build up your paths. It will do the right thing.
Performs operations on String instances that contain file or directory path information. These operations are performed in a cross-platform manner.
var full = Path.Combine(baseDir, dirFragment);
Use Path.Combine to concatenate path tokens.
If the path is a file to set/change the extension use Path.ChangeExtension.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With