0

在 Windows 中,我们可以通过右键单击它来打开一个文本文件,然后通过任何软件(如记事本、notepad++ 等)选择“打开方式”选项。我想使用 Visual Basic 创建一个记事本类型的软件,它可以打开(加载)任何通过右键单击在 Windows 资源管理器中使用“打开方式”选项的文本文件。我尝试使用文本框和按钮创建一个简单的记事本,它可以通过浏览文件系统并选择文件来加载文本文件。但不能通过“打开方式”选项加载。

4

1 回答 1

0

这是一个例子。您需要了解,在我的 Windows 7 系统上,文件类型 .txt 的默认程序设置是 Sublime Text。我假设您了解如果您将 .txt 文件类型设置为使用记事本。在您的系统上使用我的代码,文件中的文本将使用下面的这行代码与记事本一起显示。

Process.Start(openPath)

要使用此代码,请在您的表单上创建一个 RichTextBox 大小约为 1150 x 850 将其命名为 rtbViewCode
您将需要两个名为 btnViewFile 和 btnCopy 的按钮
我已经包含一些额外代码,可让您将文件文本复制到剪贴板

 Private Sub btnViewFile_Click(sender As Object, e As EventArgs) Handles btnViewFile.Click
    Dim openFileDialog As OpenFileDialog = New OpenFileDialog()
    Dim dr As DialogResult = openFileDialog.ShowDialog()

    If dr = System.Windows.Forms.DialogResult.OK Then

        Dim openPath As String = openFileDialog.FileName
        MsgBox("openPath " & openPath)

        Process.Start(openPath)
        'Line of code above should be used behind it's own Button Click Event

        rtbViewCode.Text = File.ReadAllText(openPath)

    End If
End Sub

Private Sub btnCopy_Click(sender As Object, e As EventArgs) Handles btnCopy.Click
    rtbViewCode.SelectAll()
    rtbViewCode.Copy()
End Sub
于 2022-02-09T21:12:43.400 回答