I have file:// links with non-english characters which are UrlEncoded in UTF-8. For these links to work in a browser I have to re-encode them.
file://development/H%C3%A5ndplukket.doc
becomes
file://development/H%e5ndplukket.doc
I have the following code which works:
public string ReEncodeUrl(string url)
{
Encoding enc = Encoding.GetEncoding("iso-8859-1");
string[] parts = url.Split('/');
for (int i = 1; i < parts.Length; i++)
{
parts[i] = HttpUtility.UrlDecode(parts[i]); // Decode to string
parts[i] = HttpUtility.UrlEncode(parts[i], enc); // Re-encode to latin1
parts[i] = parts[i].Replace('+', ' '); // Change + to [space]
}
return string.Join("/", parts);
}
Is there a cleaner way of doing this?