【问题标题】:C# Program not writing to a fileC#程序不写入文件
【发布时间】:2015-09-16 20:25:32
【问题描述】:
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,所以我把它作为一个测试程序来做。每个文件都是在它们各自的位置创建的,但随后都不会被写入。请帮我解决这个问题,因为我看不到我的错误是什么。 非常感谢

【问题讨论】:

  • 你熟悉使用调试器吗..为什么人们想编写代码但不能使用他们可以使用的工具来逐步执行他们自己的代码..之前关闭文件放tw.Flush() 然后关闭文件也.. 并且不调用Dispose 直接将您的StreamWriter 代码包装在using 周围,例如using(StreamWriter tw = new StreamWriter(File.OpenWrite(yearpath))
  • 如果您调试代码并看到它实际上正在执行写入,请尝试在发出关闭之前使用 Flush。见:stackoverflow.com/questions/7376956/…
  • 您是否希望将所有行都写入一个文件。我会快速重构您的混乱并使其更具可读性,请给我 2 分钟
  • File.WriteAllText 和 File.AppendAllText 会起作用吗?不需要流,WriteAllX 会自动创建文件。
  • @TriggSharp 我修复了您的代码并使其正常运行,如果您有任何其他问题,请告诉我,谢谢

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


【解决方案1】:

创建文件后,您需要将其释放,以便其他进程可以使用它。我会用

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

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

【讨论】:

    【解决方案2】:

    对于忘记添加 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();
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2021-09-22
      • 2016-02-19
      • 2012-01-16
      • 1970-01-01
      • 2014-01-30
      • 2021-11-11
      • 2012-02-19
      • 2016-09-15
      • 1970-01-01
      相关资源
      最近更新 更多