错误:文件路径太长

nis*_*ain 4 c#

我试图在C#中使用各种文件函数File.GetLastWriteTime,复制命令放在路径上的文件大于Windows 7上的最大允许路径,即260.它给我一个长路径名错误.在MSDN支持上,他们要求使用\\?\前面的路径.我做了同样的但仍然得到了同样的错误,它似乎没有做任何改变.以下是我的代码.如果我正确使用它或者我需要添加任何东西,请告诉我:
这些我正在使用的所有lib作为代码还有其他东西:

以下是各自的代码:

filesToBeCopied = Directory.GetFiles(path,"*",SearchOption.AllDirectories);
for (int j = 0; j < filesToBeCopied.Length; j++)
{
    try
    {
        String filepath = @"\\?\" + filesToBeCopied[j];
        File.GetLastWriteTime(filepath);
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error Inside the single file iteration for the path:" +
            filesToBeCopied[j] + " . The exception is :" + ex.Message);
    }
}
Run Code Online (Sandbox Code Playgroud)

其中path是Windows机器上以驱动器号开头的文件夹的路径.例如:d:\abc\bcd\cd\cdc\dc\..........

Jon*_*yna 6

这是至少您的请求的复制部分的解决方案(谢谢pinvoke.net):

[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
static extern bool CopyFile(string lpExistingFileName, string lpNewFileName, bool bFailIfExists);
Run Code Online (Sandbox Code Playgroud)

然后实际复制你的文件:

// Don't forget the '\\?\' for long paths
string reallyLongPath = @"\\?\d:\abc\bcd\cd\cdc\dc\..........";
string destination = @"C:\some\other\path\filename.txt";
CopyFile(reallyLongPath , destination, false);
Run Code Online (Sandbox Code Playgroud)


Agh*_*oub 1

尝试使用此代码

var path = Path.Combine(@"\\?\", filesToBeCopied[j]); //don't forget extension
Run Code Online (Sandbox Code Playgroud)

路径字符串的“\?\”前缀告诉 Windows API 禁用所有字符串解析并将其后面的字符串直接发送到文件系统。

重要提示:并非所有文件 I/O API 都支持“\?\”,您应该查看每个 API 的参考主题