0

Problem: I am working on a program to take the output of tree and re create the directory structure on a different system. My code works for test sets of files that I have created at random. However when dealing with Systems with detailed/long folder names I run into a System.IO.IOException '(the filename or extension is too long) on this code

String path = @".\" + PreviousDirectory + @"\";
                    int errorCheck = path.Length;
                    Directory.SetCurrentDirectory(path, PathFormat.LongFullPath);    

Attempted Solutions: I have found this thread which describes several options. I have tried many of these I am currently not using System.IO I am using AlphaAeonis.Win32.Filesytem which supposedly has support for 32,000 chars in a path however my error occurs on a path that is 282 chars long.

I am also attempting to use .Net Framework 4.6.2 or higher which removed the path limit. I changed my target framework to 4.6.2 in Visual Studio 2017 which I am using I also have .NET SDK 6.0.0 installed. My app.config file which controls how Visual studio runs the code this looks like this

    <startup>  
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
    </startup>
    <runtime>   
        <AppContextSwitchOverrides value="Switch.System.IO.UseLegacyPathHandling=false"/>   
    </runtime>

If I change to anything except version = v"4.0" I am asked to install that version of .NET despite having higher version frameworks installed. I assume that the v4.0 is what is actually running.

Questions:

a. How can I ensure that all Directory functions use the Override from the imported Alphaleonis Library?

b. How can I ensure a minimum .Net Version upon Runtime?

c. What else could I set to allow long Paths?

corbin
  • 27
  • 3
  • I use `Pri.LongPath` myself, available on NuGet. I haven't tried [this](https://stackoverflow.com/a/52778791/1048799) solution from the thread you mentioned, but I like it! – rfmodulator Nov 19 '21 at 21:11
  • I tried that one and have had no luck but I will give pri.longpath a try – corbin Nov 19 '21 at 21:17
  • Make sure you're running everything through `Pri.LongPath`, and not `System.IO` (maybe this was the issue with `AlphaAeonis`?)... easiest way would be to comment out `using System.IO;` and all fully qualified references to that namespace. – rfmodulator Nov 19 '21 at 21:20
  • You can only use the version of .NET Framework installed in Windows, no matter what version you target. So what’s your Windows version? – David Browne - Microsoft Nov 19 '21 at 21:22
  • Does your installed operating system support/have_enabled long paths? – Caius Jard Nov 19 '21 at 21:23
  • @DavidBrowne I am on windows 10.0.19043 – corbin Nov 19 '21 at 21:24
  • @rfmodulator No luck with Pri.LongPath as my code heavily uses SetCurrentDirectory which Pri does not support Unfortunately – corbin Nov 19 '21 at 21:25
  • @CaiusJard yes I have enabled the registry key for longpaths – corbin Nov 19 '21 at 21:26
  • 1
    .NET 6 is for use with .NET-Core. (Which is different run-time...) It's confusing because they dropped the "-Core" terminology after 3.1 and skipped version 4 to avoid confusion with .NET4.8 which is older than -Core1. If you are using .NET6 SDK, you should target .NET6 (which is core run-time...) You should be able to bundle core with your app.... choose "self-contained" instead of "framework-dependent" when building/publishing. – pcalkins Nov 19 '21 at 21:33
  • 1
    btw, .NET5 and .NET6 are still pretty new. If you are going to switch to core, I think .NET-Core 3.1 is the long-term support version. – pcalkins Nov 19 '21 at 21:48
  • 1
    @pcalkins switching from .Net Framework to .net core did it thank you and yeah they can be new but I was trying any version basically those were the latest and it s a one off tool so no big deal – corbin Nov 19 '21 at 21:56
  • Your version of Windows 10 10.0.19043 is the 21H1 release, and has .NET Framework 4.8 built-in, so that's not the issue. – David Browne - Microsoft Nov 19 '21 at 22:41

1 Answers1

1

The Solution was switching from .NET Framework to .Net Core. Thank You to pcalkins

corbin
  • 27
  • 3
  • If you find the solution, you could click '✔' to mark it as an answer to change its status to Answered. It will also help others to solve a similar issue. See also [stackoverflow.com/help/why-vote](https://stackoverflow.com/help/why-vote) – Jiale Xue - MSFT Nov 23 '21 at 10:01
  • Thanks for the reminder – corbin Nov 23 '21 at 15:56