1

My code here var fullPath = Path.GetFullPath(input); throws OutOfMemoryException Insufficient memory to continue the execution of the program when I pass this value "\\." as argument. Any explanation as to why Path.GetFullPath("\\.") crashes?

XAML:

<TextBox><TextBox.Text><Binding Path="FolderPath" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True" NotifyOnValidationError="True"/></TextBox.Text></TextBox>

ViewModel:

// Implementation of IDataErrorInfo
public string this[string columnName]
{
   get
   {
      if (columnName != null)
      {
         if (columnName.Equals(nameof(FolderPath)))
         {
            if(!Validator.IsValidPath(FolderPath)
                 return "Invalid Folder";
         }
       }
   }
}

Validator Class:

private static bool IsValidPath(string input)
{
   bool isValid = false;
   try
   {
      var fullPath = Path.GetFullPath(input);
      return input.Equals(fullPath, StringComparison.InvariantCultureIgnoreCase);
   }
   catch (Exception)
   {
      isValid = false;
   }
   return isValid;
}
mark uy
  • 521
  • 1
  • 6
  • 17

1 Answers1

0

Any explanation as to why Path.GetFullPath("\.") crashes?

Per MSDN: https://msdn.microsoft.com/en-us/library/system.io.path.getfullpath(v=vs.110).aspx

Remarks
The .NET Framework does not support direct access to physical disks through paths that are device names, such as "\.\PHYSICALDRIVE0 ".

You'll either need to use pinvoke, Direct disk access in windows (C#) or perhaps this library might help CCS LABS C#: Low Level Disk Access.

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • thanks Jeremy. we've narrowed down the cause and as what Rawitas mentioned, it could be related to the build platform. My code above works if target platform is x64. But when I set it to x32 or Any CPU with preferred 32-bit, it gives the memory exception – mark uy Jan 18 '18 at 05:15
  • @markuy That's what is known as an X Y problem. You shouldn't be running out of memory for such a simple process. The memory usage is a symptom of a bigger problem. – Logarr Jan 18 '18 at 05:43