I am trying to open a pdf file that is located on the network :
I call the file like this in c# (Sorry for sending my code as a picture because of breakpoint i have to)
But it can't find the path .Another thing that i should add is when i call the file outside the c# like this \\127.0.0.1\dccfile\test\dcc1\1.pdf it works .
The value you're looking at in the debugger tooltip is a C# literal, not a string. C# literals delimit strings with straight quotes " and escapes metacharacters with backslashes \. See the quotes at the start and end of the literal in the tooltip? They're not part of the string. Backslashes are C# metacharacters, to include one in a string you have to precede it with another backslash. The C# literal "\\" encodes a string containing a single backslash character. The first \ you see in "\\127.0.0... is a metacharacter telling C# that the next character is a literal backslash, not a metacharacter. The code "\\127.0.0.1\\DCCFile\\test\\dcc1\\1.pdf" you see in the tooltip encodes the C# string \127.0.0.1\DCCFile\test\dcc1\1.pdf with no quotes and single backslashes.
Your problem is the value of Configuration.AccountDetail.DCCFileAddress needs to start with two backslashes and it does not.
Your code pathString.Replace(#"\\", #"\") will have no effect because there are no double backslashes in your string; the debugger is displaying the backslashes doubled so you know they are literal backslashes and not metacharacters.
Related
I have a StringBuilder object and wanted to used its Append() method to add this whole string to it:
so I used "#" and copy pasted that whole string like this, but it gives a lot of errors such as "; expected ", "Invalid Expression '<'" , etc
myString.Append(#"COPY-PASTED-THAT_WHOLE-STRING");
What is the correct way of adding this string to my string builder object?
Thank you.
Even with an # prefixing the string, you need to escape any " characters, otherwise they will be interpreted as the end of the string literal.
EDIT:
e.g.
var entity = #"<!ENTITY xsd ""http://www.w3.org/2001/XMLSchema#"">";
Double-quotes (") inside the string you want to paste need to be escaped by being replaced with two consecutive double-quotes, as in "". Here's a trick to use:
Paste your string into a new instance of Notepad
Replace all double quotes (") with two double quotes ("")
Select and copy the content from Notepad back into clipboard
Paste it into #"…" in your code/text editor
From C# docs:
In a verbatim string literal, the characters between the delimiters
are interpreted verbatim, the only exception being a
quote-escape-sequence.
You can use the # syntax to add multiple lines. But you need to escape the "s inside your string by using ""
For example
#"<Ontology xmlns=""http://www.w3.org/2002/07/owl#"""
If you don't escape them, C# will treat the quote mark as the end of the string.
One option, as others have said, is to escape all of the double quotes (") with a double double quote ("").
What I prefer to do, as it makes the code more readable, when adding an XML block as a literal string, is to use single quotes rather than double quotes. Just put the XML file into a text editor and do a replace all on double quote with a single quote (').
Another option, since your XML literal isn't all that short, is to put it into a file and read in that file at runtime.
You can escape them like this as well...
#"<Ontology xmlns=\"http://www.w3.org/2002/07/owl#\""
I'm trying to escape quotes in an xpath string like so:
var mktCapNode = htmlDoc.DocumentNode.SelectSingleNode("//*[#id=""yfs_j10_a""]");
The actual string I want passed is:
//*[#id="yfs_j10_a"]
This gives a compiler errors: ) expected and ; expected
I'm sure it's simple but I'm stumped. Any ideas?
You need to make this a verbatim string to use the "" as an escape
#"//*[#id=""yfs_j10_a""]"
For a normal string literal you need to use backslashes to escape the double quotes
"//*[#id=\"yfs_j10_a\"]"
Or use the escape char '\':
"//*[#id=\"yfs_j10_a\"]"
In C# the \ character is used to escape (see documentation).
This is different from VB where there are no escape characters except "" which escapes to ".
This means in C# you do not need vbCrLf to start a new line or vbTab to add a tab character to a string. Instead use "\r\n" and "\t".
You can also make the string a literal using the # character, but I do not think this works with the quotation mark.
Add the # prefix to your string.
#"//*[#id=""yfs_j10_a""]"
or escape the quotes with a \
"//*[#id=\"yfs_j10_a\"]"
I've noticed that C# adds additional slashes (\) to paths. Consider the path C:\Test. When I inspect the string with this path in the text visualiser, the actual string is C:\\Test.
Why is this? It confuses me, as sometimes I may want to split the path up (using string.Split()), but have to wonder which string to use (one or two slashes).
The \\ is used because the \ is an escape character and is need to represent the a single \.
So it is saying treat the first \ as an escape character and then the second \ is taken as the actual value. If not the next character after the first \ would be parsed as an escaped character.
Here is a list of available escape characters:
\' - single quote, needed for character literals
\" - double quote, needed for string literals
\\ - backslash
\0 – Null
\a - Alert
\b - Backspace
\f - Form feed
\n - New line
\r - Carriage return
\t - Horizontal tab
\v - Vertical quote
\u - Unicode escape sequence for character
\U - Unicode escape sequence for surrogate pairs.
\x - Unicode escape sequence similar to "\u" except with variable length.
EDIT: To answer your question regarding Split, it should be no issue. Use Split as you would normally. The \\ will be treated as only the one character of \.
.Net is not adding anything to your string here. What your seeing is an effect of how the debugger chooses to display strings. C# strings can be represented in 2 forms
Verbatim Strings: Prefixed with an # sign and removes the need o escape \\ characters
Normal Strings: Standard C style strings where \\ characters need to escape themselves
The debugger will display a string literal as a normal string vs. a verbatim string. It's just an issue of display though, it doesn't affect it's underlying value.
Debugger visualizers display strings in the form in which they would appear in C# code. Since \ is used to escape characters in non-verbatum C# strings, \\ is the correct escaped form.
Okay, so the answers above are not wholly correct. As such I am adding my findings for the next person who reads this post.
You cannot split a string using any of the chars in the table above if you are reading said string(s) from an external source.
i.e,
string[] splitStrings = File.ReadAllText([path]).Split((char)7);
will not split by those chars. However internally created strings work fine.
i.e.,
string[] splitStrings = "hello\agoodbye".Split((char)7);
This may not hold true for other methods of reading text from a file. I am unsure as I have not tested with other methods. With that in mind, it is probably best not to use those chars for delimiting strings!
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What's the # in front of a string for .NET?
I have the following code:
new Attachment(Request.PhysicalApplicationPath + #"pdf\" + pdfItem.Value)
What does the # sign do?
It has nothing to do with filepath. It changes the escaping behavior of strings.
In a string literal prefixed with # the escape sequences starting with \ are disabled. This is convenient for filepaths since \ is the path separator and you don't want it to start an escape sequence.
In a normal string you would have to escape \ into \\ so your example would look like this "pdf\\". But since it's prefixed with # the only character that needs escaping is " (which is escaped as "") and the \ can simply appear.
This feature is convenient for strings literals containing \ such as filepaths or regexes.
For your simple example the gain isn't that big, but image you have a full path "C:\\ABC\\CDE\\DEF" then #"C:\ABC\CDE\DEF" looks a lot nicer.
For regular expressions it's almost a must. A regex typically contains several \ escaping other characters already and often becomes almost unreadable if you need to escape them.
It's a verbatim string literal.
This allows the string to contain backslashes and even linebreaks without them being handled differently:
string multiLineString = #"First line
second line
third line";
As backslashes aren't used for escaping, inserting a double quote into the string requires it to be doubled:
string withQuote = #"before""after";
Verbatim string literals are typically used for file paths (as you've shown) and regular expressions, both of which frequently use backslashes.
See my article on strings for more information.
It allows you to enter the backslash (\) without escaping it:
var s1 = "C:\\Temp\\MyFileName";
var s2 = #"C:\Temp\MyFileName";
Both result in a string with the same contents (and since strings are interned at compile time, probably even the same string reference).
While searching on how to escape a single quote in String.Format, I found the answer at SO: Escaping single quote in String.Format()
It seems to be different for VB though. I tested it, and indeed C# needs
string s = DateTime.Now.ToString("MMM d \\'yy 'at' H:mmm");
while VB needs
Dim s As String = Now.ToString("MMM d \'yy 'at' H:mmm")
Why does C# need a double backslash, and VB a single backslash? This might be a bit of a silly question to C# users, but while I can read C#, I'm not used to writing it.
In C#, string literals can contain escape sequences such as \n for a new line or \t for a tab or \" for a quote. If you do not need the escaping, you can prefix the literal with # (eg: #"MMM ...") and get the same string a VB.
In VB, escaping is never allowed, so there is no need to escape the backslash.
The reason why is that C# supports escape sequences within string literals via the \ character. VB has no such escaping mechanisms and hence the single \ is interpretted as a \.
In C# you can get the same behavior by using verbatim strings
#"MMM d \'yy 'at' H:mmm"
In C# the backslash has a meaning (\n is newline \t tab ....).
So backlslahs itselft is an escape character - which you have to escape :)
Or place a AT-sign in front of the string - this makes a "non escaped string" (typically used for paths)
In c# \ will escape . Your text will become "MMM d \'yy 'at' H:mmm". You don't need to escape the ' character in a string. If you were to use " in the string on the other hand, you would need to escape it to not end your string "MMM d \"yy \"at\" H:mmm". Or you could also use the #"" string method which will automatically escape characters for you (not " though). So you could write #"this will not \n be two lines"