Click here to Skip to main content
15,910,661 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

i am just trying to create a file with its name as current date and checking whether the file is existing or not.if existing i am writing some text to the file otherwise i am appending some data to the file.

the code i wrote is

C#
StringBuilder  log=new StringBuilder ();
            
            string path= string.Format(@"C:/Documents and Settings/aj99823/Desktop/project/{yyyy-mm-dd}.txt",System.DateTime .Now .Date);
            
            if (!File.Exists(path))
            {
                log = new StringBuilder (path);
                File.WriteAllText(path, commaDelimitedText.ToString ()); 
            }
            else
            {
                File.AppendAllText(path, commaDelimitedText.ToString());
            }


its giving me error saying input string is not in correct format...
can anyone pleae help me...........??????
Posted
Updated 14-Mar-12 20:33pm
v2

look man i solve it; i try it locally and i get the same error and the new modifications are below:

C#
StringBuilder log = new StringBuilder();

        //string path = string.Format(@"C:\Tester\Lottery_Windows\{yyyy-mm-dd}.txt", System.DateTime.Now.Date);
        string path = @"C:/Documents and Settings/aj99823/Desktop/project/" + DateTime.Now.ToString("yyyy-MM-dd");

        if (!File.Exists(path))
        {
            log = new StringBuilder(path);
            File.WriteAllText(path, "Hello World");
        }
        else
        {
            File.AppendAllText(path, "Hello World");
        }


if this help you please rate
best regards
 
Share this answer
 
Comments
amaljosep 15-Mar-12 3:01am    
yes...its working....thanks a looooooot for helping me.....
youssef_123 15-Mar-12 3:03am    
you are welcome but plzz can you rate for meeee
youssef_123 15-Mar-12 3:04am    
and improve solution for others if they faced the same problem
amaljosep 15-Mar-12 3:28am    
see i need to confirm my email address for doing the rating......will surely do it in the evening...
youssef_123 15-Mar-12 4:03am    
ok
Error is due to casting issue at path string construction. Updated code should be:

C#
string path= string.Format(@"C:/Documents and Settings/aj99823/Desktop/project/{yyyy-mm-dd}.txt",System.DateTime.Now.ToString("yyyy-MM-dd ));


It should work.
 
Share this answer
 
Comments
amaljosep 15-Mar-12 2:47am    
no ...its throwing the same error again....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900