Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm reading an encoded string into memory and decode it. The string is something like "test\file1.txt".

Normally, C# would see this as string literal "test \\ file1.txt", correctly assigning an escape character to the backslash-char.

In this case, C# sees the slash as an escape character for the f from file. ("\f").

I can't use the Replace(@"\", @"\") method of string, because C# doesn't find "\", it only finds "\f". The filename is completely variable, so I can't use Replace(@"\f", @"\f") either...

How would I proceed with this in-memory string and add a slash, so that the string is a valid path?

The string is just loaded from a text-file and passed through a decoder.

public static string Decode(string inp)
{
    byte[] ToDecode = System.Convert.FromBase64String(inp);
    return System.Text.ASCIIEncoding.UTF8.GetString(ToDecode);
}

This is where I actually use the string (which is called 'A')

foreach (string A in Attchmnts)
    Msg.Attachments.Add(new Attachment(_AttachmentsPath + @"\" + A));

If I check the contents of the Attachment, via immediate, this is the result:

?_AttachmentsPath + @"\" + A
"\\\\BUPC1537\\MailServer\\Attachments\\test\file2.txt"

I have manually encoded the string by calling following method in immediate (and then pasting that data into an XML document):

public static string Encode(string inp)
{
    byte[] ToEncode = System.Text.ASCIIEncoding.UTF8.GetBytes(inp);
    return System.Convert.ToBase64String(ToEncode);
}

//Immediate code
?Utils.Encoder.Encode("test\file2.txt")
"dGVzdAxpbGUyLnR4dA=="
share|improve this question
3  
Please show your code. What you say is happening doesn't happen when you decode a string. There must be something else going on here. Most likely, the string already got written incorrectly into the file. – Daniel Hilgarth 16 hours ago
1  
I am still certain that the string got persisted incorrectly into the file. Where does that file come from? – Daniel Hilgarth 16 hours ago
1  
As you can see, the "\" before the "f" is not excaped, so this is really only one char : '\f'. You need to verify why you have this behaviour. – Toto 16 hours ago
1  
@Recipe: Please understand that what you say is not true. It is not the decoding at fault nor Base64. The fault lies with the code that created the string before encoding it or writing it to a file. Most likely, you forgot the @ in front of a part of the string when you created your sample file. Please show the exact code you used to create that file. – Daniel Hilgarth 16 hours ago
1  
If you do var test = @"test\file2.txt"; then evaluate test == Encoding.UTF8.GetString(Convert.FromBase64String(Convert.ToBase64String(Convert.‌​ToBase64String(Encoding.UTF8.GetBytes(test)))); you'll find it evaluates to true. In fact, you'll find it evaluates to true whatever value you use for test. – Jodrell 15 hours ago
show 10 more comments

1 Answer

up vote 3 down vote accepted

As I suspected all along, the code that creates the file doesn't correctly escape the backslash.

Fix it by doing so, either by using a verbatim string:

Utils.Encoder.Encode(@"test\file2.txt")

or by explicitly escaping the backslash:

Utils.Encoder.Encode("test\\file2.txt")
share|improve this answer
I'm going to try that right away! edit: What a mistake to make...thanks very much Daniel Hilgart. – Recipe 16 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.