为什么我的 OpenFileDialog 不起作用?
Why my OpenFileDialog didn´t work?
我尝试打开一个文本文档,然后收到消息:来自 try-catch
的文件格式无效。我将 Visual Studio 2015 与 Visual C# 和 Windows 表单应用程序一起使用。
这里是我打开函数的代码:
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
try {
// Create an OpenFileDialog to request a file to open.
OpenFileDialog openFile1 = new OpenFileDialog();
// Initialize the OpenFileDialog to look for RTF files.
openFile1.Filter = "Text Files (*.txt)|*.txt| RTF Files (*.rtf)|*.rtf| All (*.*)|*.*";
// 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.
TextBox.LoadFile(openFile1.FileName);
}
}
catch (Exception a)
{
MessageBox.Show(a.Message);
}
}//end open
希望你能帮我送上友情祝福sniffi
问题可能是您没有加载 RTF 文档 - 请参阅 MSDN 上的 docs。
With this version of the LoadFile method, if the file being loaded is
not an RTF document, an exception will occur. To load a different type
of file such as an ASCII text file, use the other versions of this
method that accept a value from the RichTextBoxStreamType enumeration
as a parameter.
所以尝试使用这种方法的重载version,它接受这样的流类型(根据您的需要调整)
TextBox.LoadFile(openFile1.FileName, RichTextBoxStreamType.PlainText);
我尝试打开一个文本文档,然后收到消息:来自 try-catch
的文件格式无效。我将 Visual Studio 2015 与 Visual C# 和 Windows 表单应用程序一起使用。
这里是我打开函数的代码:
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
try {
// Create an OpenFileDialog to request a file to open.
OpenFileDialog openFile1 = new OpenFileDialog();
// Initialize the OpenFileDialog to look for RTF files.
openFile1.Filter = "Text Files (*.txt)|*.txt| RTF Files (*.rtf)|*.rtf| All (*.*)|*.*";
// 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.
TextBox.LoadFile(openFile1.FileName);
}
}
catch (Exception a)
{
MessageBox.Show(a.Message);
}
}//end open
希望你能帮我送上友情祝福sniffi
问题可能是您没有加载 RTF 文档 - 请参阅 MSDN 上的 docs。
With this version of the LoadFile method, if the file being loaded is not an RTF document, an exception will occur. To load a different type of file such as an ASCII text file, use the other versions of this method that accept a value from the RichTextBoxStreamType enumeration as a parameter.
所以尝试使用这种方法的重载version,它接受这样的流类型(根据您的需要调整)
TextBox.LoadFile(openFile1.FileName, RichTextBoxStreamType.PlainText);