I have this simple basic code in to create new file and write some data to it. The problem is when I run the program I don't get anything neither the file is created nor any error messages or exceptions is shown, even when debugging the code no errors appear.
This is the code:
namespace WriteToFile
{
class WriteData
{
static void Main(string[] args)
{
string nFile = @"C:\Users\Ashraf\Documents\newFilex.txt";
FileStream fs = null;
// checking if file already exists and getting user input on deleting it or not
if (File.Exists(nFile))
{
Console.WriteLine("File already exists!");
Console.WriteLine("What would you like to do? \n");
Console.WriteLine("\"DELETE\" to delete the file, or press Enter to continue ?");
string ans;
ans = Console.ReadLine();
switch (ans)
{
case "":
Console.WriteLine("continue ...");
break;
case "DELETE":
File.Delete(nFile);
if (File.Exists(nFile))
{
Console.WriteLine("Error deleting file!");
}
else
{
Console.WriteLine("File deleted successfuly");
}
break;
default:
break;
}
// continue creating new file and inserting data to it
try
{
fs = new FileStream(nFile, FileMode.CreateNew, FileAccess.ReadWrite);
for (char c = 'A'; c <= 'Z'; c++)
{
fs.WriteByte((byte)c);
}
Console.WriteLine("File created and data written to file successfuly");
}
catch (IOException ce)
{
Console.WriteLine("\t File couldn't be created! \t \n" + ce.Message);
}
finally
{
Console.Read();
fs.Dispose();
}
}
}
}
}