1

I have an odd issue that I'm hoping someone may be able to explain.

I have written a very short piece of code to demonstrate the issue at the bottom.

I have an app that iterates through folders and there is a good chance that the file paths and names are longer than windows typically allows. I found the workaround of using \\?\ at the start of the path fixed the issue.

However, this fix only works in debug mode or when running the exe directory. If I run it from the start menu it fails with an "illegal characters in path" error.

It appears that both ways of running have the same permissions but there is something that is different. Any ideas?

To replicate:

  • Create a new C# form with a single textbox that fills the window
  • Add the following code to the Load event:
    StringBuilder list = new StringBuilder();
    try
    { 
         foreach (string folder in System.IO.Directory.EnumerateDirectories(@"\\?\C:\"))
         {
              list.Append(folder + "\r\n");
         }
         textBox1.Text = list.ToString();
    } catch (Exception ex)
    {
         textBox1.Text = ex.ToString();
    }
    
  • Run in debug; you'll get a list of directories from C.
  • Publish it. It will fail with the illegal chars error.
  • Locate and run the exe directly. It will work.

Any assistance is appreciated!!

GSerg
  • 76,472
  • 17
  • 159
  • 346
  • [What FW version](https://stackoverflow.com/a/5188559/11683) are you targeting? Does it change anything if you include the config changes mentioned in https://web.archive.org/web/20160818035729/https://blogs.msdn.microsoft.com/jeremykuhne/2016/06/21/more-on-new-net-path-handling/? – GSerg Jan 13 '21 at 13:57
  • 1
    I can reproduce this; looking at the running exe it's launched from a user specific location `C:\Users\alexk\AppData\Local\Apps\2.0\TDG3CHV2.Z7Z\X5C16P33.K67\cons..tion_8944e795751b3205_0001.0000_644f9973a26f6147` which contains invalid path chars (`..`) - This is related to obsfucartion done by Click Once publishing. (I don't use this personally so don't know if this is normal/expected/what to do to correct it) This would not be an issue with any other type of deployment. – Alex K. Jan 13 '21 at 14:14
  • If you comment out the foreach loop for a moment, does the app run or produces it the same error? – Klaus Gütter Jan 13 '21 at 15:51
  • I'm targeting .Net 4.7.2 I've stripped the code back to bare minimum where all it does is textBox1.Text = System.IO.Directory.EnumerateDirectories(@"\\?\C:\").First(); But I get the same result. As Alex points out, I believe it is a ClickOnce issue. It's due to the .Application file being used to launch it. – user2729972 Jan 13 '21 at 21:03

2 Answers2

0

OK, I have a dirty solution. I'm not happy or proud of it, and I'm sure I'll be crucified but here it is.

It seems that anytime it is run as a ClickOnce application (using the appname.application instead of the appname.exe) it fails.

So the very first I check in the LOAD function is if it running as a ClickOnce application. If it is, I load the exe and kill the ClickOnce.

 if (ApplicationDeployment.IsNetworkDeployed)
 {
      Process.Start(Application.ExecutablePath);
      Application.Exit();
 }

It's not pretty but if it helps someone else then great. :)

  • You might not have to completely relaunch the application. Have you tried setting the `Environment.CurrentDirectory` property? – Ben Voigt Jan 13 '21 at 21:27
-3

Use WMI reference.

Using WMI, The WMI query like “SELECT * FROM Win32_Directory Where Drive= ‘C:’”

It just returns the folders until it reaches a folder with long path. The rest of the remaining folders are skipped even though they have small paths. You will never knowthat you have encountered an error in WMI process.

Enable NTFS long paths using group policy

Enabling the long paths will allow you to get the long path’s files or folders info using System.IO or WMI. If you’re using a Windows 10 machine, then please follow the below-mentioned steps to enable long paths

a. Hit the Windows key, type gpeedit.msc and press Enter. b. Navigate to Local Computer Policy > Computer Configuration > Administrative Templates > System > Filesystem > NTFS. c. Double click the Enable NTFS long paths option and enable it.

hzrbasaran
  • 11
  • 1
  • Thanks for your suggestions. Unfortunately, I cannot skip folders as I need them all, and I also cannot control group policies on the machine in question. – user2729972 Jan 13 '21 at 14:08