George
George

Reputation: 85

how to write to a text file in C# in Linux with MONO Develop

Im using Ubunto OS with MONO Develop and Im programming with C#.

I want to write into a text file but I dont sure how to do it.

I tried this:

string[] lines = {"some text1", "some text2", "some text3"};
System.IO.File.WriteAllLines(@"/home/myuser/someText.txt", lines);

this didn't work.

I tried this:

string str = "some text";

StreamWriter a = new StreamWriter("/home/myuser/someText.txt");

a.Write(str);

this didn't work too.

what to do?

tnx.

Upvotes: 2

Views: 10669

Answers (2)

Matteo
Matteo

Reputation: 21

Make sure you close the stream (File.Close() or a.Close(), I'm not familiar with c# syntax) as only when the stream is disposed, it actually writes on the file.

Upvotes: 2

sehe
sehe

Reputation: 393084

Both should work, perhaps you forgot to provide the application code?

using System;
using System.IO;

public class Program
{
    public static int Main(string[] args)
    {
         string[] lines = {"some text1", "some text2", "some text3"};
         File.WriteAllLines(@"/home/myuser/someText.txt", lines);
         return 0;
    }
}

Compile with dmcs program.cs, e.g.

Upvotes: 8

Related Questions