Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,023 questions

51,974 answers

573 users

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 348 views
348 views asked Mar 11, 2015 by avibootz
2 answers 246 views
1 answer 188 views
1 answer 174 views
2 answers 254 views
1 answer 203 views
203 views asked Nov 29, 2017 by avibootz
...