0

I have a path with over 260 characters and tried to retreive file informations from the file from this path with FileInfo. But I was getting a PathTooLongException. I have shortened it with following code:

if (longName.Length >= 247)
{
    longName = @"\\?\" + longName;
}
StringBuilder shortPath = new StringBuilder(300);
GetShortPathName(longName, shortPath, shortPath.Capacity);
return shortPath.ToString();

Now my path have only 139 characters but I still get a PathTooLongException from FileInfo. Does anyone knows why?

Is the shortened path just a placeholder like a conjunction for the long path and FileInfo is using the long path instead?

EDIT: The Execption occurs here:

string fileName = ToShortPathName(_path);
fileName = fileName.Replace(@"\\?\", "");
fileInformations.FileName = fileName;
fileInformations.FullPath = _path;
=> fileInformations.FileLastChanged = File.GetLastWriteTime(fileName);

EDIT2: fileName at the position of error is:

D:\\06PROJ~1\\1094IH~1\\FACHBE~1\\IT\\Azubi\\KH\\PROJEK~1\\VISUAL~1\\USB_BA~1\\packages\\NUNIT3~1.0\\lib\\PORTAB~1.IOS\\NUNITF~1.DLL
Snickbrack
  • 1,253
  • 4
  • 21
  • 56
  • What is inside the `GetShortPathName`? And where does the `Exception` occur? – Ian May 12 '16 at 13:15
  • @Ian [GetShortPathName](https://msdn.microsoft.com/en-gb/library/windows/desktop/aa364989%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396) – Matthew Watson May 12 '16 at 13:20
  • @MatthewWatson Sorry, I cannot open the link you gave. It gives me: `Unable to Service Request` – Ian May 12 '16 at 13:21
  • @Ian Oops had a "]" in the wrong place – Matthew Watson May 12 '16 at 13:22
  • @MatthewWatson ok, thanks! – Ian May 12 '16 at 13:23
  • @Ian have edited the question... – Snickbrack May 12 '16 at 13:24
  • So, the error occurs in the `File.GetLastWriteTime`? and what is the `filename` at that point? – Ian May 12 '16 at 13:25
  • `FileInfo` (and [almost] all other `System.IO` classes) expand the filename again. See [How can I use FileInfo class, avoiding PathTooLongException?](http://stackoverflow.com/questions/1357358/how-can-i-use-fileinfo-class-avoiding-pathtoolongexception). Alternatively [substitute the start of the path with a new drive letter](http://superuser.com/questions/644684/mapping-drive-letters-to-local-folders). – CodeCaster May 12 '16 at 13:27

0 Answers0