这段代码在VS 2010中工作得很完美。现在我有了VS 2013,它不再写入文件。它没有错误或任何东西。 (我在记事本中得到一个警报++,说明该文件已被更新,但并没有什么写的。)StreamWriter不能在C#中工作
这一切看起来好像没什么问题。有任何想法吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
String line;
try
{
//Pass the file path and file name to the StreamReader constructor
StreamReader sr = new StreamReader("C:\\Temp1\\test1.txt");
StreamWriter sw = new StreamWriter("C:\\Temp2\\test2.txt");
//Read the first line of text
line = sr.ReadLine();
//Continue to read until you reach end of file
while (line != null)
{
//write the line to console window
Console.WriteLine(line);
int myVal = 3;
for (int i = 0; i < myVal; i++)
{
Console.WriteLine(line);
sw.WriteLine(line);
}
//Write to the other file
sw.WriteLine(line);
//Read the next line
line = sr.ReadLine();
}
//close the file
sr.Close();
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Executing finally block.");
}
}
}
}
你在寻找正确的目录吗?你是否打开该文件作为完整性检查? – user1666620
尝试显式刷新以及。 – Lloyd
我没有看到任何你称之为sw.Close();如果你这样做,写入将被刷新并关闭文件。此外,你应该看看包裹的StreamReader和StreamWriter使用块 - 它们都实现IDisposable,而当你离开使用块 – thorkia