Click here to Skip to main content
11,410,118 members (60,669 online)
Rate this: bad
good
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 at 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 at 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 at 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
0 Sergey Alexandrovich Kryukov 169
1 OriginalGriff 110
2 Maciej Los 68
3 BillWoodruff 60
4 Philippe Mori 60
0 Sergey Alexandrovich Kryukov 8,755
1 OriginalGriff 6,915
2 Maciej Los 3,390
3 Abhinav S 3,248
4 Peter Leow 3,059


Advertise | Privacy | Mobile
Web01 | 2.8.150414.5 | Last Updated 4 Nov 2012
Copyright © CodeProject, 1999-2015
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