How to write string to a text file in C#

3 Answers

0 votes
using System;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            File.WriteAllText("d:\\file.txt", "C# language features in the .NET Framework");
        }
    }
}

/*
run:
   
file.txt:
--------
C# language features in the .NET Framework

*/


answered Mar 11, 2015 by avibootz
0 votes
using System;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "C# language features in the .NET Framework";

            File.WriteAllText("d:\\file.txt", s);
        }
    }
}

/*
run:
   
file.txt:
--------
C# language features in the .NET Framework

*/


answered Mar 11, 2015 by avibootz
0 votes
using System;   
using System.IO;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = @"d:\test.txt";

            try
            {
                  string s = "c# .net vb java" + Environment.NewLine;
                  File.WriteAllText(path, s);
            }
            catch (FileNotFoundException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (IOException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Console.WriteLine("finally");
            }
        }
    }
}


/*
run:
    
finally

*/

 



answered Apr 26, 2017 by avibootz

Related questions

1 answer 371 views
371 views asked Mar 11, 2015 by avibootz
2 answers 276 views
1 answer 206 views
1 answer 198 views
2 answers 285 views
1 answer 224 views
224 views asked Nov 29, 2017 by avibootz
...