0

The legit path is: @"S:\Personal Folders\UserA\test

Why does the Directory.Exists return True on below path?

@"S:\Personal Folders\UserA\\\\\\\\\\\\\\\\test

Or even more slashes.

PS:I tested with File.Move, it was not recognizing that as a valid path.

LONG
  • 4,490
  • 2
  • 17
  • 35
  • 1
    The bigger question: why use Directory.Exists() at all? In most cases it's MUCH slower (adds an extra file system lookup from disk), and doesn't save you from still needing an exception handler. – Joel Coehoorn Dec 18 '18 at 20:51
  • or https://stackoverflow.com/questions/9294133/full-path-with-double-backslash-c – gunr2171 Dec 18 '18 at 20:51
  • @JoelCoehoorn - Much slower than what? Sorry but I feel like I'm missing a piece of context from your comment. – Broots Waymb Dec 18 '18 at 20:55
  • 1
    @BrootsWaymb Slower than just using the untested path and letting the exception happen. As slow as exceptions are, file system access is WAY worse. – Joel Coehoorn Dec 18 '18 at 20:56
  • @JoelCoehoorn: Really? So it's faster to do a file operation and have it possibly fail and throw than to wrap the operation in an if/exists. Hmmm... Who knew? I guess since the if/exists gets called on both the success and failure situations, where the exception only occurs on failure, skipping the test optimizes the success path. – Flydog57 Dec 18 '18 at 21:28
  • @Flydog57 Yes, it's better for success. For the failure path, remember there are two failure modes to think about when using `Exists()`: one where you fail because your test returned "false" (costs one disk access) and one where you fail for a reason reason `.Exists()` couldn't handle (costs the disk access _and_ the exception). There are several reasons this can happen (permissions, network or fs lock, file system redirector, volatile file system, etc). You still need the exception handler; `Exists()` doesn't save writing that code. – Joel Coehoorn Dec 18 '18 at 22:40

1 Answers1

1

Path with slashes still satisfied Absolute Path standards.

According to the Docs,

The path parameter is permitted to specify relative or absolute path information.

Edit: Reference for Path Normalization

Almost all paths passed to Windows APIs are normalized. During normalization, Windows performs the following steps:

  • Identifies the path.
  • Applies the current directory to partially qualified (relative) paths.
  • Canonicalizes component and directory separators.
  • Evaluates relative directory components (. for the current directory and .. for the parent directory).
  • Trims certain characters.

This normalization happens implicitly, but you can do it explicitly by calling the Path.GetFullPath method, which wraps a call to the GetFullPathName() function. You can also call the Windows GetFullPathName() function directly using P/Invoke.

Edit

Not Every function normalizes paths by default.

File.Move by myself gives error for this path.

on the other hand,

var parsedPath = Path.GetFullPath("S:\\Personal Folders\\UserA\\\\\\\\\\\\\\\\test");
File.Move(parsedPath, ...);

does not.

Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72