[英]StreamWriter not creating file?
我写了一个输出文本文件的类。 它运行良好,并且完成了工作。 但是,我执行了相同的过程,但没有成功(未创建文件)。 我觉得自己缺少了一些非常简单的东西-我缺少了什么?
工作代码(在Main方法中):
static void Main(string[] args)
{
Kinematics hello = new Kinematics();
System.IO.StreamWriter file = new System.IO.StreamWriter
("C:/Users/myName/Documents/GoogleDrive/folder/folder/Level 3.txt");
file.WriteLine("Time(s)\tPosition(m)\tVelocity(m/s)\tAcceleration(m/s^2)");
//create a file to print stuff in.
file.Close();
//close the file.
}
不起作用的代码:
class Program
{
static void Main(string[] args)
{
System.IO.StreamWriter file = new System.IO.StreamWriter
("C:/Users/myName/Documents/GoogleDrive/folder/folder/hellooo.txt");
file.WriteLine("Time(s)\tPosition(m)\tVelocity(m/s)\tAcceleration(m/s^2)");
Projectile ballProjectile = new Projectile();
ballProjectile.Level1(file);
ballProjectile.Level2(file);
ballProjectile.Level3(file);
file.Close();
}
}
文件路径完全相同,并且都是正确的,并且我再次检查了是否在正确的位置查找了文件。
运动学进行一些计算,我使用file.WriteLine();
写出方法的输出。 所有的编写都是在main方法中完成的。 到目前为止,Level1,Level2,Level3方法是空方法。 稍后它们将运行file.WriteLine();
方法。
将文件file.Close()
移到上方
Projectile ballProjectile = new Projectile();
ballProjectile.Level1(file);
ballProjectile.Level2(file);
ballProjectile.Level3(file);
另外,如评论中所建议,StreamWriter是一次性的,因此您应该这样做:
var file = new System.IO.StreamWriter(@"C:/Users/myName/Documents/GoogleDrive/folder/folder/hellooo.txt")
using (file)
{
file.WriteLine("Time(s)\tPosition(m)\tVelocity(m/s)\tAcceleration(m/s^2)");
}
注意文件路径开头的@
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.