2 Replies - 87 Views - Last Post: 06 February 2012 - 06:00 PM Rate Topic: -----

Topic Sponsor:

#1 jRaskell  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-February 12

Inconsistency in DirectoryInfo ToString() method

Posted 06 February 2012 - 03:57 PM

Following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo root = new DirectoryInfo(@"c:\temp");
            Console.WriteLine(root);
            Console.WriteLine();

            DirectoryInfo[] folders = root.GetDirectories();
            foreach (DirectoryInfo di in folders)
                Console.WriteLine(di);
            Console.WriteLine();

            DirectoryInfo newFolder = root.CreateSubdirectory("New Folder");
            Console.WriteLine(newFolder);

            Console.ReadLine();
        }
    }
}



Provides the following output (assuming two folders already exist named test1 and test2 in my temp folder):

c:\temp

test1
test2

c:\temp\New Folder

The ToString() method of root and newFolder returns the full path. Where the ToString() method of the array of DirectoryInfos only return the folder name itself.

Is this intended behavior? I would expect ToString() to return the same string for a given folder regardless how the object was instantiated. If this is intended behavior, what was the reasoning behind this intent?

Is This A Good Question/Topic? 0
  • +

Replies To: Inconsistency in DirectoryInfo ToString() method

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 535
  • View blog
  • Posts: 1,506
  • Joined: 04-October 09

Re: Inconsistency in DirectoryInfo ToString() method

Posted 06 February 2012 - 05:33 PM

According to MSDN, ToString "Returns the original path that was passed by the user." The GetDirectories method doesn't include the full path, so ToString doesn't include the full path. Working as intended.
Was This Post Helpful? 0
  • +
  • -

#3 jRaskell  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-February 12

Re: Inconsistency in DirectoryInfo ToString() method

Posted 06 February 2012 - 06:00 PM

I was referring more to the differences between the different methods of DirectoryInfo that return other DirectoryInfo objects. GetDirectories() doesn't include the full path, but CreateSubdirectory() does. Both were called from root, the latter appears to have inherited roots original path, while the former clearly did not.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1