1

I am writing an application that needs to create a lot of folders. I ran into this stubborn error that I couldn't understand. I was getting DirectoryNotFoundException, which states in the exception message the exact string path that failed. I could copy paste that path into a file explorer bar and it would take me right there. My visual studio solution would fail at a line like this every time. I copy pasted the text into linqpad to isolate it and figure out what it didn't like. The exact same code works without issue in linqpad. I became very confused. I went down the rabbit hole for a while until I realized it was just the space at the end of a folder that it didn't like. Problem solved and lesson learned. I have now added that to some path sanitation functions I have.

What I don't get is why it works in Linqpad, but not VS. Linqpad 5 uses Roslyn and it works there. Linqpad 4 uses CSharpCodeProvider and it works there. Visual Studio uses CSC.exe and it does not work there. I have no idea how these vary.

Can anyone help me understand why they differ in this case? I'd like to know when I should take tests in Linqpad with a grain of salt(maybe always?).

The code below fails at CreateDirectory when running in Visual Studio. It succeeds when run in LinqPad 4 or 5. Code in question:

try
{
    string myDocsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    Directory.CreateDirectory(Path.Combine(myDocsPath, "SpaceOnTheEnd ", "NoSpaceOnTheEnd"));
    Console.WriteLine("Creation succeeded.");
}
catch (Exception ex)
{
    if (ex is DirectoryNotFoundException)
    {
        Console.WriteLine("Creation failed due to space.");
    }

    throw;
}
OsakaRhymes
  • 96
  • 1
  • 8
  • 1
    The code above works fine for me (directory is created) in both LinqPad 5 AND Visual Studio 2015. You say, *"My visual studio solution would fail at a line like this every time"*. Which line is it failing on, and what is the error you get? – Rufus L Aug 22 '19 at 17:45
  • @RufusL This fails for me in VS2019. Message: Could not find a part of the path 'C:\Users\Nate's Work Laptop\Documents\SpaceOnTheEnd \NoSpaceOnTheEnd'. StackTrace: at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.Directory.InternalCreateDirectory(String fullPath, String path, Object dirSecurityObj, Boolean checkHost) at System.IO.Directory.InternalCreateDirectoryHelper(String path, Boolean checkHost) at System.IO.Directory.CreateDirectory(String path) – OsakaRhymes Aug 22 '19 at 17:58
  • what are the versions of VS and target framework? – KolA Aug 22 '19 at 18:46
  • interesting, I just ran this code in VS and LINQPad - and got two subdirectories in My Documents - one "SpaceOnTheEnd" (without space, created by LINQPad v4.59.00) and another "SpaceOnTheEnd " (with space, created by VS / .Net Core 2.2). So code does behave differently but doesn't crash – KolA Aug 22 '19 at 19:21
  • and the only way to delete folder with space at the end was via cmd (rd "\\?\C:\Users\my.user.name\Documents\SpaceOnTheEnd "). Seems to be known behavior of .NET Core: https://github.com/dotnet/corefx/issues/26008 – KolA Aug 22 '19 at 19:29
  • 1
    On VS2019 .Net Framework 4.7.2 this throws a DirectoryNowFoundException. On VS2019 .Net Core 2.0 this works as described by @KolA – OsakaRhymes Aug 22 '19 at 20:19
  • 1
    I think you are running in to issues related to changes in .Net Path handling as described [here](https://blogs.msdn.microsoft.com/jeremykuhne/2016/06/21/more-on-new-net-path-handling/). See also [this article](https://learn.microsoft.com/en-us/dotnet/framework/migration-guide/mitigation-path-normalization). – NetMage Aug 22 '19 at 20:49

0 Answers0