Windows路径,长度超过260个字符

时间:2015-12-29 06:32:17

标签: c# path

我需要复制路径长度超过260个字符的文件。

如果我理解正确,File.Copy不允许这样做。我必须使用Win32.CopyFile并在路径前添加\\?\

但是如果我尝试访问\\?\my_server\my_path\my_file,我会收到错误0x03(找不到路径)。但是,Explorer中的相同链接可以正常工作。

文件存储在DFS文件结构中。这很重要吗?

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();

1 个答案:

答案 0 :(得分:1)

您需要为UNC路径指定正确的语法。

请参阅MSDN

来自MSDN:

  

" \\?\"前缀也可以与根据通用命名约定(UNC)构造的路径一起使用。要使用UNC指定此类路径,请使用" \\?\ UNC \"字首。例如," \\?\ UNC \ server \ share",其中"服务器"是计算机的名称和"分享"是共享文件夹的名称。

在上面的示例中,您可能需要:

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

相关问题