I need to create a file with a name of IP address:
The code is:

string dirAddress = "C:\\FTPTrace\\" + ipHeader.DestinationAddress.ToString() + ".txt";
System.IO.StreamWriter logfile = new StreamWriter(dirAddress,true);

It works fine.. But i also want to include current date in the file name...
The code is :

string dirAddress = "C:\\FTPTrace\\" + ipHeader.DestinationAddress.ToString() + DateTime.Now.ToShortDateString().ToString()+ ".txt";

System.IO.StreamWriter logfile = new System.IO.StreamWriter(dirAddress, true);

Now the file is not being created , an exception is raised

"A first chance exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll"

Kindly guide where am I wrong.... thanks...

Recommended Answers

All 4 Replies

this error because when you use this

DateTime.Now.ToShortDateString().ToString()

it gives following output and with the slashes(/) 2/15/2012
so insted of try this to solve a problem or

DateTime.Now.ToShortDateString().ToString().Replace("/","")
DateTime.Now.ToShortDateString().ToString().Replace("/","-")
DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Date.ToString()

Thanks Alot... It works....

You don't have to use DateTime.Now.ToShortDateString().ToString(); use DateTime.Now.ToShortDateString();
And for your problem use : DateTime.Now.ToString("dd-MM-yyyy");

commented: Excellent, to-the-point advice. +12
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.