Click here to Skip to main content
15,897,968 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to write a bit of text to a file called reciept.txt. I have included the "System.IO" namespace. When I click the save button it should just write that simple bit of text in the file. When I open the file however, it's just blank. The file already exists.


private void btnSave_Click(object sender, EventArgs e)
      {
          string deskPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
          bool fileExists = File.Exists(DesktopBounds + "/reciept.txt");

          using (StreamWriter sw = new StreamWriter(deskPath+"reciept.txt"))
          {
              sw.WriteLine("asdasdasd");

          }
      }


Any suggestions why it's not writing even when things look right?
Posted

1 solution

just fix it like this

C#
string deskPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            bool fileExists = File.Exists(DesktopBounds + "/reciept.txt");


            using (StreamWriter sw = new StreamWriter(Path.Combine(deskPath,"reciept.txt")))
            {
                sw.WriteLine("asdasdasd");

            }
 
Share this answer
 
Comments
FourCrate 4-Nov-12 17:21pm    
Thanks for the input. That didn't solve my problem but I noticed the problem was that the piece of code above was automatically creating a new text file called "reciept" and updating that instead.
FourCrate 4-Nov-12 17:35pm    
I am replying this to you as there is a bug and I can't create my own solution :/

I noticed the problem was that the piece of code above was automatically creating another new text file called "reciept" and updating that instead. I thought the file I created manually called "reciept.txt" would be the same as what I am writing to in the code but apparently not.
The code I have was automatically creating a new text file because it didn't exist when I hadn't actually programmed it to create a new file.
Ahmedwaheed 5-Nov-12 12:12pm    
do you want to append to the file if it is existed is it right?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900