You want to specify a pattern for the time, especially if you'll be
using the value in a filename (slashes are not valid in a file name).
DateTime.Now.To String("yyyy-MM-dd--HH-mm-ss-") + "Report.dat "
But you have to be concerned with multiple clients making the file at
the same time so you may want to include a counter within a loop and
error checking:
int i = 1;
bool alreadyUsed = false;
FileStream fs;
do {
string name = null;
try {
name =
String.Format(" {0:yyyy-MM-dd--HH-mm-ss}--{1:000}-Report.dat",
DateTime.Now, i++);
fs = new FileStream(name , FileMode.Create );
} catch(IOExcepti on ex) {
if (File.Exists(na me)) {
alreadyUsed = true;
} else {
throw;
}
}
} while(alreadyUs ed);
If you think the likihood of two processes creating the same file is
high then you can check File.Exists before trying to create the file
to be more efficient, but you still need the error handling since
another process could create the file between your File.Exists call
and the new FileStream call.
HTH,
Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On 6 Dec 2006 12:41:01 -0800,
ew******@gmail. com wrote:
>The following code which is supposed to dynamically create files with
incrementing names is throwing an exception due to the inclusion of the
DateTime.Now component of the sReportSave string:
string sReportSave = ( DateTime.Now + "Report.Dat " );
FileStream fs = new FileStream( sReportSave , FileMode.Create );
Is my approach completely wrong or is there different way to modify the
FileStream so that .NETclients used by multiple users could save files
incrementall y without saving to the same file? The logic that I would
normally use for this sort of thing seems unsuitable due to the fact
that I am trying to make the clients run independantly of each other
even if used on the same machine.
-E