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 d开发者_如何学JAVAidn'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.
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.
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.
精彩评论