-1

I am trying to search for a file with a given name from a given root directory using the code below

Dim searchPattern As String = fileNameWithoutExtension.TrimEnd & "*"
Dim foundFiles As String() = Directory.GetFiles(Path.Combine("\\?", directoryPath),
searchPattern, SearchOption.AllDirectories)

I am able to do this without any issues when I run the vb.net application from visual studio. But, getting the below error when I running it as a published Windows application.

Illegal characters in path.
Stack Trace
at
System.Security.Permissions.FilelOPermission.EmulateFilelOPermission Checks(String fullPath)
at System.IO.FileSystemEnumerableIterator 1..ctor(String path, String original UserPath,
String searchPattern, SearchOption searchOption, SearchResultHandler 1 resultHandler, Boolean
checkHost)
at System.IO.Directory.GetFiles(String path, String SearchPattern,
SearchOption searchOption) at Fluence PanelSchedule.ProjectDocumentCreator.SearchFile(String
projectName, String docName, List 1 tempArrayFilesNotFound) Source::
\\?\C:\Users\username\directoryA\10_Document files.

The prefix \\?\ is added to avoid longer file path issues. And there is no other special character present in the file path. What am I missing here?

  • This is what \\?\ is https://learn.microsoft.com/en-us/dotnet/standard/io/file-path-formats#dos-device-paths. Also, I'm not a fan of creating paths using concatenation. – dbasnett Jul 11 '23 at 14:33
  • @dbasnett Thanks for the response. I went through the document that you have mentioned. I couldn't really get what is the issue with my approach. And why is that happening? The code runs well as a vb.net application on visual studio but fails when the same is published as a windows application. – Manjunath M Jul 11 '23 at 15:21
  • Is it failing on the development PC or a different computer? If the latter, which version of .NET Framework is installed where it fails, assuming it's using .NET Framework and not .NET Core or .NET - \\?\ only works in .NET Framework 4.6.2 and later. – Andrew Morton Jul 11 '23 at 16:24
  • @AndrewMorton It is failing on the same development PC. The PC has .NET version 4.8+ – Manjunath M Jul 12 '23 at 07:06
  • @ManjunathM What is the value of the SearchPattern parameter? Maybe it is going wrong in the way mentioned in ["Illegal characters in path" error using wildcards with Directory.GetFiles](https://stackoverflow.com/a/42531393/1115360). – Andrew Morton Jul 12 '23 at 07:17
  • @AndrewMorton It's going as "fileName*" (file name followed by *) – Manjunath M Jul 12 '23 at 07:50
  • @ManjunathM Does it still complain if you *don't* add the \\?\ ? – Andrew Morton Jul 12 '23 at 11:16
  • @AndrewMorton If I don't add it, some files (I have a huge set of files to be searched for) are not being fetched as the total file path is more than 260 characters. – Manjunath M Jul 12 '23 at 13:27
  • 1
    I was able to fix the issue by using Alphaleonis.Win32.Filesystem.File's Directory.EnumerateFiles() method, instead of System.IO's methods – Manjunath M Jul 12 '23 at 13:40

1 Answers1

0

To handle this long path issue I have used the AlphaFS library, as it provides enhanced file system functionality with support for long paths. ( Which can be easily installed via the NuGet package manager in Visual Studio)

Imports Alphaleonis.Win32.Filesystem
...Other code
Dim fileNameWithoutExtension As String =Path.GetFileNameWithoutExtension(fileName.Trim())
Dim searchPattern As String = fileNameWithoutExtension & "*"
Dim files As IEnumerable(Of String) =Directory.EnumerateFiles(directoryPath,
searchPattern, SearchOption.AllDirectories)

Also using Directory.EnumerateFiles() over Directory.GetFiles() sounded better as the number of files to be searched and processed is in the hundreds.

  • Hi ,glad to know you've found the solution to resolve this issue! Please consider accepting it as an answer to change its status to Answered. See [can I answer my own question..](https://stackoverflow.com/help/self-answer), Just a reminder :) – Jiachen Li-MSFT Jul 17 '23 at 07:25