-4

For eg., I have a code in C#

a = 10
b = 10
c = a * b

I want to redirect the output c to a text file using c#. Can you please let me know how to do the same?

mario
  • 360
  • 1
  • 10
  • Check MSDN, in particular `System.IO.File` class and its `WriteXyz()` methods (BTW that _code_ isn't _really_ C#). – Adriano Repetti Nov 13 '14 at 11:13
  • [Easiest way to read from and write to files](http://stackoverflow.com/questions/7569904/easiest-way-to-read-from-and-write-to-files). – CodeCaster Nov 13 '14 at 11:15

1 Answers1

1

Try StreamWriter:

var a = 10;
var b = 10;
var c = a * b;

System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test.txt");
file.WriteLine(c);

file.Close();
Vladimirs
  • 8,232
  • 4
  • 43
  • 79
  • My -1 because 1) to answer such poor questions doesn't help anyone and it may encourage more and more bad questions. 2) There are tons of better questions on SO that can be used to close this as duplicate. 3) It's longest way to do it and 4) even if it's a fictional example it should _at least_ be _protected_ with using. – Adriano Repetti Nov 13 '14 at 11:17
  • 1
    @AdrianoRepetti 1) Poor question for you but not everyone have that knowledge that you have and know where to start at all 2) agree 3) I just pointed to one of possible solutions 4) no need for using - file.Close() will do the same. But thanks for explanation. – Vladimirs Nov 13 '14 at 11:22
  • 1
    1) no, IMO it's not about knowledge but about effort and research. StackOverflow doesn't replace a book or a tutorial, _homework_ MUST (see [help]) be done before asking. Where _working code_ is? Where an attempt (any!) is? Where some research is? 3) In case WriteLine() fails (for any reason, disk full, connection lost, whatever...) – Adriano Repetti Nov 13 '14 at 13:59