简体   繁体   中英

Set-content on a new line when outputting to a text file

I have a script which asks the user to enter information using a windows rich text box form, each piece of information is entered on a different line, then the script adds the information to a text file, but i want it to seperate the information by means of a new line. below is the code i am using: at the moment is adds the information as one piece of text without any spaces or lines. any help would be appreciated

cls
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "Till's to Update"
$objForm.Size = New-Object System.Drawing.Size(300,550) 
$objForm.StartPosition = "CenterScreen"



$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,480)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$x=$RichTextBox.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,480)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20) 
$objLabel.Size = New-Object System.Drawing.Size(280,20) 
$objLabel.Text = "Please enter information:"
$objForm.Controls.Add($objLabel) 

$RichTextBox = New-Object System.Windows.Forms.richtextbox 
$RichTextBox.Location = New-Object System.Drawing.Size(10,80) 
$RichTextBox.Size = New-Object System.Drawing.Size(260,320)
$objForm.Controls.Add($RichTextBox) 


$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

$x


Set-Content -Value  `n$x`n -Path c:\Computers.txt

Split the text by `n and pipe it to the file. You can remove the last two lines oin your script.

$OKButton.Add_Click({
    $RichTextBox.Text -split "`n" | Set-Content -Path c:\Computers.txt
    $objForm.Close()
})

Change these lines

$x
Set-Content -Value  `n$x`n -Path c:\Computers.txt

with

set-content -Value ($RichTextBox.text -split "`n" ) -Path c:\Computers.txt

or

set-content -Value $RichTextBox.Lines -Path c:\Computers.txt

The RichTextBox wants to give you RTF formated text. When you ask for Text, I think it strips the formating codes and gives you just the letters and spaces, I don't think it turns it into a lightweight ascii formating thing.

You might want to use the .Lines property , which returns a string[] that you can loop through and write to the file.

ref: http://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.lines.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM