Ho provato a buttare giu questo codice, il file temp me lo scrive bene, ma quando arrivo ad aprire due nuovi stream per riversare quello che ce scritto nel file temporaneo nel file "reale" che a me serve mi dice che è in utilizzo da un'altro processo, eppure mi pare di aver chiuso abbondantemente tutti gli stream:
codice:
public void scrivi(string linea)
{
//mi creo uno stremwriter temporaneo
path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
string nomefile = "\\temp.txt";
FileStream temp_fs_writer = new FileStream(path + @nomefile, FileMode.Append, FileAccess.Write);
StreamWriter temp_stremwriter = new StreamWriter(temp_fs_writer);
temp_stremwriter.WriteLine(linea);
//mi creo uno streamreader temporaneo
FileStream temp_fs_reader = new FileStream(path + this.@file_scrittura, FileMode.OpenOrCreate, FileAccess.Read);
StreamReader temp_streamreader = new StreamReader(temp_fs_reader);
string lettura = "";
while (!temp_streamreader.EndOfStream)
{
lettura = temp_streamreader.ReadLine();
temp_stremwriter.WriteLine(lettura);
}
temp_streamreader.Close();
temp_stremwriter.Close();
temp_fs_reader.Close();
temp_fs_writer.Close();
//mi creo uno stremwriter temporaneo
temp_fs_writer = new FileStream(path + this.@file_scrittura, FileMode.Create, FileAccess.Write);
temp_stremwriter = new StreamWriter(temp_fs_writer);
//mi creo uno streamreader temporaneo
FileStream temp_fs_reader2 = new FileStream(path + nomefile, FileMode.Open, FileAccess.Read);
StreamReader temp_streamreader2 = new StreamReader(temp_fs_reader2);
while (!temp_streamreader2.EndOfStream)
{
lettura = temp_streamreader2.ReadLine();
temp_stremwriter2.WriteLine(lettura);
}
temp_streamreader2.Close();
//temp_stremwriter2.Close();
temp_fs_reader2.Close();
// temp_fs_writer2.Close();
}
}
Secondo voi dove sbaglio? Ma contando che il file temporaneo lo devo riversare totalmente su un'altro file, non c'è un modo di farlo senza dover scandire tutto linea per linea? qualche comando del tipo "cancella file x; rinomina y in x" ?