0

Ok, I feel like this is a really stupid basic question but I cannot find the answer.

I am building a web service in c# and I am using MsgReader to read Outlook email files. To open a message I use this line:

Storage.Message message = new Storage.Message(filePath);

filePath will be coming from a remote location and will be something like "c:\apple.msg". This does not work. If I use "c:\\apple.msg" it works just fine.

It is interpreting that as an escape character. I tried String.Replace to change the \ to \\ but the string is a null value before it can even get to that point. This seems like a very simple problem, because I enter file paths as command line arguments all the time. How do you do this?

Additional info: The path will come from a web service call from a PHP application using SOAP. It dawned on me that I can replace the slash with double slashes before I make the call, but I would like to know how other people handle this problem.

Roofus McDuffle
  • 105
  • 1
  • 10
  • where does filePath get assigned? Please add that. – rene May 12 '18 at 08:18
  • related maybe: https://stackoverflow.com/questions/1928909/can-i-escape-a-double-quote-in-a-verbatim-string-literal – rene May 12 '18 at 08:24
  • This is just testing, but eventually filePath will be assigned when it is passed into the method as a string parameter. The string parameter will be assigned when the web service is called, but regardless, it will be a string. – Roofus McDuffle May 12 '18 at 08:39
  • What does it mean that "the string is a null value before it can even get to that point"? Obviously `String.Replace` will not work because you would need to replace `"\a"` with `"\\a"` and so on. – Zdeněk Jelínek May 12 '18 at 08:42
  • Q2: How do you parse the string from remote payload? The problem probably needs to be solved there as the system on the other side (or maybe your own serializer) gives you stuff in format that is not acceptable for you so you need to transform it. You shouldn't let strings like this pass into your system so that you would need to handle this at various places. That would only increase the complexity of doing anything for you. – Zdeněk Jelínek May 12 '18 at 08:48
  • 1
    The string in the line `Storage.Message message = new Storage.Message(filePath);` is not the problem. It's where `filePath` is assigned or when it gets its value from. Show that please. Otherwise this question is off-topic. – Enigmativity May 12 '18 at 09:19
  • 1
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. – Enigmativity May 12 '18 at 09:19
  • It seems that the data you get from a remote service is correct. I think you're confusing what the debugger displays to you. *Of course* if you put a string *literal* in your own code (like you do) you'll need to escape the characters properly. But chances are when you *do* hook it to the remote service that they aren't sending invalid data. Same can be said when you pass command line arguments like you state. You don't escape slashes when you type on the command prompt, that'd be silly. But hover over the variable and the debugger will show you the escaped slash. – pinkfloydx33 May 12 '18 at 15:03
  • Ok, I will add more code later and try the code in the actual way it is going to be used in real life. What will happen is I will use PHP to make a web service call and in the service call the file path will be one of the parameters passed in the XML. I will do this instead of trying to test in my code like I was, and see what happens. – Roofus McDuffle May 12 '18 at 19:43

1 Answers1

0

Mmm, try to write the path like this:

string filepath = @"c:\apple.msg";
Errol Chaves Moya
  • 307
  • 2
  • 5
  • 20