discovered this was apparently a package bug. worked fine using Nuget package DocX
Im trying to read in a file from a folder and save it as a different extension. What is the correct way to handle this? I read through the folder and encounter file paths like:
C:\Users\xx\Desktop_REPOS\scraper\Reading Questions\Week 1\239523-1094170 - yyy - Aug 24, 2017 148 PM - Short Answer Aug 21.docx
Error in my code
FileLoadException: Could not load file or assembly 'System.IO.Packaging, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
I tried the solution from this post and got this error
System.IO.IOException: 'The filename, directory name, or volume label syntax is incorrect : 'C:\Users\king\Desktop_REPOS\scraper\scraper\bin\Debug\netcoreapp2.1\"C:\Users\king\Desktop_REPOS\scraper\Reading Questions\Week 1\239523-1094170 - yyy - Aug 24, 2017 148 PM - Short Answer Aug 21.docx"''
Mycode 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);
}
I wound up confirming this was a package bug and switched to using DocX
This was my final working solution:
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;
}
User contributions licensed under CC BY-SA 3.0