I'm using PayWay payment gateway.
I want to put the pay certificate File path in the web.config file.
so I add like this
<add key="PayWayPath" value="c:\payway\ccapi.q0&logDirectory=c:\payway"/>
the I call in my web form like this.
String initParams = WebConfigurationManager.AppSettings["PayWayPath"];
My initParams like this
initParams = c:\\payway\\ccapi.q0&logDirectory=c:\\payway
but I need to get like this output.
initParams = c:\payway\ccapi.q0&logDirectory=c:\payway
I try this things. But non of the work
initParams = initParams.Replace("\\\\", "\\");
initParams = Regex.Replace(initParams, #"[\\ ]", "\");
So how can I do it?
You don't need anything.
First one is only seems on IDE, when you write it, it will be without slashes..
Let's try it;
string s = "c:\\payway\\ccapi.q0&logDirectory=c:\\payway";
Console.WriteLine(s);
Output will be;
c:\payway\ccapi.q0&logDirectory=c:\payway
Here is a DEMO.
Irrelevant but if you want to write your strings exactly what they are, you can use # is called verbatim string literal
If i understand correctly, you won't need to do anything more. If the string already has the slashes escaped, when you "print" that value, it will appear as you want it to (ie. single slashes only)
You can do this by
initParams = initParams.Replace("\\\\", "\\");
Related
I'm completely a junior here. I have tried something like
save a path and file string in a file like:
c:\aaa\bbb\text.txt
then I need to read again as path but I get c:\aaa\bbb\text.txt from streamreader, but I need c:\\\aaa\\\bbb\\\text.txt
Can anyone help me?
I think you might be confusing string literals with a string.
Say I write var myString = "\\" or var myString = #"\", this will show in the debugger as \\, because the debugger will format it as a literal. But if print it to the console, a file, or press the magnifying glass next to the string in the debugger, it will be shown as \, because that is the actual string value. See also verbatim string literal
So, if you do myStreamWriter.Write("c:\\aaa\\bbb\\text.txt");, you will be actually saving the string c:\aaa\bbb\text.txt, and that is also the string that will be read back.
However I fail to understand why you would want three slashes, I can only assume the OP thinks the escaping is done multiple times.
In order to be able to read a file in asp.net, the file path must be written in this:
1.
C:\\yung\\Desktop
returns
however, the string that the fileUpload get returns is
2.
C:\yung\Desktop
After reading the comments i have this code:
string FilePath = FileUploadPublicInfo.PostedFile.FileName;
System.IO.StreamReader file = new System.IO.StreamReader(FilePath);
string line = File.ReadLines(FilePath.ToString()).Skip(4).ToString();
TextBox1.Text = line.ToString();
But now its giving this error:
System.Linq.Enumerable+<SkipIterator>d__30`1[System.String]
How to solve this problem?
Thank you.
I'm not so sure I understand the question, but I think you are looking for string.Replace:
string DoubleSlash(string singleSlash)
{
return singleSlash.Replace(#"\", #"\\");
}
The reason backslashes disappear is that C# compiler treats slashes in string literals as a special "escape" character. Because of this treatment, backslash needs to be encoded as two slashes in a regular string literal.
C# offers two ways of inserting backslashes the way you need:
Use verbatim literals - prefix it with "at" sign, i.e. #"C:\\yung\\Desktop", or
Double each slash - put two slashes for each slash in the result: C:\\\\yung\\\\Desktop
Ok, i have manage to solve this problem, turns out it was not reading anything.
This is the code that i finally get:
This is to retrieve the File's path, using this, would give the file path will double slash, so there is not a need for Replace(#"\",#"\")
string FilePath = FileUploadPublicInfo.PostedFile.FileName;
Then read the specified file
System.IO.StreamReader file = new System.IO.StreamReader(FilePath);
If you know which line you specifically want, this retrieves the 5th line
string line = File.ReadLines(FilePath.ToString()).Skip(4).First().ToString();
Thank you so much for your help...
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
In my Application there is a class that works with PdfSharp to generate some PDF reports. I specified output folder as a string with verbatim
string file_path = #"D:\Intranet\Students\DailyMarks\";
Also there is a StringBuilder that generates file name based on some ID and DateTime:
... sb.Append(document.Type); sb.Append(document.Id); sb.Append(DateTime.Now.ToShortString());
And finally I do the following
file_path + sb.toString();
But my Application cathes an exception. After debugging session I see that actually my file_path is
file_path = "D:\\Intranet\\Students\\DailyMarks\\...";
As I understand it happens after concatenation of origin file with StringBuilder's toString() call.
I tried to replace file_path string with something like this:
file_path = file_path.Replace(#"\\",#"\");
but it doesn't work. Where did I do wrong?
Probably this is caused by the DateTime.Now.ToShortString() method, which adds forbidden characters to the path (:).
It's totally fine.
"D:\\Intranet\\Students\\DailyMarks\\..." == #"D:\Intranet\Students\DailyMarks\..."
In regular string you need to escape slashes, in verbatim it's done automatically
Another similar situation I faced today was about sending Japanese 「:」 (colon with whole inside) as a file's name's element and it worked. I wonder, why Russian colon calls an exception and Japanese not. Very interesting.
I created a directory inside my WPF solution called Sounds and it holds sound files.(For example: mySound.wav).
Inside my code I use a List and there I have to add to those strings that relate to the sound files. In the beginning I used #"C:..." but I want it to be something like "UNIVERSAL" path. I tried using: "\Sounds\mySound.wav" but it generates an error.
The lines that I use there this directory are:
myList.Add("\Sounds\11000_0.2s.wav");//Error
using (WaveFileReader reader = new WaveFileReader(sourceFile))
where sourceFile is a string which express a path of the file.
Make sure that you check CopyToOutputDir in the properties of the soundfile, that will make sure the file is copied to the location you program runs from.
Also don't use single backslashes in the path since its an escape character.
Instead, do one of the following things:
Use a verbatim string:
#"Sounds\11000_0.2s.wav"
Escape the escape char:
"Sounds\\11000_0.2s.wav"
Use forward slashes:
"Sounds/11000_0.2s.wav"
For more information on string literals check msdn.
You either need to escape the / in the string or add the string literal indicator # at the beginning of the string.
Escape example:
var myFilePath = "c:\\Temp\\MyFile.txt";
String literal example:
var myFilePath = #"c:\Temp\MyFile.txt";