I've a problem with a file! but i think this is due to a code error i can't see.
My program create a file with some information within. Then I search the information into the file, I fine them, and write them to screen (I use visual studio pro evaluetion ^^) with some textbox.. then I give the possibility to modify or delete the content found, to do this i make a string of all file (i will quote my code!) then i search use the replace string.replace("before";"after");, but this is not the problem. Now I've my string with all file modified, and ready to replace the all the old file! So i decide do rewrite all the file with the new string, but it doesn't change. If i decide to save it to a new file, than it works... but I don't need this!
Code:
/* Already created string named elimina (i'm italian..) with the content i want to delete in the file, and the string fileContent, with all the file content.*/
try
{
//Pass the file path and file name to the StreamReader constructor (some comments are of the msdn, not mine :D)
StreamReader sr = new StreamReader("lista.txt"); // file name is lista.txt into the folder of the executable
fileContent = sr.ReadToEnd(); // taken all the file
fileContent = fileContent.Replace(elimina, ""); // deleted from the new string what I don't want anymore
//close the file
sr.Close();
Console.ReadLine();
}
catch (Exception f)
{
Console.WriteLine("Exception: " + f.Message);
}
finally
{
Console.WriteLine("Executing finally block.");
}
/* Now I try to overwrite all the file lista.txt with the new string fileContent */
try
{
//Open the File
StreamWriter sc = new StreamWriter("Lista.txt", false);
sc.Write(fileContent);
//close the file
sc.Close();
}
catch (Exception de)
{
Console.WriteLine("Exception: " + de.Message);
}
finally
{
Console.WriteLine("Executing finally block.");
}
Thanks for reading, and sorry for my italian-school english! (No good teacher... XD)
