|
Separating the date into it's components isn't very useful. It's much easier to just format the date the way that you want: date.ToString("ddMMyyyy") .
If you don't know how to concatenate strings and use variables instead of literal string values, you really need to pick up a beginners book and gets some basics down.
---
single minded; short sighted; long gone;
|
|
|
|
|
...I know how to concatenate strings! the problem is how I then refer to that "variable" when creating the .csv
|
|
|
|
|
...In case it is not clear...
int day = date.Day;
int month= date.Month;
int year= date.Year;
string str="hi";
int res="123";
string fileName = day + month + year + "_" + str + "_" + res
However, if I do : File.Create("c:\\code\\fileName.txt"), it creates a file with the name "fileName"...rather than the value of fileName
...any ideas?
|
|
|
|
|
Have you heard of string.Format()? Surely you must have seen it in the intellisence and wondered if it might be useful.
mocasu wrote: File.Create("c:\\code\\fileName.txt"), it creates a file with the name "fileName"...rather than the value of fileName
Have you thought of also concatenating the file path into the string too?
|
|
|
|
|
...what can I say...thanks both of you for being so ...
|
|
|
|
|
mocasu wrote: ...what can I say...thanks both of you for being so ...
"Sarcastic"? (That's the normal accusation)
|
|
|
|
|
...if that's what you think you were being...Not everybody thinks the same...All of us can finish it with something different, it is just a matter of judgement
|
|
|
|
|
mocasu wrote: ...if that's what you think you were being...
I didn't say that I thought it. I said it was what I (we) are normally accused of.
|
|
|
|
|
go like this:
File.Create("c:\\code\\" + filename + ".txt");
It's one of those obvious things that you can't see...like having to look a little right or left to see a dim star.
Dave
|
|
|
|
|
Thanks a lot David! really appreciate...that is what i was looking for.
-- modified at 11:39 Saturday 13th October, 2007
|
|
|
|
|
You're welcome
|
|
|
|
|
Colins sarcasm came from the fact that you knew how to do string concantenation, but when it came to giving a string representing a filename, you suddenly forgot how to do string concantenation!
Someone told you that "1+1=2" and you said "I KNOW!!", then when they asked you "what's 1+1?", you said you didn't know!
|
|
|
|
|
...and what "good" did sarcasm brought to the whole afair!? ...
I see your point Dave...
" but when it came to giving a string representing a filename, you suddenly forgot how to do string concantenation! "
It wasn't that I forgot,I just didn't know that it could be done like that...as you all might have gueesed by now I am new with this. Anyway, thanks to PIEBALconsul and David for the help
|
|
|
|
|
mocasu wrote: It wasn't that I forgot,I just didn't know that it could be done like that...
That's why I'm saying that you should start with the basics.
If you only can use something in a way that you've seen it used before, you have only learned to mimic others, you haven't really learned how it works.
---
single minded; short sighted; long gone;
|
|
|
|
|
Dave Kreskowiak wrote: Someone told you that "1+1=2" and you said "I KNOW!!", then when they asked you "what's 1+1?", you said you didn't know!
...depends on whether or not you're working in binary, hex, decimal, octal...you need to be more specific
|
|
|
|
|
Or strings.
Or meta information.
|
|
|
|
|
You need to buy a book on C# and read it. It's pretty basic stuff that anything in quotes, is a verbatim string. Your variable name becomes a variable name only when it's not in quotes.
@"c:\code\" + filename + ".txt";
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
mocasu wrote: ...I know how to concatenate strings! the problem is how I then refer to that "variable" when creating the .csv
So you don't know how to use a variable instead of a literal string.
Here's an example:
This is some code with a literal string:
Console.WriteLine("Hello world!");
You can put the string in a variable and use that variable in place of the literal string:
string message;<br />
message = "Hello world!";<br />
Console.WriteLine(message);
This is very basic in programming, and what you are trying to do is on a much higher level. You should start with the basics before moving on to more complex things.
---
single minded; short sighted; long gone;
|
|
|
|
|
System.IO.FileInfo fi = new System.IO.FileInfo
(
string.Format
(
"{0:ddMMyyyy}_{1}_{2}.csv"
,
System.DateTime.Now
,
"hi"
,
123
)
) ;
using ( System.IO.FileStream tw = fi.OpenWrite() )
{
...
}
However, I do suggest you format the date as "yyyyMMdd" to comply with ISO 8601.
Don't bother testing for file existence unless you want to read it, not write it.
The OpenWrite will create the file if it doesn't exist, and overwrite it if it does.
|
|
|
|
|
Thanks for the info.;P
I was checking for file existance in case the file had already been created, so the new file should have some extra character in the name to differenciate them.
|
|
|
|
|
Well that's alright then.
|
|
|
|
|
System.IO.FileInfo fi ;
int seq = 0 ;
do
{
fi = new System.IO.FileInfo
(
string.Format
(
"{0:ddMMyyyy}_{1}_{2:000}.csv"
,
System.DateTime.Now
,
"hi"
,
++seq
)
) ;
} while ( fi.Exists ) ;
using ( System.IO.FileStream tw = fi.OpenWrite() )
{
}
|
|
|
|
|
|
I wrote a windows service that runs under the system account.
This service should launch other programs and monitor them, so it's a kind of a watchdog.
I use the System.diagnostics.process class to launch these programs.
The code is working perfectly when I run this in a simple console.
but this doesn't work in the service.
Process.start() simply returns, no program is launched, no exception is thrown...
what is wrong here ?
Is there another approuch that I can try?
gr,
Niko
<< Nearly all men can stand adversity, but if you want to test a man's character, give him power. >>
|
|
|
|
|
NikoTanghe wrote: Process.start() simply returns, no program is launched, no exception is thrown...
what is wrong here ?
The problem is you don't understand how services work. Services run under their own desktop, NOT the users desktop that you can see. The Process.Start() code worked. The application was launched. All you have to do is open TaskManager and click on the Processes tab to see that.
You have to configure your service to interact with the users desktop. Click Start/Run, then type SERVICES.MSC and hit enter. Find your service in the list and double-click it. Click on the LogOn tab and find the little check box that says something like "Allow server to interact with desktop" and enable it.
Now, YOU HAVE A MUCH BIGGER PROBLEM. Services run all the time, even when noone is logged into the machine. So, with no user desktop to show an app, you're service will still launch the applications, but noone will ever see them because there is no user desktop! Just logging into the machine will NOT automatically show you the launched applications either. They will be running, but on the services desktop only. There is no way to transfer the application between desktops. In order for the logged in user to see them, the applications would have to be killed and restarted.
Notice, in the .NET Framework's Service classes, that you don't get any "User logged on" events...
|
|
|
|