代码之家  ›  专栏  ›  技术社区  ›  Rilcon42

无法加载带有空格系统的文件。Io错误

  •  0
  • Rilcon42  · 技术社区  · 8 年前

    发现这显然是一个软件包错误。使用Nuget包DocX运行良好

    我试图从一个文件夹中读入一个文件并将其另存为一个扩展名。正确的处理方法是什么?我通读了文件夹,发现文件路径如下:

    C:\Users\xx\Desktop\u REPOS\scraper\Reading Questions\Week 1\239523-1094170-yyy-2017年8月24日下午148-简答8月21日.docx

    FileLoadException:无法加载文件或程序集“System.IO.Packaging,Version=4.0.2.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”。定位的程序集的清单定义与程序集引用不匹配(HRESULT异常:0x80131040)

    我试着从 this post 我犯了这个错误

    霉菌代码 Code example I used

    foreach (string path in Directory.EnumerateFiles(@"C:\Users\xx\Desktop\_REPOS\scraper\Reading Questions\Week 1", "*.*", SearchOption.AllDirectories)
    .Where(s => s.EndsWith(".pdf") || s.EndsWith(".docx")))
    {
            FileToTxt(path);
            //FileToTxt(AddQuotesIfRequired(path));
            Console.WriteLine("converted: " + Path.GetFileName(path));
    }
    
    public static void FileToTxt(string filepath)
    {
        //Install-Package sautinsoft.document
        string textFilePath = Path.ChangeExtension(filepath, ".txt");
        DocumentCore docx = DocumentCore.Load(filepath); ////////---ERROR HERE
        docx.Save(textFilePath, SaveOptions.TxtDefault);
    }
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Rilcon42    8 年前

    我最后确认这是一个软件包错误,并切换到使用 DocX

        public static bool FileToTxt(string filepath)
        {
            try { 
            //Install-Package DocX
            string textFilePath = Path.ChangeExtension(filepath, ".txt");
            DocX docx = DocX.Load(filepath);
            File.WriteAllText(textFilePath, docx.Text);
            }catch(Exception e)
            {
                Console.WriteLine($"{Path.GetFileName(filepath)} error: {e.Message}");
                return false;
            }
            return true;
        }