21

Writing some code in C#, I was wondering if there was a way to get the correct path of a directoryinfo object?

Currently I have, for example, a directory such as:

DirectoryInfo dirInfo = new DirectoryInfo(pathToDirectory);

The issue is that if I want to get the path of that specific dirInfo object, it always returns the debug path (bin folder). If the original dirInfo object is referencing a directory in the D:\testDirectory path, then I want a way to get that path again somewhere else in the code instead of getting \bin\debug\testDirectory

Is there any way to do this?

Currently I am trying to get the path of dirInfo using Path:

Console.WriteLine("Path: " + Path.GetFullPath(dirInfo.ToString()));
Giardino
  • 1,367
  • 3
  • 10
  • 30
  • 1
    What is pathToDirectory? Is it D:\testDirectory or the debug path? – Justin Pihony Nov 19 '13 at 17:51
  • how are you getting the path for `dirInfo` ? – Habib Nov 19 '13 at 17:52
  • Can you please show code that you use to get "path of `DirectoryInfo`"? And for sample code try to use constant values where possible (i.e. what is value of `pathToDirectory` when your code does not work) – Alexei Levenkov Nov 19 '13 at 17:52
  • @JustinPihony pathToDirectory is the string @"D:\testDirectory" – Giardino Nov 19 '13 at 17:52
  • @AlexeiLevenkov Edited my code to show how I am trying to get the path. – Giardino Nov 19 '13 at 17:53
  • so what are you getting if you try to print the path on console? – Sudhakar Tillapudi Nov 19 '13 at 17:55
  • so what is the exact problem , are you not able to get the proper path which was assigned to DirectoryInfo? – Sudhakar Tillapudi Nov 19 '13 at 17:57
  • 3
    You are either not showing your code OR don't know what is passed to it: `Path.GetFullPath(new DirectoryInfo(@"D:\testDirectory ").ToString())` returns "D:\testDirectory" as expected. Please double check your sample. – Alexei Levenkov Nov 19 '13 at 17:58
  • This isn't as silly as it might sound to some, I've had a similar problem: If you create a folder using `Directory.CreateDirectory("some\\relative\\pathtest")` it returns a `DirectoryInfo` instance, _but_ if you call `.ToString()` on it then it will _only_ return the same as the Name property, in this case: `"pathtest"` - which is really weird, you would expect CreateDirectory to pass on the original string - but it doesn't! – AnorZaken Apr 19 '16 at 05:25
  • _...cont:_ Actually looking at the latest reference source it looks like they have fixed this? When I wonder - .net4.0 or 4.5? (Hasty look at source: appears it returns the full path now - so still not what you passed, but a lot better at least.) – AnorZaken Apr 19 '16 at 05:34

2 Answers2

49

Try this.

string pathToDirctory = "D:\\testDirectory";
DirectoryInfo dirInfo = new DirectoryInfo(pathToDirctory);
string path = dirInfo.FullName;
Console.WriteLine(path);
Muhammad Umar
  • 3,761
  • 1
  • 24
  • 36
  • @user1806716 - note that this code behaves exactly the same as code that you claim to have - [DirectoryInfoToString()](http://msdn.microsoft.com/en-us/library/system.io.directoryinfo.tostring(v=vs.110).aspx) "Returns the original path that was passed by the user." – Alexei Levenkov Nov 19 '13 at 18:00
  • @AlexeiLevenkov: i think OP is sending the "testDirectory" relative path hence it is getting added to the current path(dubug folder). – Sudhakar Tillapudi Nov 19 '13 at 18:01
  • @user1806716 6 minutes... :) – Sergey Sep 26 '19 at 23:36
3

A DirectoryInfo represents a particular directory. When you create it, what directory it represents is dependent on the path you give it. If you give it an absolute path like c:\foo\bar\baz\bat, that's the directory you get. If, on the other hand, you give it a relative path, like foo\bar\baz\bat, the path is relative to the process' current working directory. By default, that is inherited from the process that spawned the current process. Visual Studio starts a debug session and sets the CWD of the process being debugged to its bin directory. So if you create a DirectoryInfo and give it a path like testDirectory, you will get a DirectoryInfo about [project-root]\bin\Debug\testDirectory.

If you want an absolute path, you'll have to specify that absolute path. There aren't any shortcuts.

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
  • so here OP must be giving the Relative path to the DirectoryInfo instead of absolute path that is why he is getting path as=> projectfolder/bin/debug/DitestDirectory right? – Sudhakar Tillapudi Nov 19 '13 at 18:05