Possible hiccups with Path.FullGetPath() and Directory.GetDirectoryRoot()

Sep 9, 2011 at 8:21 AM
Edited Sep 9, 2011 at 8:22 AM

Both of these behave differently to what I expected, also different to their counterparts in System.IO.Path and System.IO.Directory.
I am in Windows 7.0 64bit, Visual studio 2010.

GetDirectory Root doesnt seem to return the trailing '\'

FullGetPath doesnt address the fact that "C:" is an alias to current directory on device with root at C:\

Below is the output of a simple test case I wrote up for it.

0 Directory.GetCurrentDirectory() D:\Data\DevVS\Catalog\src\AlphaFSTest\bin\Release
0 Alphaleonis.Win32.Filesystem.Path.GetFullPath(@"C:") C:
0 System.IO.Path.GetFullPath(@"C:") C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE
0 Alphaleonis.Win32.Filesystem.Directory.GetDirectoryRoot(@"C:") C:
0 System.IO.Directory.GetDirectoryRoot(@"C:") C:\

1 Directory.GetCurrentDirectory() C:\Windows
1 Alphaleonis.Win32.Filesystem.Path.GetFullPath(@"C:") C:
1 System.IO.Path.GetFullPath(@"C:") C:\Windows

2 Directory.GetCurrentDirectory() C:\
2 Alphaleonis.Win32.Filesystem.Path.GetFullPath(@"C:") C:
2 System.IO.Path.GetFullPath(@"C:") C:\

3 Directory.GetCurrentDirectory() D:\Data\DevVS\Catalog\src\AlphaFSTest\bin\Release
3 Alphaleonis.Win32.Filesystem.Path.GetFullPath(@"C:") C:
3 System.IO.Path.GetFullPath(@"C:") C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE

 

 

Forgot to mention, stumbled on this library a while back and am using it for a simple little disk catalog app I am toying with. Overall its a very nice little library.

Thanks :).

 

Sep 12, 2011 at 5:16 PM
Let me get it to you tonight.

My Windows is coughing right now after too many beta apps been installed.


Sincerely,
Alexandr Normuradov



On 9 September 2011 02:21, rluiten <notifications@codeplex.com> wrote:
> From: rluiten
>
> Both of these behave differently to what I expected, also different to their
> counterparts in System.IO.Path and System.IO.Directory.
> I am in Windows 7.0 64bit, Visual studio 2010.
>
> GetDirectory Root doesnt seem to return the trailing '\'
>
> FullGetPath doesnt address the fact that "C:" is an alias to current
> directory on device with root at C:\
>
> Below is the output of a simple test case I wrote up for it.
>
> 0 Directory.GetCurrentDirectory()
> D:\Data\DevVS\Catalog\src\AlphaFSTest\bin\Release
> 0 Alphaleonis.Win32.Filesystem.Path.GetFullPath(@"C:") C:
> 0 System.IO.Path.GetFullPath(@"C:") C:\Program Files (x86)\Microsoft Visual
> Studio 10.0\Common7\IDE
> 0 Alphaleonis.Win32.Filesystem.Directory.GetDirectoryRoot(@"C:") C:
> 0 System.IO.Directory.GetDirectoryRoot(@"C:") C:\
>
> 1 Directory.GetCurrentDirectory() C:\Windows
> 1 Alphaleonis.Win32.Filesystem.Path.GetFullPath(@"C:") C:
> 1 System.IO.Path.GetFullPath(@"C:") C:\Windows
>
> 2 Directory.GetCurrentDirectory() C:\
> 2 Alphaleonis.Win32.Filesystem.Path.GetFullPath(@"C:") C:
> 2 System.IO.Path.GetFullPath(@"C:") C:\
>
> 3 Directory.GetCurrentDirectory()
> D:\Data\DevVS\Catalog\src\AlphaFSTest\bin\Release
> 3 Alphaleonis.Win32.Filesystem.Path.GetFullPath(@"C:") C:
> 3 System.IO.Path.GetFullPath(@"C:") C:\Program Files (x86)\Microsoft Visual
> Studio 10.0\Common7\IDE
>
> Read the full discussion online.
>
> To add a post to this discussion, reply to this email
> ([email removed])
>
> To start a new discussion for this project, email
> [email removed]
>
> You are receiving this email because you subscribed to this discussion on
> CodePlex. You can unsubscribe or change your settings on codePlex.com.
>
> Please note: Images and attachments will be removed from emails. Any posts
> to this discussion will also be available online at codeplex.com
Sep 13, 2011 at 5:37 AM

