要为每个应用程序启用新的长路径行为,必须满足两个条件。 必须设置注册表值,应用程序清单必须包含 longPathAware 元素。
来源:最大路径长度限制 - Win32 apps | Microsoft Learn
不知道是不是这个原因,我还看到说什么.net的版本太低了,什么之类的原因,反正就是现在不支持长路径,至少在我这不支持
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
public static void Exec(Quicker.Public.IStepContext context)
{
try
{
// 1. 获取传入的路径参数
string path = context.GetVarValue("path") as string;
context.SetVarValue("debugInfo", $"开始处理路径: {path}");
// 2. 检查路径有效性
if (string.IsNullOrWhiteSpace(path))
{
throw new ArgumentException("路径不能为空");
}
// 3. 处理长路径(超过260字符)
string longPath = path;
if (path.Length > 260 && !path.StartsWith(@"\\?\"))
{
if (path.StartsWith(@"\\"))
{
longPath = @"\\?\UNC\" + path.Substring(2);
}
else
{
longPath = @"\\?\" + path;
}
context.SetVarValue("debugInfo", $"启用长路径模式: {longPath}");
}
// 4. 验证目录是否存在
if (!Directory.Exists(longPath) && !Directory.Exists(path))
{
throw new DirectoryNotFoundException($"目录不存在: {path}");
}
// 5. 获取目录内容
List<string> results = new List<string>();
// 获取子目录
try
{
foreach (string dir in Directory.GetDirectories(longPath))
{
results.Add(dir);
}
}
catch (Exception ex)
{
context.SetVarValue("debugInfo", $"读取目录错误: {ex.Message}");
}
// 获取文件
try
{
foreach (string file in Directory.GetFiles(longPath))
{
results.Add(file);
}
}
catch (Exception ex)
{
context.SetVarValue("debugInfo", $"读取文件错误: {ex.Message}");
}
// 6. 返回结果
context.SetVarValue("list", results);
context.SetVarValue("debugInfo", $"成功获取 {results.Count} 个项目");
}
catch (Exception ex)
{
// 7. 错误处理
context.SetVarValue("debugInfo", $"❌ 错误: {ex.Message}");
context.SetVarValue("list", new List<string>());
throw;
}
}
就是普通模式v2,传入的path是一个长路径就报错,如果path是短路径是正常输出list
https://temp.getquicker.net/311786/2035546cd78940cb9bd954f58d260f66.html
https://temp.getquicker.net/311786/277946ba7f434e2bab4857665ddb7b4d.html