a file doesn't want to be deleted or overwritted

Discussion in 'C#' started by raikoug, Jan 19, 2012.

  1. raikoug

    raikoug New Member

    Joined:
    Jan 19, 2012
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hi all,
    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.");
                }
    
    
    This code give no error, but it doesn't work. If in the second stream of file I choose to write into a file named "what i want.txt" it works perfectly, creating the new file and writing on it the new content i wanted.

    Thanks for reading, and sorry for my italian-school english! (No good teacher... XD)
     
  2. raikoug

    raikoug New Member

    Joined:
    Jan 19, 2012
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I'm going mad on this (in italian we say these exactly words, dunno if in english it means anything... sorry). But the conclusion is that visual studio is mad:
    This code totally work, IF you click 5 times on the button that should start that code... if you click it once, it doesn't works... do you know something similar o why it does so? Thanks
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice