I'm trying to write a method that extracts a zip file to a directory, finds a file in the extracted contents, reads the text in that file to a string, and returns that string. Here is my attempt
private string _getDataFile(string zipFile)
{
string pathToFolder = @"C:\Path\To\The\File";
foreach (char c in Path.GetInvalidPathChars())
{
pathToFolder = Regex.Replace(pathToFolder, c.ToString(), "");
}
string pathToFile = pathToFolder + @"\model.dat";
ZipFile.ExtractToDirectory(zipFile, pathToFolder);
string dataToReturn = File.ReadAllText(pathToFile);
return dataToReturn;
}
However, despite my foreach loop replacing illegal path characters, the program still throws an illegal characters in path exception at the ZipFile.ExtractToDirectory line no matter what directory I try to use and I have no idea why. Any help would be greatly appreciated.