2

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=="
15
  • 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. Commented Jul 16, 2013 at 11:26
  • 1
    I am still certain that the string got persisted incorrectly into the file. Where does that file come from? Commented Jul 16, 2013 at 11:33
  • 2
    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. Commented Jul 16, 2013 at 11:36
  • 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. Commented Jul 16, 2013 at 11:39
  • 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. Commented Jul 16, 2013 at 11:46

1 Answer 1

3

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")
Sign up to request clarification or add additional context in comments.

1 Comment

I'm going to try that right away! edit: What a mistake to make...thanks very much Daniel Hilgart.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.