Macros - steamwriter?

This is where you talk about Macros, show examples of your macro scripting and SHARE handy segments of script code as examples.

Macros - steamwriter?

Postby Robertspark » Thu Jun 29, 2017 12:40 pm

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
Robertspark
 
Posts: 1892
Joined: Sat Sep 03, 2016 4:27 pm

Re: Macros - steamwriter?

Postby Robertspark » Thu Jun 29, 2017 12:50 pm

....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);
}
Robertspark
 
Posts: 1892
Joined: Sat Sep 03, 2016 4:27 pm

Re: Macros - steamwriter?

Postby dezsoe » Thu Jun 29, 2017 12:55 pm

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");
dezsoe
 
Posts: 2093
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: Macros - steamwriter?

Postby Robertspark » Thu Jun 29, 2017 1:13 pm

thanks dezsoe
Robertspark
 
Posts: 1892
Joined: Sat Sep 03, 2016 4:27 pm

Re: Macros - steamwriter?

Postby dezsoe » Thu Jun 29, 2017 2:04 pm

You were faster! :)
dezsoe
 
Posts: 2093
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary


Return to Macros

Who is online

Users browsing this forum: No registered users and 3 guests