Page 1 of 1

Macros - steamwriter?

PostPosted: Thu Jun 29, 2017 12:40 pm
by Robertspark
Macros, is it possible to write to a text file using steamwriter or something else?

Tried a few variations of this:


Code: Select all
// Compose a string that consists of three lines.
string lines = "First line";

// Write the string to a file.
System.IO.StreamWriter file = new System.IO.StreamWriter("C:\UCCNC\test.txt");

file.WriteLine(lines);

file.Close();


Thanks for any help / direction

Re: Macros - steamwriter?

PostPosted: Thu Jun 29, 2017 12:50 pm
by Robertspark
....answered own question... sort of...


Code: Select all
// Create a string array with the lines of text
string[] lines = { "First line", "Second line", "Third line" };

// Set a variable to the My Documents path.
string mydocpath =
    Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

// Write the string array to a new file named "WriteLines.txt".
using (System.IO.StreamWriter outputFile = new System.IO.StreamWriter(mydocpath + @"\WriteLines.txt")) {
    foreach (string line in lines)
        outputFile.WriteLine(line);
}

Re: Macros - steamwriter?

PostPosted: Thu Jun 29, 2017 12:55 pm
by dezsoe
Longer:

Code: Select all
using (System.IO.StreamWriter sw = System.IO.File.CreateText(sOutputPath))
{
   sw.WriteLine("... text ...");
   sw.WriteLine();
   // ...
}

And shorter:

Code: Select all
System.IO.File.WriteAllText(FileName, "text without cr/lf");

Re: Macros - steamwriter?

PostPosted: Thu Jun 29, 2017 1:13 pm
by Robertspark
thanks dezsoe

Re: Macros - steamwriter?

PostPosted: Thu Jun 29, 2017 2:04 pm
by dezsoe
You were faster! :)