|
-
Dec 2nd, 2003, 09:43 AM
#1
Replace doesn't work
this...
Code:
DateTime.Now.ToShortDateString().Replace("\\", "-")
...doesn't get rid of the "\" in the date string such as "02\12\03"
it should be giving me "02-12-03".
Whereas, this DOES work...
Code:
DateTime.Now.ToShortTimeString().Replace(":", ".")
which gives me "10.15" rather than "10:15".
What the hell is going on?
this is my full code...
Code:
string pathTemp = itsLocalLogPath + category + "_" + DateTime.Now.ToShortDateString().Replace("\\", "-") + "_" + DateTime.Now.ToShortTimeString().Replace(":", ".") + EXTN_LOG_FILE;
//write the error message to a local file on the HDD
MySW = new StreamWriter(pathTemp, false);
Last edited by wossname; Dec 2nd, 2003 at 11:00 AM.
I don't live here any more.
-
Dec 2nd, 2003, 10:54 AM
#2
the replace function doesnt work that way ( ie: DateTime.Now.ToShortDateString().Replace("\\", "-") )
you have to make a value = the item , eg:
VB Code:
[COLOR=blue]string[/COLOR] strDate = DateTime.Now.ToShortDateString().Replace("\\", "-")
then the string strDate will hold the date with the replaced item.
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Dec 2nd, 2003, 11:09 AM
#3
I was thinking that too, but weirdly the bit highlited in blue does work (see original code again). And I haven't a clue why.
Why should one work and the other not? They are doing pretty much the same thing anyway (removing characters that would confuse the filesystem).
The only difference I can see is the escape sequence in the red code. But surely that is handled by the precompiler.
I don't live here any more.
-
Dec 2nd, 2003, 03:37 PM
#4
Frenzied Member
Originally posted by wossname
I was thinking that too, but weirdly the bit highlited in blue does work (see original code again). And I haven't a clue why.
Why should one work and the other not? They are doing pretty much the same thing anyway (removing characters that would confuse the filesystem).
The only difference I can see is the escape sequence in the red code. But surely that is handled by the precompiler.
It works because its being concatenated and assigned to another variable. Thats how the strings work.
-
Dec 2nd, 2003, 10:01 PM
#5
PowerPoster
Replace(@"\", "-")
You can do above instead of escaping it. The @ symbol makes the following string literal. Don't know if that will help your problem, but it is not widely known you can do that so I thought I would offer that up.
-
Dec 3rd, 2003, 03:18 AM
#6
good point hellswraith , same applies if you are opening files ( say in a StreamReader ) you specify the path like this... @"C:\yourpath here"
rather than using double \\
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Dec 3rd, 2003, 08:21 PM
#7
Hyperactive Member
PHP Code:
DateTime.Now.ToShortDateString().Replace("\\\", "-")
works perfectly fine - It doesn't replace anything because there are no backslashes \ in DateTime.Now.ToShortDateString(). DateTime.Now.ToShortDateString() returns 12/3/2003 so if ya wanna use Replace, use .Replace("/","-"). What's kinda cool is instead of using the replace dealio, the ToString() on DateTime.Now can take formatters or a datetime format string as arguments so you can do this:
PHP Code:
DateTime.Now.ToString("dd-mm-yyyy_hh.MM")
to get the same effect as the two replace snippets. Here's a link to an explanation of the characters you can use:http://msdn.microsoft.com/library/de...matstrings.asp
-
Dec 4th, 2003, 10:15 AM
#8
PVB has solved the mystery.
My program was crashing and filling the console with the error message. It was something along the lines of "Cannot find path c:\...whatever\02\12\03\...
It didn't even enter my head that the forward slash also works as a directory delimiter. And I'd got confused with the backslashes used in the date.
Anyway the long and short of it is that it now works fine.
Many thanks to all.
PS I did happen to know the @"\" trick! But that didn't do anything to solve it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|