OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How do I include newlines with PowerShell.AddCommand("Set-Content") in c#?

  • Thread starter Thread starter newGrad coder
  • Start date Start date
N

newGrad coder

Guest
I have code like the following that turns a list of strings into one string of the contents separated by \n

Code:
List<string> list = new List<string>();
list.Add("string1");
list.Add("string2");
list.Add("string3");

string stringOfListContents = string.Join("\n", list);

then when I write the string to a output file like:

Code:
using (PowerShell PowerShellInstance = PowerShell.Create())
{
    PowerShellInstance.Streams.ClearStreams();
    PowerShellInstance.AddCommand("Set-Content");

    PowerShellInstance.AddParameter("Path", outputFile);
    PowerShellInstance.AddParameter("Value", stringOfListContents);

    PowerShellInstance.Invoke();
}

The result is

Code:
string1string2string3

but I would like the result in the txt file to be like

Code:
string1
string2
string3
<p>I have code like the following that turns a list of strings into one string of the contents separated by \n</p>
<pre><code>List<string> list = new List<string>();
list.Add("string1");
list.Add("string2");
list.Add("string3");

string stringOfListContents = string.Join("\n", list);
</code></pre>
<p>then when I write the string to a output file like:</p>
<pre><code>using (PowerShell PowerShellInstance = PowerShell.Create())
{
PowerShellInstance.Streams.ClearStreams();
PowerShellInstance.AddCommand("Set-Content");

PowerShellInstance.AddParameter("Path", outputFile);
PowerShellInstance.AddParameter("Value", stringOfListContents);

PowerShellInstance.Invoke();
}
</code></pre>
<p>The result is</p>
<pre><code>string1string2string3
</code></pre>
<p>but I would like the result in the txt file to be like</p>
<pre><code>string1
string2
string3
</code></pre>
Continue reading...
 
Top