【问题标题】:VB 2010 openfiledialog file format not validVB 2010 openfiledialog 文件格式无效
【发布时间】:2014-11-30 02:03:38
【问题描述】:

我已经在文本编辑器上工作了一段时间,接近完成终于决定开始研究这个问题,这个问题困扰了我很长一段时间......

我有一个 openfiledialog 总是给我“文件格式无效”的错误

Private Sub OpenToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles OpenToolStripMenuItem.Click
    Dim openWork As New OpenFileDialog
    openWork.Filter = "Text Documents (*.swtf)|*.swtf|Text Documents (*.rtf)|*.rtf|All Files (*.*)|*.*"
    If openWork.ShowDialog = Windows.Forms.DialogResult.OK Then
        RichTextBox1.LoadFile(openWork.FileName, RichTextBoxStreamType.RichText)
        Title.Text = System.IO.Path.GetFileNameWithoutExtension(openWork.FileName)
    End If
End Sub

错误 RichTextBox1.LoadFile(openWork.FileName, RichTextBoxStreamType.RichText) 没有解决方案,但总是给我“文件格式无效”错误 - 包括我的自定义文件扩展名和 .rtf“富文本文件”扩展名.

谢谢!

【问题讨论】:

    标签: vb.net richtextbox openfiledialog rich-text-editor


    【解决方案1】:

    如果您打开的文件是真正的富文本文件,它就可以工作。如果您尝试加载不是 .rtf 的文件,则会抛出 file format not valid 错误,因为您在 LoadFile 方法中使用了RichTextBoxStreamType.RichText

    你可以试试这样的……我在 VB2010 中测试过,它可以工作:

        Dim openWork As New OpenFileDialog
        openWork.Filter = "Text Documents (*.swtf)|*.swtf|Text Documents (*.rtf)|*.rtf|All Files (*.*)|*.*"
        If openWork.ShowDialog = Windows.Forms.DialogResult.OK Then
            'if the file is an .rtf, use the rich text format, if not, use plain text
            RichTextBox1.LoadFile(openWork.FileName, IIf(System.IO.Path.GetExtension(openWork.FileName) = ".rtf", RichTextBoxStreamType.RichText, RichTextBoxStreamType.PlainText))
            Title.Text = System.IO.Path.GetFileNameWithoutExtension(openWork.FileName)
        End If
    

    【讨论】:

    • 这里唯一的问题是,现在如果我打开一个设置为 rtf 文件格式的 .swtf,它默认为纯文本并且不可读,给出“文件格式无效”错误
    • 实际上它仍然给我两种文件类型的文件扩展名错误,有没有办法强制它读取无论如何? - 在尝试 rtf 之前,我尝试打开 .swtf
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多