简体   繁体   中英

Windows path with a length of more than 260 characters

I need to copy files that have a path with a length greater than 260 characters.

If I understood correctly, File.Copy doesn't allow to do that. I have to use Win32.CopyFile and add \\\\?\\ before the path.

But if I try to access to \\\\?\\my_server\\my_path\\my_file , I get the error 0x03 (path not found). However, the same link in the Explorer works fine.

Files are stored in DFS file structures. Is it important?

string src  = @"\\?\my_server\my_folder\my_file.ext";

if (Kernel32.CopyFile(src, f2c.getDest, true))
{
    Console.WriteLine("[SUCCESS] Copie du fichier {0} vers {1}", src, f2c.getDest);
    list_updSQL.Add(String.Format(@"UPDATE dbo.Fichier SET NOM_FICHIER_COPIE = '{0}' WHERE HASH_FICHIER ='{1}' ;", src, f2c.getHash));
}
else
{   
    Console.WriteLine("[FAILED][ERROR {0}] Copie du fichier {1} vers {2}", Kernel32.GetLastError().ToString(), src, f2c.getDest);
}

/* ----- */

[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
public static extern bool CopyFile(string lpExistingFileName, string lpNewFileName, bool bFailIfExists);

[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
public static extern UInt32 GetLastError();

You need to specify the correct syntax for UNC paths.

See MSDN .

From MSDN:

The "\\\\?\\" prefix can also be used with paths constructed according to the universal naming convention (UNC). To specify such a path using UNC, use the "\\\\?\\UNC\\" prefix. For example, "\\\\?\\UNC\\server\\share", where "server" is the name of the computer and "share" is the name of the shared folder.

In your example above, you probably want:

string src = @"\\\\?\\UNC\\my_server\\my_folder\\my_file.ext";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM