在VB.NET中单击按钮时打开txt文件

时间:2013-09-16 10:07:00

标签: vb.net vb.net-2010

我的项目中有一个日志文件。该文件是文本文件(.txt)。有没有办法在没有使用OpenFileDialog工具的情况下点击按钮时打开此文件?

请注意,我正在使用VB.NET 2010。

3 个答案:

答案 0 :(得分:21)

Dim FILE_NAME As String = "C:\FileName.txt"

If System.IO.File.Exists(FILE_NAME) = True Then
    Process.Start(FILE_NAME)
Else
    MsgBox("File Does Not Exist")
End If

答案 1 :(得分:5)

这是一个简单的例子:

 Public Class OpenTextFile

    Private Sub OpenTextFile_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub OpenBUT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenBUT.Click
        'OpenBUT_Click the text file
        Process.Start("C:\File Name.txt")
    End Sub
End Class

答案 2 :(得分:0)

尝试一下

 Dim file as string = "whatever file you want"
 If System.IO.File.Exists(file) = True Then
      Process.Start(file)
 Else
        MsgBox("File Does Not Exist!!", MsgBoxStyle.Critical)
 End If