0
using System;

namespace CAT
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            string path = @"E:\CAT\Name.txt";
            if (!System.IO.File.Exists(path))
            {
                System.IO.File.Create(path);
                System.IO.TextWriter tw = new System.IO.StreamWriter(path);
                tw.WriteLine("File Created!");
                tw.Close();
            } else if (System.IO.File.Exists(path)){
                System.IO.TextWriter tw = new System.IO.StreamWriter(path, true);
                tw.WriteLine("New Boot.");
                tw.Close();
            }

        Console.WriteLine("Directory System - Copyright 2016 xxxxCo.");
        Console.WriteLine("What is your surname?");
        string Surname = Console.ReadLine();
        System.IO.TextWriter fileEnter = new System.IO.StreamWriter(path);
        fileEnter.WriteLine(Surname);
        Console.WriteLine ("What is your first name?");
        string fn = Console.ReadLine ();
        fileEnter.WriteLine(fn);
        Console.WriteLine ("Hello " + fn + " " + Surname);
        Console.Read();
        Console.Clear();
        Console.WriteLine("Directory System - Copyright 2016 xxxxCo.");
        Console.WriteLine ("Year?");
        string year = Console.ReadLine();
        // New file created for year and meta data
        string yearpath = @"E:\CAT\Year.txt";
        if (!System.IO.File.Exists(yearpath))
        {
            System.IO.File.Create(yearpath);
            System.IO.TextWriter tw = new System.IO.StreamWriter(yearpath);
            tw.WriteLine("File Created!");
            tw.Close();
        }
        else if (System.IO.File.Exists(yearpath))
        {
            System.IO.TextWriter tw = new System.IO.StreamWriter(yearpath, true);
            tw.WriteLine("New Boot.");
            tw.Close();
        }
        // new text writer created for year data
        System.IO.TextWriter fileEnterYear = new System.IO.StreamWriter(yearpath);
        fileEnterYear.WriteLine(year + " " + fn.Substring(0, 1) + Surname.Substring(0, 1));
        Console.Read();
    }
}

Please excuse my horrible organisation, I whipped this up as a test program since I just downloaded Xamarin Studio. Each of the files are created within their respective locations but then neither are written to. Please help me to fix this as I can't see what my error is. Many Thanks

Rahul
  • 76,197
  • 13
  • 71
  • 125
  • are you familiar with using the debugger.. why is it that people want to write code but can't use the tools that are right at their disposal in regards to stepping through their own code.. before closing the file put `tw.Flush()` then close the file also.. and to not call `Dispose` directly wrap your StreamWriter code around a `using` for example `using(StreamWriter tw = new StreamWriter(File.OpenWrite(yearpath))` – MethodMan Sep 16 '15 at 20:50
  • 1
    If you debug your code and see that it is actually executing the write, try to use Flush before issuing close. See: http://stackoverflow.com/questions/7376956/close-a-filestream-without-flush – NoChance Sep 16 '15 at 21:00
  • are you wanting all the lines written to a single file.. I will quickly refactor your mess and make it more readable give me 2 mins – MethodMan Sep 16 '15 at 21:09
  • Will File.WriteAllText and File.AppendAllText work? No streams required, and WriteAllX will create the file automatically. – Pedro Sep 16 '15 at 21:23
  • @TriggSharp I fixed your code and made functional please let me know if you have any other issues thanks – MethodMan Sep 16 '15 at 22:31

2 Answers2

1

After you create the file, you need to dispose it so other processes can use it. I would use

System.IO.File.Create(path).Dispose();

In addition it looks like you are trying to write to an external drive. I would try creating and writing to your hard drive first.

Seth Kitchen
  • 1,526
  • 19
  • 53
1

You had quite a few problems with you code for starters you forgot to add a Console.Read(); to capture the Year I also created a method that you can call once and have the code at the bottom check if the file exist.. if not it creates it ..then it appends to the exiting files

    static void Main(string[] args)
    {
        string path = @"E:\CAT\Name.txt";

        if (!System.IO.File.Exists(path))
        {
            WriteAndOrAppendText(path, "File Created");
        }
        else if (System.IO.File.Exists(path))
        {
            WriteAndOrAppendText(path, "New Boot.");             
        }

        Console.WriteLine("Directory System - Copyright 2016 xxxxCo.");
        Console.WriteLine("What is your surname?");
        string Surname = Console.ReadLine();
        WriteAndOrAppendText(path, Surname);
        Console.WriteLine("What is your first name?");
        string fn = Console.ReadLine();
        WriteAndOrAppendText(path, fn);
        Console.WriteLine(string.Format("Hello {0}, {1}",  fn , Surname));
        Console.Read();
        Console.Clear();
        Console.WriteLine("Directory System - Copyright 2016 xxxxCo.");
        Console.WriteLine("Year?");
        Console.Read();
        string year = Console.ReadLine();
        // New file created for year and meta data
        string yearpath = @"E:\CAT\Year.txt"; ;
        if (!System.IO.File.Exists(yearpath))
        {
            using (StreamWriter tw = new StreamWriter(File.OpenWrite(yearpath)))
            {
                tw.WriteLine("File Created!");
                tw.Flush();
            }
        }
        else if (System.IO.File.Exists(yearpath))
        {
            WriteAndOrAppendText(yearpath, "New Boot.");
        }
        // new text writer created for year data
        var substrText = string.Format("{0} {1} {2}", year, fn.Substring(0, 1), Surname.Substring(0, 1));
        WriteAndOrAppendText(yearpath, substrText);
        Console.WriteLine("Program Complete please check the files Located in {0} , {1}", path, yearpath);
        Console.Read();
    }


    private static void WriteAndOrAppendText(string path, string strText)
    {
        if (!File.Exists(path))
        {
            StreamWriter fileStream = new StreamWriter(path, true);
            fileStream.WriteLine(strText);
            fileStream.Flush();
            fileStream.Close();
        }
        else
        {
            StreamWriter fileStream2 = new StreamWriter(path, true);
            fileStream2.WriteLine(strText);
            fileStream2.Flush();
            fileStream2.Close();
        }
    }
MethodMan
  • 18,625
  • 6
  • 34
  • 52