thanks for using it, any feedback is very welcome.

we had this conversation before about the trailing slash. You see MS approach to is very conservative and over accumulated the bizarre file system API. So we decided to keep it simple and consistent without a trailing slash.

As many WIN32 functions return names without trailing slash, so there is no need to append any extra, that's the reasoning.

Sep 13, 2011 at 5:49 AM

While I agree MS api isnt exactly a consistent place to be.

D: and D:\ do not have the same meaning.
Without the trailing slash it means current dir on D: device.
With the slash it means root of D: device regardless of current directory on the device.

So if current dir device D:  is  D:\DirA\DirB.
Then D:\ and D: are actualy different paths.

And returning D: as the Root of any path is not going to be correct.

So I am not sure how you can make the choice it isnt required.

I have a couple of helper functions so its not getting in my road at all, just thought id mention it, since i did create helper functions to get less surprising behaviour. And i really wanted to let someone provide a parameter of "D:" to specify the current directory on device D: as the starting point for activity.

 

Sep 20, 2011 at 5:16 AM

Can you please provide some code samples so we can get a much more clear picture of how do you use them and what are the end consuming methods of dir root results?

Sep 28, 2011 at 2:17 PM

Sorry for my tardy response.

At the moment I have created a command line utility that allows me to capture a file system catalog from a given path. It then allows searching these captured catalogs an also processing catalogs to find duplicate files and calculating MD5 checksums of candidate duplicate files.

Given that its a command line tool I use the short form of Drive Letter Colon to mean current directory on drive D: (or other drive letter).

eg. "catutil --scan D:"

This would capture the current directory file system heirarchy on D: and not the root of D:\ as such.

The code takes the parameter being the path to capture a folder heirachy from.

And converts it to a more canonical form - ie to the full path on the device specififed.

public string CanonicalPath(string path)
{
	path = GetFullPath(path);

	var volumeRoot = GetDirectoryRoot(path);

	if (IsUnc(path))
	{
		if (!path.EndsWith(Path.DirectorySeparatorChar))
		{
			path = path + Path.DirectorySeparatorChar;
		}
	}
	else
	{
		if (path.EndsWith(Path.DirectorySeparatorChar) && volumeRoot != path)
		{
			path = path.TrimEnd(Path.DirectorySeparatorChar.ToCharArray());
		}
		path = char.ToUpper(path[0]) + path.Substring(1);
	}
	
	return path;
}

I am not aware of another means to convert a "D:" to a D:\Whatever\Current\Directory\Path\Currently\Is" so was using System.IO.Path.GetFullPath().

When I switched to AlphaFS, this no longer let me find out what the full current directory was on the device.

The work around I use was to create my own GetFullPath that uses System.IO.Path.GetFullPath() for Short Device Letter Colon format strings.

Which hasnt caused me any pain at the moment.

The extension of this to the result of GetDirectoryRoot() return D: without a backslash hasnt got the same meaning as D:\.

Hope this clears things up to the use case.

If something is unclear or if im just missing some obvious point please let me know :).

I like AlphaFS its pretty nice to use, and has some nice convenience methods as well.

 

Oh btw the I just remembered. I had a hicup with Directory.GetFiles() Method as well, I am not sure its a bug just a surprise for me.

const string name1 = "G-SN750B_02_S13UJ1NQ221583.cde";
const string name2 = "G-SN750B_02_S13UJ1NQ221583.cde-backup-with-hash";
var f1 = File.Create(name1);
var f2 = File.Create(name2);
f1.Close();
f2.Close();
var files = Directory.GetFiles(".", "*.cde", SearchOption.TopDirectoryOnly);

This code lists both of the files name1, name2 much to my initial surprise. 

I have another helper that now hides this and ensure there is no characteres beyond the end of the search Pattern parameter.

 


Sep 30, 2011 at 4:05 AM

I checked the second part about Directory.GetFiles and it's not true so far, it works as expected with only one entry in files array: G-SN750B_02_S13UJ1NQ221583.cde

As of the Path methods.

Most of them operate on strings for speed and do not make extra system calls to figure out where is what. Because take into account the LongPath notation that is very different from convenient C:\ or C:

As time goes on and many more use cases emerge we can definitely come up with some more convenient approach.

Also as this whole library is opened source you can definitely submit a patch when you feel everyone will benefit.

I just did a patch for some other new bug that just came out with recent windows updates. So the library is alive as long as it gets feedback :)