'File name encoding issues
System receives a zip file together with a list of file names (inside zip) that should be processed.
Problem: occasional "file not found error".
The file name specified in error message and the one visible in file system are visually identical, therefore it's an encoding issue. The error does not occur if file open operation is retried with a normalized file name like so:
filename.Normalize(System.Text.NormalizationForm.FormD)
Current solution is :
bool TryProcessFile(string filename) {
string[] filenamesToTry = new string[] {
filename,
filename.Normalize(System.Text.NormalizationForm.FormD)
}
foreach (string fname in filenamesToTry) {
try {
ProcessFile(fname);
return true;
}
catch (FileNotFoundException) {
}
}
return false;
}
function ProcessFile:
void ProcessFile(string filename) {
string fullpath = Path.Combine(this.extractionDir, filename);
byte[] fileBytes = File.ReadAllBytes(fullpath); // throws file not found
...
}
Is there a way to avoid an error in the first place?
In general how to tackle problems like this?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
