1

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();
                    }
                }
            }
        }
    }
Ashraf Sada
  • 4,527
  • 2
  • 44
  • 48
  • permissions problems maybe? Check if using the assembly path as stated here: http://stackoverflow.com/questions/52797/how-do-i-get-the-path-of-the-assembly-the-code-is-in may help. If the file is created then it's definetely permission problems. – Miquel Coll Jan 22 '15 at 11:09
  • 1
    Probabl I'm not getting you. Your code, if the file already exists, doesn't do nothing – LPs Jan 22 '15 at 11:13
  • I can see one problem. You have a conditional statement that checks if the file exists or not. The program will only do something if the file already exists. But what if the file does not already exist? Nothing will happen. Is this intentional? – St0ffer Jan 22 '15 at 11:16
  • @LPs The program will only do anything if the file DOES already exist. – St0ffer Jan 22 '15 at 11:17
  • Your are correct the problem is in the check file is exists, it is not logically coded. Thank you. – Ashraf Sada Jan 22 '15 at 11:18

2 Answers2

2

You got the indentation wrong:. Try this, with if(exists) and try { Create on the same level:

if (File.Exists(nFile))
{
    Console.WriteLine("File already exists!");
    ...
}
// continue creating new file and inserting data to it
try
{
    fs = new FileStream(nFile, FileMode.CreateNew, FileAccess.ReadWrite);
    ...
}
DrKoch
  • 9,556
  • 2
  • 34
  • 43
1

Problem was found in logical flow of the program, as the if statement was blocking execution of the rest of the code. The correct code:

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;
                }
            }
            else
            {
                try
                {
                    fs = new FileStream(nFile, FileMode.CreateNew, FileAccess.ReadWrite);

                    // Write Abc to file
                    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();
                }
            }
        }
    }
Ashraf Sada
  • 4,527
  • 2
  • 44
  • 48