The Artima Developer Community
Sponsored Link

.NET Buzz Forum
A Path.Combine method that takes any number of arguments

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Darrell Norton

Posts: 876
Nickname: dnorton
Registered: Mar, 2004

Darrell Norton is a consultant for CapTech Ventures.
A Path.Combine method that takes any number of arguments Posted: Dec 28, 2004 12:53 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Darrell Norton.
Original Post: A Path.Combine method that takes any number of arguments
Feed Title: Darrell Norton's Blog
Feed URL: /error.htm?aspxerrorpath=/blogs/darrell.norton/Rss.aspx
Feed Description: Agile Software Development: Scrum, XP, et al with .NET
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Darrell Norton
Latest Posts From Darrell Norton's Blog

Advertisement

Python has a function similar to the .NET Framework’s Path.Combine method, except that Python can take any number of string parameters. I decided to port this to .NET. Here’s the code for a Utils.Path.Combine method that takes a string array of paths and returns the concatenated path:

     1: using System;
     2: using System.Text;
     3: using ioPath = System.IO.Path;
     4:  
     5: namespace Utils
     6: {
     7:     public sealed class Path
     8:     {
     9:         private Path() { }
    10:  
    11:         public static string Combine(params string[] paths) {
    12:             if (paths.Length == 2) return ioPath.Combine( paths[0], paths[1] );
    13:  
    14:             StringBuilder combined = new StringBuilder(50);
    15:             foreach(string path in paths) {
    16:                 if (path == null) throw new ArgumentNullException("path array item");
    17:                 Path.CheckInvalidPathChars(path);
    18:                 if ( path.Length > 0 ) {
    19:                     if (ioPath.IsPathRooted(path)) {
    20:                         combined = new StringBuilder(50).Append(path);
    21:                     }
    22:                     else {
    23:                         char ch1;
    24:                         if (combined.Length > 0) {
    25:                             ch1 = combined[combined.Length - 1];
    26:                         }
    27:                         else {
    28:                             ch1 = path[path.Length - 1];
    29:                         }
    30:                         if (((ch1 != ioPath.DirectorySeparatorChar) && 
    31:                                 (ch1 != ioPath.AltDirectorySeparatorChar)) && 
    32:                                 (ch1 != ioPath.VolumeSeparatorChar)) {
    33:                             combined.Append(ioPath.DirectorySeparatorChar + path);
    34:                         }
    35:                         else {
    36:                             combined.Append(path);
    37:                         }
    38:  
    39:                     }
    40:                 }
    41:             }
    42:             return combined.ToString();
    43:         }
    44:  
    45:         internal static void CheckInvalidPathChars(string path) {
    46:             if (-1 != path.IndexOfAny(System.IO.Path.InvalidPathChars)) {
    47:                 throw new ArgumentException("InvalidPathChars");
    48:             }
    49:         }
    50:  
    51:     }
    52: }

You call it like this:

1: result = Path.Combine( @"C:\ProgramFiles", "someDir", @"anotherDir\", "someFile.txt" );

I tried to call the Path class's public members as much as possible to improve upgradability to the next version of the .NET Framework. The CheckInvalidPathChars internal method is the same as the System.IO.Path class's method of the same name. I used Reflector to check it out.

From my performance testing, the built-in Path.Combine performs about 60 nanoseconds faster on two arguments. For three arguments, the code above performs marginally better than calling Path.Combine twice, once with the first two arguments and once with the result and a third argument. At four arguments or more it really outperforms the built-in Path.Combine. Maybe it's not all that useful, but it was fun to do.

I did use Test-Driven Development, I just didn't show the code. The majority of the effort was in performance tuning, but the test harness kept the code running as intended.


This Blog Hosted On: http://www.DotNetJunkies.com/

Read: A Path.Combine method that takes any number of arguments

Topic: A Random Act of Podcasting Previous Topic   Next Topic Topic: MCMS.Rapid - Beta 2

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use