C#程序不写入文件

时间:2015-09-16 20:25:33

标签: c# visual-studio c#-4.0 xamarin

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();
    }
}

请原谅我这个可怕的组织,因为我刚刚下载了Xamarin Studio,所以我把它作为一个测试程序。每个文件都在各自的位置创建,但都没有写入。请帮我解决这个问题,因为我无法看到我的错误。 非常感谢

2 个答案:

答案 0 :(得分:1)

创建文件后,需要对其进行处理,以便其他进程可以使用它。我会用

Route::controllers([
  'auth' => 'Auth\AuthController',
]);

此外,您似乎正在尝试写入外部驱动器。我会先尝试创建和写入你的硬盘。

答案 1 :(得分:1)

你忘记添加Console.Read()的初学者代码有很多问题;捕获年份我还创建了一个方法,你可以调用一次,底部的代码检查文件是否存在..如果没有它创建它..然后它附加到退出文件

    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();
        }
    }