【问题标题】:File could not be found using long file Paths使用长文件路径找不到文件
【发布时间】:2020-03-13 13:44:20
【问题描述】:

我试图在处理一些文件之前从 git 中删除 lfs 跟踪标签,只有一些文件在我们公司的詹金斯服务器中使用时包含 260 多个字符的文件路径。因此,我试图让 C# 使用长文件路径。我似乎在为下面的代码苦苦挣扎,我得到“找不到文件”,但是我 100% 不存在该文件,并认为我在使用 @"\?\" 时做错了什么.

    static int removeLFSTracking(string path)
    {
        int rC = STATUS_OK;
        string lfsTracked = ".lfs.tracked";

        List<string> files = new List<string>();
        getFiles( path, files );

        foreach(string file in files)
        {
            if(file.Contains(lfsTracked))
            {
                string newFileName = file.Replace(lfsTracked, "");
                try
                {
                    System.IO.File.Move( @"\\?\" + file, @"\\?\" + newFileName );
                }
                catch(SystemException e)
                {
                    Console.WriteLine( "Failed to remove lfs tracked from file " + file );
                    Console.WriteLine( e.ToString( ) );
                }
            }
        }
        return rC;
    }

我的 XML app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration> 
  <configSections>
    <section name="Extensions" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
  </configSections>
  <runtime>
    <AppContextSwitchOverrides value="Switch.System.IO.UseLegacyPathHandling=false;Switch.System.IO.BlockLongPaths=false" />
  </runtime>
   <startup> 
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
  </startup>

答案是:我使用的是相对路径,这似乎不适用于语法,当我更改为绝对路径时,我的代码运行良好。

【问题讨论】:

  • 路径是什么样的?您可以从 Windows 资源管理器中使用该路径吗?如果路径错误,其余代码无所谓
  • 例如file是否包含路径?从这个问题我们无法知道
  • @PanagiotisKanavos 谢谢你在windows中工作的指点帮助,事实证明它并不特别喜欢使用相对路径和该语法,一旦我改为绝对路径就可以了。

标签: c# .net system.io.file system.io.directory


【解决方案1】:

我对文件和 newFileName 使用相对路径,这些不能与 \?\ 语法结合使用,一旦我将它们更改为绝对路径,它就可以正常工作了。

【讨论】:

    猜你喜欢
    • 2015-08-25
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多