【发布时间】:2021-12-19 09:03:35
【问题描述】:
我目前正在为一个项目使用 .Net Framework 和 C#。
我想知道如何取消路径的 260 个字符限制。
我已经尝试访问 regedit 和 gpedit,但没有任何效果。我试着把“\?”前缀,但路径无法识别。
这里是 C# 代码示例:
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);
}
}
}
}
这是我的批处理代码(我想将 'loclogic' 和 'sousdoss' 传递给 %1 和 %2):
xcopy "%1" "%2" /c /h /e /r /y /s
感谢您的帮助!
【问题讨论】:
-
您使用的是哪个 .NET Framework 版本?自 .NET Framework 4.6.2 起,该限制不再适用
-
为什么要使用批处理文件进行实际复制?无需调用标准的 xcopy 命令,甚至无需将其打包到批处理文件中。
-
你为什么要使用
xcopy.bat?????????xcopy曾经是一个 DOS 实用程序,旨在克服内置copycommand 的限制。在 MS-DOS 中,不是没有这种限制的 Windows NT CLI。当您拥有 PowerShell 时,没有理由使用 DOS 命令。或者更好的实用程序,例如 robocopy -
这是XY Problem 的情况。您遇到问题 X(复制目录及其内容)并假设解决方案是 Y(使用 xcopy CLI 实用程序)。当失败时,您询问的是 Y,而不是实际的问题 X。the actual question 的答案不仅展示了如何复制文件夹,还展示了如何并行复制文件夹,就像
robocopy所做的那样。首先,创建文件夹树。然后复制文件 -
我正在使用 4.7.2 但我仍然卡住,无法解决我的问题..
标签: c# .net windows batch-file