It's been a hellacious week year at the office so far.
<tangent>
Firefox's spiffy spellchecker thinks that 'hellacious' isn't a real word. It is. I was using the word instead of the less elegant alternative, 'helluva', but now that I've actually looked it up, I see that the definitions fit even better than I had intended. Particularly "formidably difficult" and "distasteful and repellant." We now return you to the blog post in progress.
</tangent>
Today's major issues revolved around a web application update that was deployed last night. The point of this post is to focus on just one of the specific issues and how it could have very easily been prevented. For the record, I'm not involved in the development or deployment of this application; I just stepped in today when SHTF.
The issue was that the application (an ASP.NET site written in C#) was looking for client-specific uploaded files in directories such as:D:\application\uploadsClientA
As you can probably guess, the ACTUAL paths were like this:D:\application\uploads\ClientA
This path was built dynamically in code by concatenating the upload destination (D:\application\uploads) stored in a configuration file with the name of the client currently logged in to the application (ClientA). This was done using basic string concatenation, and since the path in the config file didn't have a trailing slash, the resulting path did not exist.
The .NET Framework provides a very handy method in the System.IO namespace for building directory paths dynamically: Path.Combine(string path1, string path2). This method handles the insertion of slashes where needed, idiot-proofing the path concatenation without any extra code on your part. The developer's code SHOULD have looked something like this:string path = Path.Combine( Config["UploadDirectory"], clientname );
Then it wouldn't matter whether the UploadDirectory value in the config file had a trailing slash or not.
I guess the moral of the story is that the Framework is full of handy methods that handle many common scenarios and take out a lot of the guesswork (and reduce the amount of code YOU have to write and maintain). The trick is to find these methods and use them.
Wednesday, February 07, 2007
Building Directory Paths Dynamically
Posted by
jwyse
at
9:52 PM
Labels: .NET, Development, Tips
Subscribe to:
Post Comments (Atom)
1 comments:
Keep posting those handy little code lines!!!
Sometimes the only way we learn about them is when someone tells us. :-)
(FYI: The code was not mine either)
Post a Comment