5

I am using C# windows Forms in visual studio 2010.

I need to know what the best way at going about outputting data that has been selected in a winForm, into a simple text file would be please?

An Example:

The user has to select a time/date. once the user selects it, they then click confirm.

i need the confirm button in the winForm to send the date/time to a txt file that is saved onto the computer at a given location.

any help in this area will be much appreciated!

Simagen
  • 409
  • 2
  • 8
  • 18

2 Answers2

9

Use the System.IO.StreamWriter.

As an example:

System.IO.StreamWriter writer = new System.IO.StreamWriter("path to file"); //open the file for writing.
writer.Write(DateTime.Now.ToString()); //write the current date to the file. change this with your date or something.
writer.Close(); //remember to close the file again.
writer.Dispose(); //remember to dispose it from the memory.

If you have issues writing to the file, it may be due to UAC (User Account Control) blocking access to that specific file. The typical workaround would be to launch the program as an administrator to prevent this from happening, but I find that to be a rather bad solution.

Also, storing data in the documents folder is unpleasant for the user. Some users (including myself) like to keep my documents folder full of... documents, and not application settings and configuration files.

Therefore, I suggest using the Local Application Data folder (found if you type %localappdata% in the start menu search-field). This folder is not locked by UAC, and will not require administrative priviledges to write to. It is actually meant for exactly this purpose; storing settings and data.

The path to this directory can be returned using:

string path = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

I hope this answers your question.

Mathias Lykkegaard Lorenzen
  • 15,031
  • 23
  • 100
  • 187
  • okay thank you mathias.. i have tried implementing it, but the writer.write shows syntax errors? it says that 'writer' is a field but is used as a 'type'? any ideas? – Simagen Dec 25 '10 at 03:00
  • 1
    Your code example is fine, but calling both Close and Dispose is redundant as Close internally calls Dispose (I think. It might be the other way around), so you should actually just call Dispose here. – Øyvind Bråthen Dec 25 '10 at 07:54
  • Ah, thanks for the heads-up Øyvind. I can tell you that Close does not call Dispose. It has to be the other way around, if anything. But very useful! Thanks! – Mathias Lykkegaard Lorenzen Dec 25 '10 at 13:14
  • Cameron, what kind of error are you receiving? Make sure that you put "()" behind method-calls, such as ToString(). Also make sure that you do NOT put "()" behind non-method calls such as properties like DateTime.Now. – Mathias Lykkegaard Lorenzen Dec 25 '10 at 13:16
  • it says that 'writer' is a field but is used as a 'type' any ideas? – Simagen Dec 25 '10 at 23:05
  • I need more code to see what the general issue could be. Post the line with the error in it. – Mathias Lykkegaard Lorenzen Dec 25 '10 at 23:07
  • I'd like to ask the line ```System.IO.StreamWriter writer = new System.IO.StreamWriter("path to file");``` the file needs to be existing right? In my case the file is non-existent and should be created. What code should be used to create a text file in my case? – leipzy Apr 25 '19 at 09:08
2

How about...

String YourFormattedText = FieldFromForm.ToString( whatever formatting );
File.WriteAllText( FullPathAndFileNameToWriteTo, YourFormattedText );
DRapp
  • 47,638
  • 12
  • 72
  • 142