[英]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. 我需要复制路径长度大于260个字符的文件。
If I understood correctly, File.Copy
doesn't allow to do that. 如果我正确理解,则File.Copy
不允许这样做。 I have to use Win32.CopyFile
and add \\\\?\\
before the path. 我必须使用Win32.CopyFile
并在路径之前添加\\\\?\\
。
But if I try to access to \\\\?\\my_server\\my_path\\my_file
, I get the error 0x03 (path not found). 但是,如果尝试访问\\\\?\\my_server\\my_path\\my_file
,则会收到错误0x03(找不到路径)。 However, the same link in the Explorer works fine. 但是,资源管理器中的相同链接可以正常工作。
Files are stored in DFS file structures. 文件存储在DFS文件结构中。 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. 您需要为UNC路径指定正确的语法。
From MSDN: 从MSDN:
The "\\\\?\\" prefix can also be used with paths constructed according to the universal naming convention (UNC). “ \\\\?\\”前缀也可以与根据通用命名约定(UNC)构造的路径一起使用。 To specify such a path using UNC, use the "\\\\?\\UNC\\" prefix. 要使用UNC指定这样的路径,请使用“ \\\\?\\ UNC \\”前缀。 For example, "\\\\?\\UNC\\server\\share", where "server" is the name of the computer and "share" is the name of the shared folder. 例如,“ \\\\?\\ UNC \\ server \\ share”,其中“ server”是计算机的名称,“ share”是共享文件夹的名称。
In your example above, you probably want: 在上面的示例中,您可能需要:
string src = @"\\\\?\\UNC\\my_server\\my_folder\\my_file.ext";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.