Questo pezzo di codice ha funzionato perfettamente in VS 2010. Ora che ho VS 2013 non scrive più sul file. Non c'è errore o niente. (Ricevo un avviso in Notepad ++ affermando che il file è stato aggiornato, ma non c'è scritto nulla.)StreamWriter Non funziona in C#
Mi sembra tutto a posto. Qualche idea?
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.");
}
}
}
}
stai cercando nella directory giusta? hai aperto il file come controllo di integrità? – user1666620
Prova anche a svuotare esplicitamente. – Lloyd
Non vedo da nessuna parte che tu chiami sw.Close(); Se lo fai, la scrittura verrà svuotata e il file verrà chiuso. Inoltre, dovresti davvero considerare il wrapping di StreamReader e StreamWriter usando i blocchi: entrambi implementano IDisposable e si chiuderanno automaticamente quando lascerai il blocco using – thorkia