Click here to Skip to main content
12,635,630 members (26,544 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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 4-Nov-12 11:38am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

just fix it like this

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");

            }
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161208.2 | Last Updated 4 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100