在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.