Reputation: 5037
....
case "DOWNLOAD":
if (File.Exists(commandContent)) {
MessageBox.Show(commandContent);
result = System.Convert.ToBase64String(File.ReadAllBytes(commandContent)); //ERROR
result = HttpUtility.UrlEncode(result);
}
break;
And Unhandled Error exception says "illegal characters" in path. The MessageBox shows a correct path
C:\Users\Me\Myfile.exe
I tried to do the following :
commandContent = commandContent.replace(@"\",@"\\");
result = System.Convert.ToBase64String(File.ReadAllBytes(commandContent)); //ERROR
I also tried the following :
commandContent = @""+commandContent+"";
result = System.Convert.ToBase64String(File.ReadAllBytes(commandContent)); //ERROR
but this doesn't work. And the more strange is that it was working correctly as it is, but once I made some modification in the way I'm inserting commandContent into db (using ajax instead of regular form submit), this problem appears ?
EDIT:
I tried to hard code the path using
commandContent = @"C:\Users\Me\file.exe";
That worked correctly. How can I force the variable not to contain any illegal characters?
Upvotes: 1
Views: 12398
Reputation: 20140
i'm pretty sure that you have \n or \r or \t or ... at the end or beginning of the string commandContent
can you do a
System.Text.Encoding.UTF8.GetBytes (commandContent)
and check each byte?
maybe the ajax call doesn't make a proper string/path
you can use this to find which one
class Program
{
static void Main(string[] args)
{
var commandContent = "C:\\Users\\Me\\file.exe\n";
var commandContentBytes = System.Text.Encoding.UTF8.GetBytes(commandContent);
var invalidPathChars = System.IO.Path.GetInvalidPathChars().Select(x=>Convert.ToByte(x));
var found = commandContentBytes.Intersect(invalidPathChars);
}
}
Upvotes: 6