在Vb.Net中打开文件帮助 [英] Open File Help in Vb.Net

查看:66
本文介绍了在Vb.Net中打开文件帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个文字处理程序,但是我在使用打开文件"功能时遇到了麻烦,这是代码:

I am creating a word processor, but I am having trouble with the Open File function, here is the code:

Private Sub FileOpen()
        openFd.InitialDirectory = "C:\"
        openFd.Title = "Open a Text File"
        openFd.Filter = "Text Files(*.txt)|*.txt|Word Files(*.doc|*.doc|All(*.*)|*.*"
        Dim Work As Integer = openFd.ShowDialog()
        strFileName = openFd.FileName
        Dim objReader As New System.IO.StreamReader(strFileName)
        BodyText.Text = objReader.ReadToEnd
        objReader.Close()
    End Sub


当执行此操作时,会弹出对话框,但是当我单击打开"时,会弹出保存"对话框,导致我无法打开文件,这是错误的吗?

注意:STreamreader不起作用?


When I do this, the dialog pops up, but when I click "Open", a "Save" Dialog pops up, causing me not to open the file, hwats wrong?

Note: STreamreader does not work?

推荐答案

您可能已在窗体上放置了一个SaveFileDialog控件,而不是一个OpenFileDialog.我们无法分辨,因为您的代码没有说.确实,您甚至根本不需要将其放在表单上.你可以做
You may have dropped a SaveFileDialog control on the form instead of an OpenFileDialog. We can''t tell as your code doesn''t say. Really, you don''t even need to drop it on your form at all. You can just do
Dim ofd As New OpenFileDialog
ofd.'' set properties as appropriate
'' BTW: You missed a closing parenthesis in the Filter line
Dim result As DialogResult = ofd.ShowDialog
If result = DialogResult.Ok Then
    Using reader As New StreamReader(ofd.Filename)
        BodyText.Text = reader.ReadToEnd
    End Using
End If


另外,您无法读取这样的Word文件. .DOC和.DOCX文件不是文本或RTF文件,根本不会在TextBox或RichTextBox中呈现.


Also, you can''t read Word files like this. .DOC and .DOCX files are not text or RTF files and won''t render in a TextBox or a RichTextBox at all.


这篇关于在Vb.Net中打开文件帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