I'm having a doubt about this
I tried to save a xmlDocument in a network device folder, not mapping.
Where:
config.plc.Path ="\\IpAdress\\folder\\";
doc.Save(config.plc.Path + "file.xml");
It was throwing an exception, and I just fixed it using the '#'
doc.Save(#config.plc.Path + "file.xml");
When I add the parameter as verbatim string with #, it gets like this:
config.plc.Path ="\\\\IpAdress\\\\folder\\\\";
is the first time I see a path like this, with
\\\\
can someone help me to understand this?
It's simple, \\ is just an escape sequence for \. # (Verbatim String) has to be used to avoid this
Related
I'm really not great with RegEx in C#, never really used them but I have a long string that contains a lot of html that may contain numerous text parts like
src="Folder/Uploads/fd123051-532d-4804-a0fb-fd4ce6b70f7c/cd212dd7-7600-4b3f-a7d9-9a85c85a50ca.png"
or
src="Uploads/fd123051-532d-4804-a0fb-fd4ce6b70f7c/cd212dd7-7600-4b3f-a7d9-9a85c85a50ca.png"
I want to apply a reg ex over the string if it can be done in C# so it replaces the folder path so it will change any and all to be src = filename.extension
ie.
src="Uploads/fd123051-532d-4804-a0fb-fd4ce6b70f7c/cd212dd7-7600-4b3f-a7d9-9a85c85a50ca.png"
becomes
src="cd212dd7-7600-4b3f-a7d9-9a85c85a50ca.png"
Can anyone please help?
RegEx for your replace:
src="Uploads/fd123051-532d-4804-a0fb-fd4ce6b70f7c/cd212dd7-7600-4b3f-a7d9-9a85c85a50ca.png"
Will be:
F: src="(.+?)//(.+?)//(.+?).png" [You can check "Dot Matches All"]
R: src="$1/$2/$3.png" Or you can use instead of $1 , /1 /2 /3 etc.
You can use:
src = Path.GetFileName(src);
You need substring function that will take only the part which you want from string Please go here.
Get file name from path
Below I have a regex statement I have been working on for quite awhile. The problem I am having is that their are a lot of quotations I am trying to parse out (I think that's the terminology I am looking for) so Visual Studio is freaking out about it. I have tried to fix this using escape characters, but it still won't recognize the whole phrase.
Here is the phrase without the escape characters:
string exceptionPattern = #"(?:(?:"([^"}]*)")|(\w+))\s*:\s*(?:(?:"([^"}]*)")|(\w+))";
With just this code in, nothing else, almost every line in my code gets affected. Here is the code using escape characters:
string exceptionPattern = #"(?:(?:\"([^\"}]*)\")|(\w+))\s*:\s*(?:(?:\"([^\"}]*)\")|(\w+))";
Once this comes into play, only this line is not working. In VS, the ([^\ part close to the beginning is not highlighted, meaning that it is not in quotes. Does anyone have any idea on how I can fix this problem?
This is the string I am trying to match. Note: THIS IS NOT JSON! I have confirmed it many times with the developer who made the database where this sample is coming from and he confirmed it is not JSON, so please do not try to use JSON on this. Also, the regex I have is trying to match displayException and the message after it, and also exception and the message after that including success false, using the quotes as a point of splitting
{"data":"","displayException":"Invalid Account Status. Please complete the registration process by clicking the verification link in your eTTek Dash Registration Verification email. Please contact 1-800-341-6184 M-F 9a-5pm CT for further assistance.","exception":"UNABLE TO LOGIN","success":false}
Inside a verbatim string, to escape a double quotes you must need to add another double double quotes near to that like "". So the compiler treats "" as a double quotes or otherwise it would treat " as an end of the verbatim string.
Using System.IO, I tried the following code:
string[] files = Directory.GetFiles("\\folder\\folder_2\\folder_3");
And got the following exception:
"System.IO.DirectoryNotFoundException - It was not possible to locate part of the path 'C:\folder\folder_2\folder_3)' "
I don't know why "c:\" was added to the original string, and I can't seem to keep the method from doing so. What am I doing wrong?
Any help is much appreciated.
A backslash (\) at the start of a path makes it an absolute path. Remove the first \ if you want a relative path:
string[] files = Directory.GetFiles("folder\\folder_2\\folder_3");
You need to escape each of the beginning backslashes in your path, you only escaped a single slash. Use either correct escaping:
string[] files = Directory.GetFiles("\\\\folder\\folder_2\\folder_3");
Or you can use a verbatim string literal:
string[] files = Directory.GetFiles(#"\\folder\folder_2\folder_3");
Full explanation found in MSDN Documentation
In addition to the answers provided, you could use verbatim string literals, which will pass the string exactly without the need for escaping with all the messy backslashes.
In your case this would be
string[] files = Directory.GetFiles(#"folder\folder_2\folder_3");
Notice that the # is outside of quotes, but stuck to the opening quotes, this tells C# to use it, (pardon the pun) literally. The syntax highlighting for this kind of string will also change in Visual Studio, just FYI.
edit: saw a comment by another user advising you to use the #, it's the same thing. Sorry did not see this earlier.
Read about them here at MSDN
I've got a .NET 3.5 web application written in C# doing some URL rewriting that includes a file path, and I'm running into a problem. When I call string.Split('/') it matches both '/' and '\' characters. Is that... supposed to happen? I assumed that it would notice that the ASCII values were different and skip it, but it appears that I'm wrong.
// url = 'someserver.com/user/token/files\subdir\file.jpg
string[] buffer = url.Split('/');
The above code gives a string[] with 6 elements in it... which seems counter intuitive. Is there a way to force Split() to match ONLY the forward slash? Right now I'm lucky, since the offending slashes are at the end of the URL, I can just concatenate the rest of the elements in the string[], but it's a lot of work for what we're doing, and not a great solution to the underlying problem.
Anyone run into this before? Have a simple answer? I appreciate it!
More Code:
url = HttpContext.Current.Request.Path.Replace("http://", "");
string[] buffer = url.Split('/');
Turns out, Request.Path and Request.RawUrl are both changing my slashes, which is ridiculous. So, time to research that a bit more and figure out how to get the URL from a function that doesn't break my formatting. Thanks everyone for playing along with my insanity, sorry it was a misleading question!
When I try the following:
string url = #"someserver.com/user/token/files\subdir\file.jpg";
string[] buffer = url.Split('/');
Console.WriteLine(buffer.Length);
... I get 4. Post more code.
Something else is happening, paste more code.
string str = "a\\b/c\\d";
string[] ts = str.Split('/');
foreach (string t in ts)
{
Console.WriteLine(t);
}
outputs
a\b
c\d
just like it should.
My guess is that you are converting / into \ somewhere.
You could use regex to convert all \ slashes to a temp char, split on /, then regex the temp chars back to \. Pain in the butt, but one option.
I suspect (without seeing your whole application) that the problem lies in the semantics of path delimiters in URLs. It sounds like you are trying to attach a semantic value to backslashes within your application that is contrary to the way HTTP protocols define and use backslashes.
This is just a guess, of course.
The best way to solve this problem might be modifying the application to encode the path in some other way (such as "%5C" for backslashes, maybe?).
those two functions are probably converting \ to / because \ is not a valid character in a URL (see Which characters make a URL invalid?). The browser (NOT C#, as you are inferring) is assuming that when you are using that invalid character, you mean /, so it is "fixing" it for you. If you want \ in your URL, you need to encode it first.
The browsers themselves are actually the ones that make that change in the request, even if it is behind the scenes. To verify this, just turn on fiddler and look at the URLs that are actually getting sent when you go to a URL like this. IE and Chrome actually change the \ to / in the URL field on the browser itself, FireFox doesn't, but the request goes through that way anyways.
Update:
How about this:
Regex.Split(url, "/");
Using C# .net I am parsing some data with some partial html/javascript inside it (i dont know who made that decision) and i need to pull a link. The link looks like this
http:\/\/fc0.site.net\/fs50\/i\/2009\/name.jpg
It came from this which i assume is javascript and looks like json
"name":{"id":"589","src":"http:\/\/fc0.site.net\/fs50\/i\/2009\/name.jpg"}
But anyways how should i escape the first link so i get
http://fc0.site.net/fs50/i/2009/name.jpg
In this case i could just replace '\' with '' since links dont contain \ nor " so i could do that but i am a fan of knowing the right solution and doing things properly. So how might i escape this. After looking at that link for a minute i thought is that valid? does java script or json escape / with \? It doesnt seem like it should?
In your case:
"name":{"id":"589","src":"http://fc0.site.net/fs50/i/2009/name.jpg"}
"/" is a valid escape sequence. However, it is not required that / be escaped. You may escape it if you need to. The reason JSON explicitly allows escaping of slash is because HTML does not allow a string in a to contain "...
Update:
Check out this post
Odd, it doesn’t look like any JavaScript/JSON escaping you’d expect. You can have forward slashes in JavaScript strings just fine.
Why dont you try a regex on the escaped slashes to replace them in the C# code...
String url = #"http:\/\/fc0.site.net\/fs50\/i\/2009\/name.jpg";
String pattern = #"\\/";
String cleanUrl = Regex.Replace(url, pattern, "/");
Hope it helps!
Actually you want to unescape the string. Answered in this question.