Skip to main content
 首页 » 编程设计

c#之如何在 RichTextBox 中加载 .rtf 文件

2024年02月13日12zdz8207

我有两种形式。如果我单击第一个表单上的按钮,我希望它自动加载第二个表单,其中 RichTextBox 加载了 .rtf 文件。

我想请求帮助在 RichTextBox 中加载一个 .rtf 文件,而没有在表单中编码路径?我尝试使用 Directory.GetCurrentDirectory,但我遇到了困难,因为我不是那么有经验的程序员。

请您参考如下方法:

试试这个:

public void LoadMyFile() 
{ 
   // Create an OpenFileDialog to request a file to open. 
   OpenFileDialog openFile1 = new OpenFileDialog(); 
 
   // Initialize the OpenFileDialog to look for RTF files. 
   openFile1.DefaultExt = "*.rtf"; 
   openFile1.Filter = "RTF Files|*.rtf"; 
 
   // Determine whether the user selected a file from the OpenFileDialog.  
   if(openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK && 
      openFile1.FileName.Length > 0)  
   { 
      // Load the contents of the file into the RichTextBox. 
      richTextBox1.LoadFile(openFile1.FileName); 
   } 
}