
将 RichTextBox 的内容储存到特定种类的文件中。Saves the contents of the RichTextBox to a specific type of file.
public:
void SaveFile(System::String ^ path, System::Windows::Forms::RichTextBoxStreamType fileType);
public void SaveFile (string path, System.Windows.Forms.RichTextBoxStreamType fileType);
member this.SaveFile : string * System.Windows.Forms.RichTextBoxStreamType -> unit

Public Sub SaveFile (path As String, fileType As RichTextBoxStreamType)
要保存的文件的名称和位置。The name and location of the file to save.
fileType
RichTextBoxStreamType

RichTextBoxStreamType 值之一。One of the RichTextBoxStreamType values.
ArgumentException
fileType 参数中选定了无效的文件类型。An invalid file type is specified in the fileType parameter.
IOException

将控件内容保存到文件时错误。An error occurs in saving the contents of the control to a file.
下面的代码实例将 RichTextBox 的内容保存到 ASCII 文本文件中。The following code example saves the contents of the RichTextBox into an ASCII text file. 该实例使用 SaveFileDialog 类来显示一个对话框,用于请求用户的模式跟文件名。The example uses the SaveFileDialog class to display a dialog to request the path and file name from the user. 然后,代码将控件的内容保存到该文件。The code then saves the contents of the control to that file. 该实例使用 SaveFile 方法的此版本来指定将文件另存为 ASCII 文本文件,而不是标准 rtf 格式。The example uses this version of the SaveFile method to specify that the file be saved as an ASCII text file instead of the standard rich text format. 此例子要求将代码放置在带有名为 richTextBox1RichTextBox 控件的 Form 类中。This example requires that the code is placed in a Form class that has a RichTextBox control named richTextBox1.
public:
void SaveMyFile()
{
// Create a SaveFileDialog to request a path and file name to save to.
SaveFileDialog^ saveFile1 = gcnew SaveFileDialog;
// Initialize the SaveFileDialog to specify the RTF extension for the file.
saveFile1->DefaultExt = "*.rtf";
saveFile1->Filter = "RTF Files|*.rtf";
// Determine if the user selected a file name from the saveFileDialog.
if ( saveFile1->ShowDialog() == System::Windows::Forms::DialogResult::OK &&
saveFile1->FileName->Length > 0 )
{
// Save the contents of the RichTextBox into the file.
richTextBox1->SaveFile( saveFile1->FileName, RichTextBoxStreamType::PlainText );
}
}
public void SaveMyFile()
{
// Create a SaveFileDialog to request a path and file name to save to.
SaveFileDialog saveFile1 = new SaveFileDialog();
// Initialize the SaveFileDialog to specify the RTF extension for the file.
saveFile1.DefaultExt = "*.rtf";
saveFile1.Filter = "RTF Files|*.rtf";
// Determine if the user selected a file name from the saveFileDialog.
if(saveFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
saveFile1.FileName.Length > 0)
{
// Save the contents of the RichTextBox into the file.
richTextBox1.SaveFile(saveFile1.FileName, RichTextBoxStreamType.PlainText);
}
}

Public Sub SaveMyFile()
' Create a SaveFileDialog to request a path and file name to save to.
Dim saveFile1 As New SaveFileDialog()
' Initialize the SaveFileDialog to specify the RTF extension for the file.
saveFile1.DefaultExt = "*.rtf"
saveFile1.Filter = "RTF Files|*.rtf"
' Determine if the user selected a file name from the saveFileDialog.
If (saveFile1.ShowDialog() = System.Windows.Forms.DialogResult.OK) _
And (saveFile1.FileName.Length) > 0 Then
' Save the contents of the RichTextBox into the file.
richTextBox1.SaveFile(saveFile1.FileName, _
RichTextBoxStreamType.PlainText)
End If
End Sub
利用 SaveFile 方法,你可以将控件的全部内容保存至能由其它程序(如 Microsoft Word 和 Windows 写字板)使用的 RTF 文件中。The SaveFile method enables you to save the entire contents of the control to an RTF file that can be used by other programs such as Microsoft Word and Windows WordPad. 如果传递到 path 参数的文件名在选定的目录中未存在,则将在不通知的状况下覆盖该文件。If the file name that is passed to the path parameter already exists at the specified directory, the file will be overwritten without notice. 您可以使用 LoadFile 方法将文件的内容加载至 RichTextBox中。You can use the LoadFile method to load the contents of a file into the RichTextBox.
此版本的 SaveFile 方法让你可以指定应将控件的内容保存到的文件类型。This version of the SaveFile method enables you to specify a file type to save the contents of the control to. 您可以使用此用途来保证基于控件的内容以正确的格式保存文件。You can use this feature to ensure that the file is saved in the proper format based on the contents of the control. 例如richtextbox保存,如果文档的图标形状或着色没有差别,则可以借助将 fileType 参数设定为 RichTextBoxStreamType.PlainTextrichtextbox保存,将该文件另存为 ASCII 文本文件。For example, if your document has no differences in font style or coloring, you can save the file as an ASCII text file by setting the fileType parameter to RichTextBoxStreamType.PlainText.
FileIOPermission
用于创建或更改文件。for creating or modifying a file. 关联枚举: FileIOPermissionAccess的 Write 值。Associated enumeration: The Write value of FileIOPermissionAccess.
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-135213-1.html
会在网上实名揭露吗
>相信