Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
namespace SimpleLicense
{
    class Program
    {
        static void Main(string[] args)
        {
        
            string fileName = @"C:\\Temp\\abc.txt";

            try
            {
                // Check if file already exists. If yes, delete it. 
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }

                // Create a new file 
                using (StreamWriter sw = File.CreateText(fileName))
                {
                    sw.WriteLine("Installed Date: {0}", DateTime.Now.ToString());
                    sw.WriteLine("Thermo Licensing System file");
                    
                    DateTime newDate = DateTime.Now.AddDays(31);
                    sw.WriteLine("License Expires After"+newDate);
                    
                   // sw.WriteLine("Add ");
                   // sw.WriteLine("Done! ");
                }

                // Write file contents on console. 
                using (StreamReader sr = File.OpenText(fileName))
                {
                    string s = "";
                    while ((s = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(s);
                    }
                }
            }
            catch (Exception Ex)
            {
                Console.WriteLine(Ex.ToString());
            }
        }
    }
}


Hello Everyone,

I have written the above program to create a .txt file to a particular directory.But I am not able to create the file.I even changed the path to string fileName = @"D:\\abc.txt"; ,the program compiles but a file to that particular directory is not created.Would anyone help me to figure out what could be the error?

Thanks
Posted
Comments
ZurdoDev 19-May-14 10:54am    
All you have to do is debug it. Have you done that?

Hi,

The problem is on this line:
C#
string fileName = @"C:\\Temp\\abc.txt";

Try to replace it with this:
C#
string fileName = @"C:\Temp\abc.txt";


In a normal string you would need to escape \ into \\. But you're using @ before the file name, so you don't need to do that.
 
Share this answer
 
v2
Comments
vivek murli 19-May-14 9:52am    
I rectified.Still not able to create
Andrius Leonavicius 19-May-14 10:06am    
I think that you don't have a folder "Temp" in the C: drive.
Try adding the line in bold:
// Create a new file
Directory.CreateDirectory(Path.GetDirectoryName(fileName));
using (StreamWriter sw = File.CreateText(fileName))
Sergey Alexandrovich Kryukov 19-May-14 11:50am    
Good catch, both the answer and the comment. My 5.
I added a couple of good suggestions in Solution 3, please see.
—SA
Andrius Leonavicius 19-May-14 12:31pm    
Thank you, Sergey. +5 for you as well.
Sergey Alexandrovich Kryukov 19-May-14 12:39pm    
Thank you, Andrius.
—SA
In addition to the correct Solution 1:

C#
if (File.Exists(fileName)) // no need to do it
{
    File.Delete(fileName); // don't delete the file, if will be overwritten anyway
}


And also, don't use File.CreateText and the like. Instead, use something like:
using (StreamWriter writer = new StreamWriter(fileName)) {
    writer.WriteLine(/* ... */);
    // ...
} // here, writer.Dispose() is automatically called


You will need to do similar thing with the reader.

—SA
 
Share this answer
 

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