[英]C# Program not writing to a file
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,所以将其作为测试程序进行了宣传。 每个文件都在其各自的位置中创建,但随后均未写入。 请帮助我解决此问题,因为我看不到我的错误是什么。 非常感谢
创建文件后,需要处理它,以便其他进程可以使用它。 我会用
System.IO.File.Create(path).Dispose();
此外,您似乎正在尝试写入外部驱动器。 我会先尝试创建并写入您的硬盘。
您对忘记添加Console的启动器的代码有很多问题。 为了捕获Year,我还创建了一个方法,该方法可以调用一次,并在代码底部检查文件是否存在..如果不存在,则将其创建..然后将其追加到现有文件中
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.