0

I'm currently using the .Net Framework and the C# for a project.

I would like to know how to remove the 260 characters limit of the path.

I've tried to go to the regedit ans the gpedit but nothing worked. I've tried to put the "\?" prefix but the path was unrecongnised.

Here a sample of the C# code :

 private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
    {
        // Get the subdirectories for the specified directory.
        DirectoryInfo dir = new DirectoryInfo(sourceDirName);

        if (!dir.Exists)
        {
            throw new DirectoryNotFoundException(
                "Le répertoire source n'existe pas ou n'est pas accessible : "
                + sourceDirName);
        }

        DirectoryInfo[] dirs = dir.GetDirectories();
        // If the destination directory doesn't exist, create it.
        if (!Directory.Exists(destDirName))
        {
            Directory.CreateDirectory(destDirName);
        }

        // Get the files in the directory and copy them to the new location.
        FileInfo[] files = dir.GetFiles();
        foreach (FileInfo file in files)
        {
            temppath = Path.Combine(destDirName, file.Name);
            try
            {
                file.CopyTo(temppath, false);
            }
            catch (Exception ex)
            {
                Erreurs.Add(ex.Message);
                Erreurs.Add(temppath);
            }

        }

        // If copying subdirectories, copy them and their contents to new location.
        if (copySubDirs)
        {
            /*Utility.NetworkDrive.MapNetworkDrive("R", @"\\unc\path");
            var dirs1 = Directory.GetDirectories("R:");
            Utility.NetworkDrive.DisconnectNetworkDrive("R", true);*/
            foreach (DirectoryInfo subdir in dirs)
            {
               temppath = Path.Combine(destDirName, subdir.Name);

              // string fullpath = @"\\?\" + subdir.FullName;  -- HERE'S WHAT I'VE TRIED


                try
                {
                    
                    string sousdoss = subdir.FullName;
                    string loclogic = Application.StartupPath + @"\Xcopy.bat";

                    System.Diagnostics.Process proc = new System.Diagnostics.Process();
                    proc.StartInfo.FileName = Application.StartupPath + @"\Xcopy.bat";

                    proc.StartInfo.Arguments = String.Format("{0} {1} {2}",loclogic, sousdoss, temppath);
                    //set the rest of the process settings
                    proc.Start();                        
                }
                catch(Exception ex)
                {
                    Erreurs.Add(ex.Message);

                }
            }
        }
    }

And here's my batch code (I would like to pass 'loclogic' and 'sousdoss' to %1 and %2) :

xcopy "%1" "%2" /c /h /e /r /y /s

Thanks for your help !

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Which .NET Framework version are you using? That limit doesn't apply since .NET Framework 4.6.2 – Panagiotis Kanavos Nov 05 '21 at 09:28
  • 3
    Why are you using a batch file for the actual copy? There's no need to call the standard xcopy command and even less to have that wrapped in a batch file. – PMF Nov 05 '21 at 09:32
  • And why are you using `xcopy.bat` ????????? `xcopy` used to be a DOS *utility* meant to overcome the limitations of the built-in `copy` *command*. As in MS-DOS, not the Windows NT CLI which doesn't have such limitations. There's no reason to use DOS commands when you have eg PowerShell. Or far better utilities like robocopy – Panagiotis Kanavos Nov 05 '21 at 09:39
  • 1
    This is a case of an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). You have a problem X (copying a directory and its contents) and assume the solution is Y (use the xcopy CLI utility). When that fails you asked about Y, not the actual question X. The answers to [the actual question](https://stackoverflow.com/questions/58744/copy-the-entire-contents-of-a-directory-in-c-sharp) show not only how to copy folders but how to do so in parallel, the way `robocopy` does. First, create the folder tree. Then copy the files – Panagiotis Kanavos Nov 05 '21 at 09:45
  • I'm using 4.7.2 but i still stuck, can't resolve my problem .. – Benjamin DE MELO Nov 05 '21 at 15:13

1 Answers1

0

Updated: as pointed out by @Panagiotis Kanavos:

" A path that starts with \?.... can address volumes, streams, snapshots and be up to 32K long."

You can Enable Long Paths in Windows 10, Version 1607, and Later

But as the article describes it is an opt-in feature.

Starting in Windows 10, version 1607, MAX_PATH limitations have been removed from common Win32 file and directory functions. However, you must opt-in to the new behavior.

To enable the new long path behavior, both of the following conditions must be met:

The registry key Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem\LongPathsEnabled (Type: REG_DWORD) must exist and be set to 1. The key's value will be cached by the system (per process) after the first call to an affected Win32 file or directory function (see below for the list of functions). The registry key will not be reloaded during the lifetime of the process. In order for all apps on the system to recognize the value of the key, a reboot might be required because some processes may have started before the key was set.

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
  • 1
    This shouldn't be needed since .NET Framework 4.6.2 – Panagiotis Kanavos Nov 05 '21 at 09:28
  • 1
    They are not, in any version of the Windows NT line. It's only DOS-style paths and functions that are affected. A path that starts with `\\?\....` can address volumes, streams, snapshots and be up to 32K long. .NET before 4.6.2 tried to second-guess the OS. Since 4.6.2 the path is normalized to `\\?\`. – Panagiotis Kanavos Nov 05 '21 at 09:33
  • 1
    The linked article says that you no longer have to use the long path syntax, not that long paths weren't supported – Panagiotis Kanavos Nov 05 '21 at 09:35
  • Thanks for your help but how can i make a path start with \\?\ ? Should i add a prefix ? If its yes, i've already tried... Do you got any code sample for me ? – Benjamin DE MELO Nov 05 '21 at 09:36
  • Thanks Mitch for your answer but as stange as it could be, I've changed and applied the key in the Registry and changed the parameter in the group politicy that should enable the long paths. I've restarted the computer but for now, nothing changed – Benjamin DE MELO Nov 05 '21 at 09:40
  • @Panagiotis Kanavos : I learnt something today. I also learnt to read the entire article ! – Mitch Wheat Nov 05 '21 at 09:50
  • NTFS has some very advanced features that only lately appeared on Linux file systems (volume snapshots, journaling, transactions, multiple streams, compression, encryption, versioning, object access etc). Unfortunately, .NET Framework never exposed them unless you used interop. Microsoft infighting played a *very* large part in this. – Panagiotis Kanavos Nov 05 '21 at 09:55
  • And don't get me started on ReFS ... – Panagiotis Kanavos Nov 05 '21 at 09:57
  • @PanagiotisKanavos Does that mean that the .NET Framework is internally automatically prepending the `\\?\` stuff when it passes paths to the Windows API? – PMF Nov 05 '21 at 10:02
  • @PMF one of the duplicate answers links to [More on new .NET path handling](https://web.archive.org/web/20160818035729/https://blogs.msdn.microsoft.com/jeremykuhne/2016/06/21/more-on-new-net-path-handling/) which explains what happens. There's also a [Channel 9 video](https://channel9.msdn.com/Shows/On-NET/Jeremy-Kuhne-long-path-support). Unfortunately, when MS reorganized their sites they broke links to older articles. [Jeremy Kuhne's blog posts](https://learn.microsoft.com/en-us/archive/blogs/jeremykuhne/) are still available and explain the journey ... – Panagiotis Kanavos Nov 05 '21 at 10:15
  • @PMF ... and it is a journey. When 4.6.2 came out in 2016 you did need tweaks on Windows 10 and possibly some `config` changes. That Windows 10 version is no longer supported though. – Panagiotis Kanavos Nov 05 '21 at 10:18