简体   繁体   English

输出到文本文件时在新行上设置内容

[英]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. 我有一个脚本,要求用户使用Windows富文本框格式输入信息,每条信息输入不同的行,然后脚本将信息添加到文本文件,但我希望它通过以下方式分隔信息:新线的手段。 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. 用`n拆分文本并将其传递给文件。 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. RichTextBox希望为您提供RTF格式化文本。 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. 当你要求Text时,我认为它剥离了格式化代码并且只给你字母和空格,我认为它不会把它变成一个轻量级的ascii格式化的东西。

You might want to use the .Lines property , which returns a string[] that you can loop through and write to the file. 您可能希望使用.Lines属性,该属性返回可以循环并写入文件的字符串[]。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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