0

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...

3
Contributors
4
Replies
6
Views
5 Years
Discussion Span
Last Post by saneeha.nust
Featured Replies
  • 1
    ddanbe 2,525   5 Years Ago

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

0

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()
1

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");

Comments
Excellent, to-the-point advice.
This question has already been answered. Start a new discussion instead.
Be sure to adhere to our posting rules.